typedefstructnode{int value;//this is the value the node storesstructnode*next;//this is the node the current node points to. this is how the nodes link}node;
node *createNode(int val){
node *newNode =malloc(sizeof(node));
newNode->value = val;
newNode->next =NULL;return newNode;}
#include<stdio.h>#include<stdlib.h>voidinsertAtBeginning(int);voidinsertAtEnd(int);voidinsertBetween(int,int,int);voiddisplay();voidremoveBeginning();voidremoveEnd();voidremovenode(int);structNode{int data;structNode*next;}*head =NULL;intmain(){int opt,value,opt1,val1,val2;while(1){
mainmenu :printf("
MENU
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");scanf("%d",&opt);switch(opt){case1:printf("Enter the value to be insert: ");scanf("%d",&value);while(1){printf("Where you want to insert:1. At Beginning
2. At End
3. Between
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:insertAtBeginning(value);break;case2:insertAtEnd(value);break;case3:printf("Enter the two values where you want to insert: ");scanf("%d%d",&val1,&val2);insertBetween(value,val1,val2);break;/* default: printf("
Wrong InputTry again");*/}goto mainmenu;}break;case2:display();break;case3:printf("How do you want to Delete:1. From Beginning
2. From End
3. given_node
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:removeBeginning();break;case2:removeEnd(value);break;case3:printf("Enter the value which you want to delete: ");scanf("%d",&val2);removenode(val2);break;default:printf("
Wrong Input!! Try again!!!
");goto mainmenu;}break;case4:exit(0);default:printf("
Wrong input!!! Try again!!
");}}}voidinsertAtBeginning(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{
newNode->next = head;
head = newNode;}printf("
One node inserted!!!
");}voidinsertAtEnd(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;
newNode->next =NULL;if(head ==NULL)
head = newNode;else{structNode*temp = head;while(temp->next !=NULL)
temp = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidinsertBetween(int value,int loc1,int loc2){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{structNode*temp = head;while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidremoveBeginning(){if(head ==NULL)printf("
List is Empty!!!");else{structNode*temp = head;if(head->next ==NULL){
head =NULL;free(temp);}else{
head = temp->next;free(temp);printf("
One node deleted!!!
");}}}voidremoveEnd(){if(head ==NULL){printf("
List is Empty!!!
");}else{structNode*temp1 = head,*temp2;if(head->next ==NULL)
head =NULL;else{while(temp1->next !=NULL){
temp2 = temp1;
temp1 = temp1->next;}
temp2->next =NULL;}free(temp1);printf("
One node deleted!!!
");}}voidremovenode(int delValue){structNode*temp1 = head,*temp2;while(temp1->data != delValue){if(temp1 -> next ==NULL){printf("
Given node not found in the list!!!");}
temp2 = temp1;
temp1 = temp1 -> next;}
temp2 -> next = temp1 -> next;free(temp1);printf("
One node deleted!!!
");}voiddisplay(){if(head ==NULL){printf("
List is Empty
");}else{structNode*temp = head;printf("
List elements are -
");while(temp->next !=NULL){printf("%d --->",temp->data);
temp = temp->next;}printf("%d --->NULL",temp->data);}}
#include<stdio.h>#include<stdlib.h>voidinsertAtBeginning(int);voidinsertAtEnd(int);voidinsertBetween(int,int,int);voiddisplay();voidremoveBeginning();voidremoveEnd();voidremovenode(int);structNode{int data;structNode*next;}*head =NULL;intmain(){int opt,value,opt1,val1,val2;while(1){
mainmenu :printf("
MENU
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");scanf("%d",&opt);switch(opt){case1:printf("Enter the value to be insert: ");scanf("%d",&value);while(1){printf("Where you want to insert:1. At Beginning
2. At End
3. Between
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:insertAtBeginning(value);break;case2:insertAtEnd(value);break;case3:printf("Enter the two values where you want to insert: ");scanf("%d%d",&val1,&val2);insertBetween(value,val1,val2);break;/* default: printf("
Wrong InputTry again");*/}goto mainmenu;}break;case2:display();break;case3:printf("How do you want to Delete:1. From Beginning
2. From End
3. given_node
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:removeBeginning();break;case2:removeEnd(value);break;case3:printf("Enter the value which you want to delete: ");scanf("%d",&val2);removenode(val2);break;default:printf("
Wrong Input!! Try again!!!
");goto mainmenu;}break;case4:exit(0);default:printf("
Wrong input!!! Try again!!
");}}}voidinsertAtBeginning(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{
newNode->next = head;
head = newNode;}printf("
One node inserted!!!
");}voidinsertAtEnd(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;
newNode->next =NULL;if(head ==NULL)
head = newNode;else{structNode*temp = head;while(temp->next !=NULL)
temp = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidinsertBetween(int value,int loc1,int loc2){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{structNode*temp = head;while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidremoveBeginning(){if(head ==NULL)printf("
List is Empty!!!");else{structNode*temp = head;if(head->next ==NULL){
head =NULL;free(temp);}else{
head = temp->next;free(temp);printf("
One node deleted!!!
");}}}voidremoveEnd(){if(head ==NULL){printf("
List is Empty!!!
");}else{structNode*temp1 = head,*temp2;if(head->next ==NULL)
head =NULL;else{while(temp1->next !=NULL){
temp2 = temp1;
temp1 = temp1->next;}
temp2->next =NULL;}free(temp1);printf("
One node deleted!!!
");}}voidremovenode(int delValue){structNode*temp1 = head,*temp2;while(temp1->data != delValue){if(temp1 -> next ==NULL){printf("
Given node not found in the list!!!");}
temp2 = temp1;
temp1 = temp1 -> next;}
temp2 -> next = temp1 -> next;free(temp1);printf("
One node deleted!!!
");}voiddisplay(){if(head ==NULL){printf("
List is Empty
");}else{structNode*temp = head;printf("
List elements are -
");while(temp->next !=NULL){printf("%d --->",temp->data);
temp = temp->next;}printf("%d --->NULL",temp->data);}}
#include<stdio.h>#include<stdlib.h>typedefstructNode{void* data;structNode*next;} Node;//push to the front of the liststructNode*push(structNode* head,void* data){structNode* new_node =(structNode*)malloc(sizeof(structNode));
new_node->data = data;
new_node->next = head;return new_node;}intmain(){
Node* list =NULL;//remember to set the list after push
list =push(list,"!");
list =push(list,"World ");
list =push(list,"Hello ");//print out the elements
Node* crnt = list;while(crnt !=NULL){printf("%s", crnt->data);
crnt = crnt->next;}return0;}
#include<stdio.h>#include<stdlib.h>voidinsertAtBeginning(int);voidinsertAtEnd(int);voidinsertBetween(int,int,int);voiddisplay();voidremoveBeginning();voidremoveEnd();voidremovenode(int);structNode{int data;structNode*next;}*head =NULL;intmain(){int opt,value,opt1,val1,val2;while(1){
mainmenu :printf("
MENU
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");scanf("%d",&opt);switch(opt){case1:printf("Enter the value to be insert: ");scanf("%d",&value);while(1){printf("Where you want to insert:1. At Beginning
2. At End
3. Between
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:insertAtBeginning(value);break;case2:insertAtEnd(value);break;case3:printf("Enter the two values where you want to insert: ");scanf("%d%d",&val1,&val2);insertBetween(value,val1,val2);break;/* default: printf("
Wrong InputTry again");*/}goto mainmenu;}break;case2:display();break;case3:printf("How do you want to Delete:1. From Beginning
2. From End
3. given_node
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:removeBeginning();break;case2:removeEnd(value);break;case3:printf("Enter the value which you want to delete: ");scanf("%d",&val2);removenode(val2);break;default:printf("
Wrong Input!! Try again!!!
");goto mainmenu;}break;case4:exit(0);default:printf("
Wrong input!!! Try again!!
");}}}voidinsertAtBeginning(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{
newNode->next = head;
head = newNode;}printf("
One node inserted!!!
");}voidinsertAtEnd(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;
newNode->next =NULL;if(head ==NULL)
head = newNode;else{structNode*temp = head;while(temp->next !=NULL)
temp = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidinsertBetween(int value,int loc1,int loc2){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{structNode*temp = head;while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidremoveBeginning(){if(head ==NULL)printf("
List is Empty!!!");else{structNode*temp = head;if(head->next ==NULL){
head =NULL;free(temp);}else{
head = temp->next;free(temp);printf("
One node deleted!!!
");}}}voidremoveEnd(){if(head ==NULL){printf("
List is Empty!!!
");}else{structNode*temp1 = head,*temp2;if(head->next ==NULL)
head =NULL;else{while(temp1->next !=NULL){
temp2 = temp1;
temp1 = temp1->next;}
temp2->next =NULL;}free(temp1);printf("
One node deleted!!!
");}}voidremovenode(int delValue){structNode*temp1 = head,*temp2;while(temp1->data != delValue){if(temp1 -> next ==NULL){printf("
Given node not found in the list!!!");}
temp2 = temp1;
temp1 = temp1 -> next;}
temp2 -> next = temp1 -> next;free(temp1);printf("
One node deleted!!!
");}voiddisplay(){if(head ==NULL){printf("
List is Empty
");}else{structNode*temp = head;printf("
List elements are -
");while(temp->next !=NULL){printf("%d --->",temp->data);
temp = temp->next;}printf("%d --->NULL",temp->data);}}
// https://github.com/davidemesso/LinkedListC for the full// implementation of every basic functiontypedefstructNode{// Void pointer content of this node (to provide multityping).void* data;// Points to the next Node in the list. structNode* next;} Node;
Node *llNewList(void* data, Node* next){
Node* result =(Node*)malloc(sizeof(Node));
result->data = data;
result->next = next;return result;}
#include<stdio.h>#include<stdlib.h>voidinsertAtBeginning(int);voidinsertAtEnd(int);voidinsertBetween(int,int,int);voiddisplay();voidremoveBeginning();voidremoveEnd();voidremovenode(int);structNode{int data;structNode*next;}*head =NULL;intmain(){int opt,value,opt1,val1,val2;while(1){
mainmenu :printf("
MENU
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");scanf("%d",&opt);switch(opt){case1:printf("Enter the value to be insert: ");scanf("%d",&value);while(1){printf("Where you want to insert:1. At Beginning
2. At End
3. Between
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:insertAtBeginning(value);break;case2:insertAtEnd(value);break;case3:printf("Enter the two values where you want to insert: ");scanf("%d%d",&val1,&val2);insertBetween(value,val1,val2);break;/* default: printf("
Wrong InputTry again");*/}goto mainmenu;}break;case2:display();break;case3:printf("How do you want to Delete:1. From Beginning
2. From End
3. given_node
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:removeBeginning();break;case2:removeEnd(value);break;case3:printf("Enter the value which you want to delete: ");scanf("%d",&val2);removenode(val2);break;default:printf("
Wrong Input!! Try again!!!
");goto mainmenu;}break;case4:exit(0);default:printf("
Wrong input!!! Try again!!
");}}}voidinsertAtBeginning(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{
newNode->next = head;
head = newNode;}printf("
One node inserted!!!
");}voidinsertAtEnd(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;
newNode->next =NULL;if(head ==NULL)
head = newNode;else{structNode*temp = head;while(temp->next !=NULL)
temp = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidinsertBetween(int value,int loc1,int loc2){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{structNode*temp = head;while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidremoveBeginning(){if(head ==NULL)printf("
List is Empty!!!");else{structNode*temp = head;if(head->next ==NULL){
head =NULL;free(temp);}else{
head = temp->next;free(temp);printf("
One node deleted!!!
");}}}voidremoveEnd(){if(head ==NULL){printf("
List is Empty!!!
");}else{structNode*temp1 = head,*temp2;if(head->next ==NULL)
head =NULL;else{while(temp1->next !=NULL){
temp2 = temp1;
temp1 = temp1->next;}
temp2->next =NULL;}free(temp1);printf("
One node deleted!!!
");}}voidremovenode(int delValue){structNode*temp1 = head,*temp2;while(temp1->data != delValue){if(temp1 -> next ==NULL){printf("
Given node not found in the list!!!");}
temp2 = temp1;
temp1 = temp1 -> next;}
temp2 -> next = temp1 -> next;free(temp1);printf("
One node deleted!!!
");}voiddisplay(){if(head ==NULL){printf("
List is Empty
");}else{structNode*temp = head;printf("
List elements are -
");while(temp->next !=NULL){printf("%d --->",temp->data);
temp = temp->next;}printf("%d --->NULL",temp->data);}}
#include<stdio.h>#include<string.h>#include<stdlib.h>#defineTRUE1#defineFALSE0typedefint BOOL;/* a set of routines to illustrate insertion into, and deletion from, a linked
list using `traditional' single-level pointer techniques. The routines for
deleting a list element, and for inserting at the front of a list are
adapted from Kernighan and Pike's "The Practice of Programming" pp.46 et
seq. (Addison-Wesley 1999). The elements of the list are of type THING
where each THING is a structure in which the `item' field holds a
string and the `next' field holds a pointer to the next THING on the list.
The techniques for adding a THING before the start of a list, or after the
end of a list, are two special cases that are straightforward enough.
However if the list elements are to be kept ordered alphabetically (say)
the insertion of a new element needs great care to ensure that the
NULL end-of-list marker does not get dereferenced.
In summary the routines should be robust against:
1) inserting/deleting to/from an empty list
2) inserting/deleting to/from a single-element list
3) inserting/deleting at the end of a list
4) inserting/deleting at the front of a list - with updating of the
pointer to the list head
The general routine `addmiddle', supplied below, is general purpose but
it calls on 'addfront' and 'addend' in specific special cases. Note
carefully that it does allow for duplicate list elements.
Exercise: modify `addmiddle so that this duplication is NOT allowed.
*/typedefstruct_thing{char*item;struct_thing*next;} THING;
THING *start =NULL;// create new list element of type THING from the supplied text string
THING *newelement(char*text){
THING *newp;
newp =(THING *)malloc(sizeof(THING));
newp->item =(char*)malloc(strlen(text)+1);strcpy(newp->item, text);
newp -> next =NULL;return newp;}// delelement: remove from list the first instance of an element // containing a given text string// NOTE!! delete requests for elements not in the list are silently ignored
THING *delelement(THING *head,char*text){
THING *p,*prev;
prev =NULL;for(p = head; p !=NULL; p = p -> next){if(strcmp(text, p -> item)==0){if(prev ==NULL)
head = p-> next;else
prev -> next = p -> next;free(p -> item);//free off the the string fieldfree(p);// remove rest of THINGreturn head;}
prev = p;}}/* addfront: add new THING to front of list *//* example usage: start = (addfront(start, newelement("burgers")); */
THING *addfront(THING *head, THING *newp){
newp -> next = head;return newp;}/* addend: add new THING to the end of a list *//* usage example: start = (addend(start, newelement("wine")); */
THING *addend(THING *head, THING *newp){
THING *p2;if(head ==NULL)return newp;// now find the end of listfor(p2 = head; p2 -> next !=NULL; p2 = p2 -> next);
p2 -> next = newp;return head;}// add element into middle of a list of THINGs based on alphabetical order // of the `item' strings within the THING structures
THING *addmiddle(THING *head, THING *newp){
BOOL found = FALSE;
THING *p1,*p2;if(head ==NULL){//special case// printf("initial list was NULL
");
head =addfront(head, newp);return head;}// Main loop. Use p2 to remember previous p1
p2 = p1 = head ;while(!found){if(found =strcmp(p1 -> item, newp -> item)>=1){if(p1 == head){// printf("adding at head
");
head =addfront(head, newp);return(head);}else{//general case - insert the item// printf("General case entered
");
p2 -> next = newp;;
newp -> next = p1;return head;}}// match not found before end of list so insert at end if(p1 -> next ==NULL){
head =addend(head, newp);return(head);}// go round while loop one more time
p2 = p1; p1 = p1 -> next;}// end of while }voidprintlist(THING **head)// this routine uses pointer-to-pointer techniques :-){
THING **tracer = head;while((*tracer)!=NULL){printf("%s
",(*tracer)->item);
tracer =&(*tracer)->next;}}intmain(int argc,char**argv){
start =addmiddle(start,newelement("chips"));
start =addmiddle(start,newelement("wine"));
start =addmiddle(start,newelement("beer"));
start =addmiddle(start,newelement("pizza"));
start =addmiddle(start,newelement("zucchini"));
start =addmiddle(start,newelement("burgers"));
start =addmiddle(start,newelement("burgers"));
start =addmiddle(start,newelement("slaw"));printf("
INITIAL LIST
");printlist(&start);delelement(start,"pizza");delelement(start,"zucchini");delelement(start,"burgers");printf("
ALTERED LIST
");printlist(&start);}
#include<stdio.h>#include<stdlib.h>voidinsertAtBeginning(int);voidinsertAtEnd(int);voidinsertBetween(int,int,int);voiddisplay();voidremoveBeginning();voidremoveEnd();voidremovenode(int);structNode{int data;structNode*next;}*head =NULL;intmain(){int opt,value,opt1,val1,val2;while(1){
mainmenu :printf("
MENU
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");scanf("%d",&opt);switch(opt){case1:printf("Enter the value to be insert: ");scanf("%d",&value);while(1){printf("Where you want to insert:1. At Beginning
2. At End
3. Between
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:insertAtBeginning(value);break;case2:insertAtEnd(value);break;case3:printf("Enter the two values where you want to insert: ");scanf("%d%d",&val1,&val2);insertBetween(value,val1,val2);break;/* default: printf("
Wrong InputTry again");*/}goto mainmenu;}break;case2:display();break;case3:printf("How do you want to Delete:1. From Beginning
2. From End
3. given_node
Enter your choice: ");scanf("%d",&opt1);switch(opt1){case1:removeBeginning();break;case2:removeEnd(value);break;case3:printf("Enter the value which you want to delete: ");scanf("%d",&val2);removenode(val2);break;default:printf("
Wrong Input!! Try again!!!
");goto mainmenu;}break;case4:exit(0);default:printf("
Wrong input!!! Try again!!
");}}}voidinsertAtBeginning(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{
newNode->next = head;
head = newNode;}printf("
One node inserted!!!
");}voidinsertAtEnd(int value){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;
newNode->next =NULL;if(head ==NULL)
head = newNode;else{structNode*temp = head;while(temp->next !=NULL)
temp = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidinsertBetween(int value,int loc1,int loc2){structNode*newNode;
newNode =(structNode*)malloc(sizeof(structNode));
newNode->data = value;if(head ==NULL){
newNode->next =NULL;
head = newNode;}else{structNode*temp = head;while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;}printf("
One node inserted!!!
");}voidremoveBeginning(){if(head ==NULL)printf("
List is Empty!!!");else{structNode*temp = head;if(head->next ==NULL){
head =NULL;free(temp);}else{
head = temp->next;free(temp);printf("
One node deleted!!!
");}}}voidremoveEnd(){if(head ==NULL){printf("
List is Empty!!!
");}else{structNode*temp1 = head,*temp2;if(head->next ==NULL)
head =NULL;else{while(temp1->next !=NULL){
temp2 = temp1;
temp1 = temp1->next;}
temp2->next =NULL;}free(temp1);printf("
One node deleted!!!
");}}voidremovenode(int delValue){structNode*temp1 = head,*temp2;while(temp1->data != delValue){if(temp1 -> next ==NULL){printf("
Given node not found in the list!!!");}
temp2 = temp1;
temp1 = temp1 -> next;}
temp2 -> next = temp1 -> next;free(temp1);printf("
One node deleted!!!
");}voiddisplay(){if(head ==NULL){printf("
List is Empty
");}else{structNode*temp = head;printf("
List elements are -
");while(temp->next !=NULL){printf("%d --->",temp->data);
temp = temp->next;}printf("%d --->NULL",temp->data);}}
#include<stdio.h>#include<stdlib.h>intmain(){//node structurestructnode{int data;structnode*next;};//declaring nodesstructnode*head,*middle,*last;//allocating memory for each node
head =malloc(sizeof(structnode));
middle =malloc(sizeof(structnode));
last =malloc(sizeof(structnode));//assigning values to each node
head->data =10;
middle->data =20;
last->data =30;//connecting each nodes. head->middle->last
head->next = middle;
middle->next = last;
last->next =NULL;//temp is a reference for head pointer.structnode*temp = head;//till the node becomes null, printing each nodes datawhile(temp !=NULL){printf("%d->",temp->data);
temp = temp->next;}printf("NULL");return0;}