Singly Linked List Implementation

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
harshchandra harshchandra is offline Offline Nov 21st, 2004, 9:18 am |
0
This program just demonstrate how a node can be added,deleted,searched,counted,and printed using Singly Linked List.
An easy code to understand
Quick reply to this message  
C Syntax
  1. //////////////////////////////////////////////////////////////
  2. ////////// -: Singly Link list :- /////////
  3. ////////////////////////////////////////////////////////////
  4.  
  5. /* reference : Understandng pointer though c - Y karnetkar.
  6. */
  7.  
  8. //////////////////////////////////////////////////////////
  9. ////////// Programmer : Harsh chandra ///////////
  10. ////////////////////////////////////////////////////////
  11.  
  12. # include<stdio.h>
  13. # include<conio.h>
  14. # include<alloc.h>
  15. # include<stdlib.h>
  16. struct node
  17. { int data;
  18. struct node *link;
  19. };
  20. void append(struct node **,int);
  21. void in_begin(struct node **,int);
  22. void del(struct node **,int);
  23. void in_middle(struct node **,int,int);
  24. int count(struct node *);
  25. void display(struct node *);
  26.  
  27. void main()
  28. { struct node *p; /* p can be said as the head or a start ptr */
  29. p=NULL;
  30. /* Printing the menu */
  31. int num,loc;
  32. char choice;
  33. do
  34. { clrscr();
  35. printf("PROGRAM TO IMPLEMENT SINGLY LINKED LIST ");
  36. printf("\n=====================================");
  37. printf("\n\n1.Create \\ Appending The List");
  38. printf("\n2.Insert Node At Begining");
  39. printf("\n3.Insert Node In Middle");
  40. printf("\n4.Deleting a Node");
  41. printf("\n5.Counting The No Of Nodes");
  42. printf("\n6.Displaying the list");
  43. printf("\n7.Exit");
  44. oper:
  45. gotoxy(1,15);printf(" ");
  46. gotoxy(1,11);printf("\n\nEnter ur Choice : ");
  47. choice=getch();
  48. switch(choice)
  49. { case '1':
  50. char ans;
  51. do
  52. { printf("Enter any number : ");
  53. scanf("%d",&num);
  54. append(&p,num);
  55. printf("Enter more (y/n) :");
  56. fflush(stdin);
  57. ans=getchar();
  58. }while(ans !='n');
  59. break;
  60. case '2':
  61. printf("Enter The Data : ");
  62. scanf("%d",&num);
  63. in_begin(&p,num);
  64. break;
  65.  
  66. case '3':
  67. printf("\nEnter The Position :");
  68. scanf("%d",&loc);
  69. printf("\nEnter The Data : ");
  70. scanf("%d",&num);
  71. in_middle(&p,loc,num);
  72. break;
  73.  
  74. case '4':
  75. printf("\nEnter The Data u Want To Delete : ");
  76. scanf("%d",&num);
  77. del(&p,num);
  78. break;
  79.  
  80. case '5':
  81. printf("\nThe No Of Nodes Are %d",count(p));
  82. getch();
  83. break;
  84.  
  85. case '6':
  86. display(p);
  87. getch();
  88. break;
  89.  
  90. case '7':
  91. printf("\n\nQuiting.......");
  92. getch();
  93. exit(0);
  94. break;
  95.  
  96. default:
  97. gotoxy(1,15);printf("Invalid choice.Please Enter Correct Choice");
  98. getch();
  99. goto oper;
  100.  
  101. }
  102.  
  103. }while(choice !=7);
  104.  
  105. }
  106.  
  107. void append(struct node **q,int num)
  108. { struct node *temp,*r;
  109. temp = *q;
  110. if(*q==NULL)
  111. { temp = (struct node *)malloc(sizeof(struct node));
  112. temp->data=num;
  113. temp->link=NULL;
  114. *q=temp;
  115. }
  116. else
  117. { temp = *q;
  118. while(temp->link !=NULL)
  119. { temp=temp->link;
  120. }
  121. r = (struct node *)malloc(sizeof(struct node));
  122. r->data=num;
  123. r->link=NULL;
  124. temp->link=r;
  125. }
  126. }
  127.  
  128. void display(struct node *q)
  129. { if(q==NULL)
  130. { printf("\n\nEmpty Link List.Can't Display The Data");
  131. getch();
  132. goto last;
  133. }
  134. while(q!=NULL)
  135. { printf("\n%d",q->data);
  136. q=q->link;
  137. }
  138. last:
  139. }
  140.  
  141. int count(struct node *q)
  142. { int c=0;
  143. if(q==NULL)
  144. { printf("Empty Link List.\n");
  145. getch();
  146. goto last;
  147. }
  148. while(q!=NULL)
  149. { c++;
  150. q=q->link;
  151. }
  152. last:
  153. return c;
  154.  
  155. }
  156.  
  157. void in_begin(struct node **q,int num)
  158. { struct node *temp;
  159. if(*q==NULL)
  160. { printf("Link List Is Empty.Can't Insert.");
  161. getch();
  162. goto last;
  163. }
  164. else
  165. { temp=(struct node *)malloc(sizeof(struct node));
  166. temp->data=num;
  167. temp->link=*q;
  168. *q=temp; /* pointing to the first node */
  169. }
  170. last:
  171. getch();
  172. }
  173.  
  174. void in_middle(struct node **q,int loc,int num)
  175. { struct node *temp,*n;
  176. int c=1,flag=0;
  177. temp=*q;
  178. if(*q==NULL)
  179. { printf("\n\nLink List Is Empty.Can't Insert.");
  180. getch();
  181. goto last;
  182. }
  183. else
  184. while(temp!=NULL)
  185. { if(c==loc)
  186. { n = (struct node *)malloc(sizeof(struct node));
  187. n->data=num;
  188. n->link=temp->link;
  189. temp->link=n;
  190. flag=1;
  191. }
  192. c++;
  193. temp=temp->link;
  194. }
  195. if(flag==0)
  196. { printf("\n\nNode Specified Doesn't Exist.Cant Enter The Data");
  197. getch();
  198. }
  199. else
  200. { printf("Data Inserted");
  201. getch();
  202. }
  203. last:
  204. getch();
  205. }
  206.  
  207. void del(struct node**q,int num)
  208. { if(*q==NULL)
  209. { printf("\n\nEmpty Linked List.Cant Delete The Data.");
  210. getch();
  211. goto last;
  212. }
  213. else
  214. {
  215. struct node *old,*temp;
  216. int flag=0;
  217. temp=*q;
  218. while(temp!=NULL)
  219. { if(temp->data==num)
  220. { if(temp==*q) /* First Node case */
  221. *q=temp->link; /* shifted the header node */
  222. else
  223. old->link=temp->link;
  224.  
  225. free(temp);
  226. flag=1;
  227. }
  228. else
  229. { old=temp;
  230. temp=temp->link;
  231. }
  232. }
  233. if(flag==0)
  234. printf("\nData Not Found...");
  235. else
  236. printf("\nData Deleted...Tap a key to continue");
  237. getch();
  238. }
  239. last:
  240. getch();
  241. }
  242.  
0
1o0oBhP 1o0oBhP is offline Offline | Jan 8th, 2005
nice1 harshchandra! I have a snippet with a doubly linked list if you are interested!
 
0
1o0oBhP 1o0oBhP is offline Offline | Jan 8th, 2005
and im impressed with the **, you dont see that often
 
0
Asif_NSU Asif_NSU is offline Offline | Feb 8th, 2005
Would have been better if it were a template class.
 
0
Q8iEnG Q8iEnG is offline Offline | Jul 14th, 2008
Awesome thanks a lot
 
0
Lug16 Lug16 is offline Offline | Oct 22nd, 2008
Your code is really awesome, I've personally tested and almost everything works great... Just one thing generates an error...
And I wanna show you how I fixed it...

<code>

if(temp->data==num)

{ if(temp==*q) /* First Node case */

*q=temp->link; /* shifted the header node */

//If I want to delete the first node, the code will blow up and I use the follow lines to fix it
free(temp);
flag = 1;
//Just the same thing that you show after the else
else

old->link=temp->link;
</code>

Thanks...!
 
0
pushpa.gangu pushpa.gangu is offline Offline | Apr 10th, 2009
Hi,

can you send me the snippet for doubly linked list?
 
0
ashishju ashishju is offline Offline | May 24th, 2009
Hi Harsh ..
The code is awesome . thanks for posting it . I tested it on Dev C Compiler .

But I got an error in the delete function .

Since there is no break statement , the program goes in a while loop . I just modified the code as below and it worked :-)


if(temp->data == num)
{

if(temp == *q)
{
*q=temp->link;
free(temp);
flag = 1;
break;
}
else
{
old->link = temp->link;
free(temp);
flag = 1;
break;
}
}
 
0
neo_and22 neo_and22 is offline Offline | Jul 29th, 2009
dudes, i compiled this program in gcc compiler but i gotta error msg while passing &p to append function. error message is "incompatible pointer type","expected to be struct node ** but it is struct node **". please anyone help me out.
 
0
arushi agarwal arushi agarwal is offline Offline | Aug 31st, 2009
please explain the concept of accessing reference pointers, in append function
temp=*q , /what does it mean/
why not we can use, temp = q.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC