node*beginning(node*head,intx)//In this function, the head pointer needs to point to the next node.
{
node*insertnode=create(x,head);// This is creating the next node
head=insertnode;// The variable head is being assigned to insertnode
returnhead;// The program will return head.
}
node*insertNodeAfter(node*head,intx,node*idnode)
{
if(head==NULL||idnode==NULL)// The if statement is comparing if both pointers equal null. If they do equal null, the program will return null.
returnNULL;
node*pointer=head;
while(pointer!=idnode)
pointer=pointer->n;
if(pointer!=NULL)
{
node*insertnode=create(x,pointer->n);
pointer->n=insertnode;
returnhead;
}
else
{
returnNULL;
}
}
node*removefirstNode(node*head)
{
if(head==NULL)
returnNULL;
node*first=head;
head=head->n;
first->n=NULL;
if(first==head)
head=NULL;
free(first);
returnhead;
}
node*removeNode(node*head,node*hold)
{
if(hold==NULL)
returnNULL;
if(hold->n==NULL)
returnremovefirstNode(head);
node*point=head;
while(point!=NULL)
{
if(point->n==hold)
break;
point=point->n;
}
if(point!=NULL)
{
node*spare=point->n;
point->n=spare->n;
spare->n=NULL;
free(spare);
}
returnhead;
}
voiddisplay(node*dis)
{
if(dis!=NULL)
printf("%d ",dis->x);
}
node*find(node*head,intx)
{
node*point=head;
while(point!=NULL)
{
if(point->x==x)
{
returnpoint;
}
point=point->n;
}
returnNULL;
}
voidprintlist(node*head){
node*now=head;
while(now!=NULL){
printf("%d\n",now->x);
now=now->n;
}
}
voidtraverse(node*head,callbacka)
{
node*locate=head;
while(locate!=NULL)
{
a(locate);
locate=locate->n;
}
}
node*sortList(node*head)
{
node*a,*b,*c;
a=head;
head=NULL;
while(a!=NULL)// if a is equal to nothing, node c will equal to a and a will point to itself and the node n.
{
c=a;
a=a->n;
if(head!=NULL)
{
if(c->x>head->x)//x is the integer variable for the node structure.If c point to x is less than head pointing to head, b gets assgined to head.
{
b=head;
while((b->n!=NULL&&c->x>b->n->x))// In the while loop, while b point to n is not equal to null and if c point to x is greater than b pointing to n pointing to x
{
b=b->n;// b will point to n to compare the values
}
c->n=b->n;// c pointing to n would equal b pointing to n if the numbers need switching.
b->n=c;// When the switch is finished, b pointing to n would equal to c
}
else
{
c->n=head;// In the else statement, c pointing to n would equal to head if the numbers are finished switching places.
head=c;// After the switching has finshed, c will pass the sorted list to head.
}
}
else
{
c->n=NULL;// In the second else statement, c pointing to n will equal null if there are no switches made.
head=c;// c will pass the current state of the list to head.
}
}
returnhead;
}
node*reverseList(node*head)
{
node*first=NULL;
node*now=head;
node*n;
while(now!=NULL)
{
n=now->n;
now->n=first;
first=now;
now=n;
}
head=first;
returnhead;
}
voidmenu()
{
printf("0.menu\n");
printf("1. Insert some nodes into the list\n");
printf("2. Insert node after a previous node\n");
printf("3. Remove a node from the front of the list\n");
printf("4. Remove any node from the list\n");
printf("5. Sort the list\n");
printf("6. Reverse the linked list\n");
printf("-1.quit\n");
}
intmain()
{
intinstruction=0;
intx;
node*head=NULL;
node*spare=NULL;
callbackshow=display;
menu();
while(1)
{
printf("Enter a command(0-6,-1 to quit):");
scanf("%d",&instruction);
if(instruction==-1)
break;
switch(instruction)
{
case0:
menu();
break;
case1:
printf("Enter 6 numbers into the list: ");
scanf("%d",&x);
head=beginning(head,x);
traverse(head,show);
break;
case2:
printf("Enter a number to insert after the previous number:");
scanf("%d",&x);
spare=find(head,x);
if(spare!=NULL)
{
printf("Enter the value you wish to insert after:");
scanf("%d",&x,&spare);
head=insertNodeAfter(head,x,spare);
if(head!=NULL)
traverse(head,show);
}
else
{
printf("The number %d does not exist",x);
}
break;
case3:
head=removefirstNode(head);
if(head!=NULL)
traverse(head,show);
break;
case4:
printf("Enter a number that you want to remove:");