Here you will learn about singly linked list in C.
What is Singly Linked List (SLL)?
The simplest kind of linked list is a singly liked list (SLL) which has one link per node. It has two parts, one part contains data and other contains address of next node. The structure of a node in a SLL is given as in C:
1 2 3 4 5 |
struct node { int data; struct node *next; }; |
Also Read: Circular Linked List in C
The program is given below that will perform insertion, deletion and display a singly linked list.
Singly Linked List in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
#include<stdio.h> #include<conio.h> #include<process.h> struct node { int data; struct node *next; }*start=NULL,*q,*t; int main() { int ch; void insert_beg(); void insert_end(); int insert_pos(); void display(); void delete_beg(); void delete_end(); int delete_pos(); while(1) { printf("\n\n---- Singly Linked List(SLL) Menu ----"); printf("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n"); printf("Enter your choice(1-4):"); scanf("%d",&ch); switch(ch) { case 1: printf("\n---- Insert Menu ----"); printf("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\n4.Exit"); printf("\n\nEnter your choice(1-4):"); scanf("%d",&ch); switch(ch) { case 1: insert_beg(); break; case 2: insert_end(); break; case 3: insert_pos(); break; case 4: exit(0); default: printf("Wrong Choice!!"); } break; case 2: display(); break; case 3: printf("\n---- Delete Menu ----"); printf("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified position\n4.Exit"); printf("\n\nEnter your choice(1-4):"); scanf("%d",&ch); switch(ch) { case 1: delete_beg(); break; case 2: delete_end(); break; case 3: delete_pos(); break; case 4: exit(0); default: printf("Wrong Choice!!"); } break; case 4: exit(0); default: printf("Wrong Choice!!"); } } return 0; } void insert_beg() { int num; t=(struct node*)malloc(sizeof(struct node)); printf("Enter data:"); scanf("%d",&num); t->data=num; if(start==NULL) //If list is empty { t->next=NULL; start=t; } else { t->next=start; start=t; } } void insert_end() { int num; t=(struct node*)malloc(sizeof(struct node)); printf("Enter data:"); scanf("%d",&num); t->data=num; t->next=NULL; if(start==NULL) //If list is empty { start=t; } else { q=start; while(q->next!=NULL) q=q->next; q->next=t; } } int insert_pos() { int pos,i,num; if(start==NULL) { printf("List is empty!!"); return 0; } t=(struct node*)malloc(sizeof(struct node)); printf("Enter data:"); scanf("%d",&num); printf("Enter position to insert:"); scanf("%d",&pos); t->data=num; q=start; for(i=1;i<pos-1;i++) { if(q->next==NULL) { printf("There are less elements!!"); return 0; } q=q->next; } t->next=q->next; q->next=t; return 0; } void display() { if(start==NULL) { printf("List is empty!!"); } else { q=start; printf("The linked list is:\n"); while(q!=NULL) { printf("%d->",q->data); q=q->next; } } } void delete_beg() { if(start==NULL) { printf("The list is empty!!"); } else { q=start; start=start->next; printf("Deleted element is %d",q->data); free(q); } } void delete_end() { if(start==NULL) { printf("The list is empty!!"); } else { q=start; while(q->next->next!=NULL) q=q->next; t=q->next; q->next=NULL; printf("Deleted element is %d",t->data); free(t); } } int delete_pos() { int pos,i; if(start==NULL) { printf("List is empty!!"); return 0; } printf("Enter position to delete:"); scanf("%d",&pos); q=start; for(i=1;i<pos-1;i++) { if(q->next==NULL) { printf("There are less elements!!"); return 0; } q=q->next; } t=q->next; q->next=t->next; printf("Deleted element is %d",t->data); free(t); return 0; } |
Output
—- Singly Linked List(SLL) Menu —-
1.Insert
2.Display
3.Delete
4.ExitEnter your choice(1-4):1—- Insert Menu —-
1.Insert at beginning
2.Insert at end
3.Insert at specified position
4.ExitEnter your choice(1-4):1
Enter data:4—- Singly Linked List(SLL) Menu —-
1.Insert
2.Display
3.Delete
4.ExitEnter your choice(1-4):2
The linked list is:
4->
1.Insert
2.Display
3.Delete
4.ExitEnter your choice(1-4):1—- Insert Menu —-
1.Insert at beginning
2.Insert at end
3.Insert at specified position
4.ExitEnter your choice(1-4):1
Enter data:4—- Singly Linked List(SLL) Menu —-
1.Insert
2.Display
3.Delete
4.ExitEnter your choice(1-4):2
The linked list is:
4->
—- Singly Linked List(SLL) Menu —-
1.Insert
2.Display
3.Delete
4.Exit
Enter your choice(1-4):4
thanks tho I have not yet tried it
Great tutorial. Can you explain what is Doubly linked list and how to add elements in it?
I will soon write about it. Thanks for your nice comment.
whoa !!.. awesome program but it's countering a problem while displaying the list it says list is empty though i've inserted numbers in the beginning and end both then i inserted at desired position n then chose option 4 i.e display it saying that list is empty and then it says press any key to continue i've done accordingly but it repeatedly says press any key to cont. … agn n agn so problem is in d code of display please can ya wrk on it ??…
not getting correct o/p
What problem you are getting, tell me the inputs that you have entered?
Ok
I like it
very useful .thanks a lot man
not getting correct o/p
can i have the link of singly & doubly linkd list in c++??
Neeraj!!
Can you make a linked list that has search options ? Thanks 🙂
In the for loop for(i=1;i<pos-1;pos++)
Why are we incrementing the pos? Shouldn't we be incrementing i?
It was a little mistake, now I have corrected it. Thanks for mentioning.
Explanation needed for
Delete at any position
where is head & next?
thanks a lot …..it’s very usefull….
For what t and q is? Their specification?
the program is not working …so many errors are there . please do it properly before putting it on website. you just wasted my time.
Delete end will not work for linked list with single element.
q->next->next can generate errors if q->next is NULL.
You can change by using an additional pointer.
Thanks a lot bro!!!!!!!!
If you try to delete the first element in with the list containing only one element, it gives an error. Please fix it.
Write a program driven program to implement following operations on the single linked list .
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(C) Insert a node at the given position in the link list.