0% found this document useful (0 votes)
45 views8 pages

Sample Code: / Program Implementation of Single Linked List

Single linked list inplementation using different functions. Insert a number at the beginning 2. Insert a number a particular location in list 3. Print the elements in the list 4. Delete a node in the linked list 5. Reverse a linked list 8. Get out of linked list Sample code.

Uploaded by

Indu Sha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

Sample Code: / Program Implementation of Single Linked List

Single linked list inplementation using different functions. Insert a number at the beginning 2. Insert a number a particular location in list 3. Print the elements in the list 4. Delete a node in the linked list 5. Reverse a linked list 8. Get out of linked list Sample code.

Uploaded by

Indu Sha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Single linked list inplementation using different functions

1. Insert a number at the beginning


2. Insert a number at last
3. Insert a number at a particular location in list
4. Print the elements in the list
5. Print the total number of elements in the list
6. Delete a node in the linked list
7. Reverse a linked list
8. Get out of linked list

Sample Code

1. /* PROGRAM IMPLEMENTATION OF SINGLE LINKED LIST */


2.  
3. #include"stdio.h"
4. //#define NULL 0
5. /* STRUCTURE CONTANING A DATA PART AND A LINK PART */
6.  
7. struct node
8. {
9. int data;
10. struct node *next;
11. }*p;
12.  
13. /* P IS A GLOBAL POINTER CONTAINS THE ADRESS OF THE
FIRST NODE IN
14. LIST
15. */
16.  
17. /*THIS FUNCTION DELETES A NODE */
18.  
19. delnode(int num)
20. {
21. struct node *temp, *m;
22. temp=p;
23. while(temp!=NULL)
24. {
25. if(temp->data==num)
26. {
27. if(temp==p)
28. {
29. p=temp->next;
30. free(temp);
31. return;
32. }
33. else
34. {
35. m->next=temp->next;
36. free(temp);
37. return;
38. }
39. }else
40. {
41. m=temp;
42. temp= temp->next;
43. }
44.  
45. }
46. printf("
47. ELEMENT %d NOT FOUND
48. ", num);
49. }/*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED
LIST */
50.  
51. append( int num )
52. {
53. struct node *temp,*r;
54. /* CREATING A NODE AND ASSIGNING A VALUE TO IT */
55.  
56. temp= (struct node *)malloc(sizeof(struct node));
57. temp->data=num;
58. r=(struct node *)p;
59.  
60. if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE
*/
61. {
62. p=temp;
63. p->next =NULL;
64. }
65. else
66. { /* GO TO LAST AND ADD*/
67.  
68. while( r->next != NULL)
69. r=r->next;
70. r->next =temp;
71. r=temp;
72. r->next=NULL;
73. }
74. }/* ADD A NEW NODE AT BEGINNING */
75.  
76. addbeg( int num )
77. {
78. /* CREATING A NODE AND INSERTING VALUE TO IT */
79.  
80. struct node *temp;
81. temp=(struct node *)malloc(sizeof(struct node));
82. temp->data=num;
83.  
84. /* IF LIST IS NULL ADD AT BEGINNING */
85. if ( p== NULL)
86. {
87. p=temp;
88. p->next=NULL;
89. }
90.  
91. else
92. {
93. temp->next=p;
94. p=temp;
95. }
96. }
97.  
98. /* ADD A NEW NODE AFTER A SPECIFIED NO OF NODES */
99.  
100. addafter(int num, int loc)
101. {
102. int i;
103. struct node *temp,*t,*r;
104. r=p; /* here r stores the first location */
105. if(loc > count()+1 || loc <= 0)
106. {
107. printf("
108. insertion is not possible :
109. ");
110. return;
111. }
112. if (loc == 1)/* if list is null then add at beginning
*/
113. {
114. addbeg(num);
115. return;
116. }
117. else
118. {
119. for(i=1;i<loc;i++)
120. {
121. t=r; /* t will be holding previous value */
122. r=r->next;
123. }
124. temp=(struct node *)malloc(sizeof(struct node));
125. temp->data=num;
126. t->next=temp;
127. t=temp;
128. t->next=r;
129. return;
130. }
131. }/* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED
LIST */
132.  
133. display(struct node *r)
134. {
135. r=p;
136. if(r==NULL)
137. {
138. printf("NO ELEMENT IN THE LIST :");
139. return;
140. }
141. /* traverse the entire linked list */
142. while(r!=NULL)
143. {
144. printf(" -> %d ",r->data);
145. r=r->next;
146. }
147. printf("<BR>);
148. }
149. //THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE
LIST
150. count()
151. {
152. struct node *n;
153. int c=0;
154. n=p;
155. while(n!=NULL)
156. {
157. n=n->next;
158. c++;
159. }
160. return(c);
161. }
162. //THIS FUNCTION REVERSES A LINKED LIST
163. reverse(struct node *q)
164. {
165. struct node *m, *n,*l,*s;
166. m=q;
167. n=NULL;
168. while(m!=NULL)
169. {
170. s=n;
171. n=m;
172. m=m->next;
173. n->next=s;
174. }
175. p=n;
176. }
177.  
178.  
179. /* THIS IS THE MAIN PROGRAM */
180.  
181. main()
182. {
183. int i;
184. p=NULL;
185. while(1) /* this is an indefinite loop */
186. {
187. printf("
188. 1.INSERT A NUMBER AT BEGINNING;<BR>);
189. printf("
190. 2.INSERT A NUMBER AT LAST:<BR>);
191. printf("
192. 3.INSERT A NUMBER AT A PARTICULAR LOCATION
INlIST:<BR>);
193. printf("
194. 4.PRINT THE ELEMENTS IN THE LIST :<BR>);
195. printf("
196. 5.PRINT THE NUMBER OF ELEMENTS IN THE LIST <BR>);
197. printf("
198. 6.DELETE A NODE IN THE LINKED LIST:<BR>);
199. printf("
200. 7.REVERSE A LINKED LIST :<BR>);
201. printf("
202. 8.GET OUT OF LINKED LIST (BYEE BYEE):<BR>);
203. printf("
204. PLEASE, ENTER THE NUMBER:");
205.  
206. scanf("%d",&i); /* ENTER A VALUE FOR SWITCH */
207.  
208. switch(i)
209. {
210. case 1:
211. {
212. int num;
213. printf("
214. PLEASE ENTER THE NUMBER :-");
215. scanf("%d",&num);
216. addbeg(num);
217. break;
218. }
219. case 2:
220. {
221. int num;
222. printf("
223. PLEASE ENTER THE NUMBER :-");
224. scanf("%d",&num);
225. append(num);
226. break;
227. }
228.  
229. case 3:
230. {
231. int num, loc,k;
232. printf("
233. PLEASE ENTER THE NUMBER :-");
234. scanf("%d",&num);
235. printf("
236. PLEASE ENTER THE LOCATION NUMBER :-");
237. scanf("%d",&loc);
238. addafter(num,loc);
239. break;
240. } case 4:
241. {
242. struct node *n;
243. printf("
244.  
245. THE ELEMENTS IN THE LIST ARE : <BR>);
246. display(n);
247. break;
248. }
249.  
250. case 5:
251. {
252. struct node *n;
253. display(n);
254. printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE
%d",count());
255. break;
256. } case 6:
257. {
258. int num;
259. printf("
260. PLEASE ENTER A NUMBER FROM THE LIST :");
261. scanf("%d",&num);
262. delnode(num);
263. break;
264. }
265. case 7:
266. {
267. reverse(p);
268. display(p);
269. break;
270. }
271. case 8:
272. {
273. exit();
274. }
275. }/* end if switch */
276. }/* end of while */
277. }/* end of main */

Copyright GeekInterview.com

Next Article: Polynomial Addition using Linked List

Latest Code Samples

 ♦ Basic Memory Management


 ♦ Finding a number is even or odd without using arithmatic operators
 ♦ Brute force solver for Challenger number puzzle.
 ♦ C Programme to Print Without Semi colon
 ♦ Polynomial Addition using Linked List
 ♦ Single Linked List
 ♦ Double Linked List
 ♦ Program to find out an adam number using arrays
 ♦ Dichotomous Search Method
 ♦ Character Formation Using Lines
 

Popular Code Samples

 ♦ Count Numbers in Binary Number


 ♦ Swap 2 variables without using 3rd variable
 ♦ Delete Spaces from File
 ♦ 10 x 10 Mulplication Table
 ♦ Swaping two variables without using 3rd variable
 ♦ String Encryption
 ♦ Check if the input number is a Amstrong number or not
 ♦ Glittering Eyes - Animation Effect
 ♦ C Programme to Print Without Semi colon
 ♦ Program to find out an adam number using arrays
 

Related Code Samples

 » Double Linked List


 » Polynomial Addition using Linked List
 » Customer List With Edit and Delete Options
 » Finding loop in a link list
 

Post Your Comment:


239 /c/single-linked-lis 0 1 0 Unregistered
Memb
ers Please Login

Your Name:*

e-mail ID:(required for notification)*

Image Verification: 

send Submit
 Subscribe    

You might also like