Code.GeekInterview.com
 
Code Samples C
 

Single Linked List


Code ResourceAuthor: mano.mithun  

Difficulty Level: Intermediate

Published: 3rd Aug 2010   Read: 4224 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 

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


Next Article: Polynomial Addition using Linked List


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. kaivalya198932313
2. mano.mithun19250
3. deepu0817246
4. meefriend4ever7824
5. chandrikakr7816
6. shashivaishnav6687
7. sadasivathavamani6563
8. venkatakrishnansvpr6437
9. Jimmy Zorald6277
10. sripri5431

Active Coders

Refined Tags