singly linked list programe.pdf

Upload: rizwan-hameed

Post on 04-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 singly linked list programe.pdf

    1/6

    view plainprint?

    1. #include 2. #include 3.4. //Structure containing a Data part & a5. //Link part to the next node in the List6.7. struct Node8. {9. int Data;10. struct Node *Next;11. }*Head;12.13. // Counting number of elements in the List14.15. int length()16. {17. struct Node *cur_ptr;18. int count=0;19.20. cur_ptr=Head;21.22. while(cur_ptr != NULL)23. {24. cur_ptr=cur_ptr->Next;25. count++;26. }27. return(count);28. }29.30. // Deleting a node from List depending upon the data in the node.31.32. int delNodeData(int num)33. {34. struct Node *prev_ptr, *cur_ptr;35.36. cur_ptr=Head;37.38. while(cur_ptr != NULL)39. {40. if(cur_ptr->Data == num)41. {42. if(cur_ptr==Head)43. {44. Head=cur_ptr->Next;45. free(cur_ptr);46. return 0;47. }48. else49. {50. prev_ptr->Next=cur_ptr->Next;51. free(cur_ptr);52. return 0;53. }54. }55. else56. {57. prev_ptr=cur_ptr;58. cur_ptr=cur_ptr->Next;59. }60. }61.62. printf("\nElement %d is not found in the List", num);63. return 1;64. }65.

  • 7/29/2019 singly linked list programe.pdf

    2/6

    66. // Deleting a node from List depending upon the location in the list.67.68. int delNodeLoc(int loc)69. {70. struct Node *prev_ptr, *cur_ptr;71. int i;72.73. cur_ptr=Head;74.75. if(loc > (length()) || loc Next;85. free(cur_ptr);86. return 0;87. }88. else89. {90. for(i=1;iNext;94. }95.96. prev_ptr->Next=cur_ptr->Next;97. free(cur_ptr);98. }99. }100. return 1;101.}102.103.//Adding a Node at the end of the list104.105.void addEnd(int num)106.{107. struct Node *temp1, *temp2;108.109. temp1=(struct Node *)malloc(sizeof(struct Node));110. temp1->Data=num;111.112. // Copying the Head location into another node.113. temp2=Head;114.115. if(Head == NULL)116. {117. // If List is empty we create First Node.118. Head=temp1;119. Head->Next=NULL;120. }121. else122. {123. // Traverse down to end of the list.124. while(temp2->Next != NULL)125. temp2=temp2->Next;126.127. // Append at the end of the list.128. temp1->Next=NULL;129. temp2->Next=temp1;130. }131.}132.133.// Adding a Node at the Beginning of the List

  • 7/29/2019 singly linked list programe.pdf

    3/6

    134.135.void addBeg(int num)136.{137. struct Node *temp;138.139. temp=(struct Node *)malloc(sizeof(struct Node));140. temp->Data = num;141.142. if (Head == NULL)143. {144. //List is Empty145. Head=temp;146. Head->Next=NULL;147. }148. else149. {150. temp->Next=Head;151. Head=temp;152. }153.}154.155.// Adding a new Node at specified position156.157.void addAt(int num, int loc)158.{159. int i;160. struct Node *temp, *prev_ptr, *cur_ptr;161.162. cur_ptr=Head;163.164. if(loc > (length()+1) || loc Data=num;185.186. prev_ptr->Next=temp;187. temp->Next=cur_ptr;188. }189. }190.}191.192.// Displaying list contents193.194.void display()195.{196. struct Node *cur_ptr;197.198. cur_ptr=Head;199.200. if(cur_ptr==NULL)201. {

  • 7/29/2019 singly linked list programe.pdf

    4/6

    202. printf("\nList is Empty");203. }204. else205. {206. printf("\nElements in the List: ");207. //traverse the entire linked list208. while(cur_ptr!=NULL)209. {210. printf(" -> %d ",cur_ptr->Data);211. cur_ptr=cur_ptr->Next;212. }213. printf("\n");214. }215.}216.217.//Reversesing a Linked List218.219.void reverse()220.{221. struct Node *prev_ptr, *cur_ptr, *temp;222.223. cur_ptr=Head;224. prev_ptr=NULL;225.226. while(cur_ptr != NULL)227. {228. temp=prev_ptr;229. prev_ptr=cur_ptr;230.231. cur_ptr=cur_ptr->Next;232. prev_ptr->Next=temp;233. }234.235. Head=prev_ptr;236.}237.238.239.int main(int argc, char *argv[])240.{241. int i=0;242.243. //Set HEAD as NULL244. Head=NULL;245.246. while(1)247. {248. printf("\n####################################################\n" );249. printf("MAIN MENU\n");250. printf("####################################################\n" );251. printf(" \nInsert a number \n1. At the Beginning");252. printf(" \n2. At the End");253. printf(" \n3. At a Particular Location in the List");254. printf(" \n\n4. Print the Elements in the List");255. printf(" \n5. Print number of elements in the List");256. printf(" \n6. Reverse the linked List");257. printf(" \n\nDelete a Node in the List");258. printf(" \n7. Delete a node based on Value");259. printf(" \n8. Delete a node based on Location\n");260. printf(" \n\n9. Exit\n");261. printf(" \nChoose Option: ");262. scanf("%d",&i);263.

  • 7/29/2019 singly linked list programe.pdf

    5/6

    264. switch(i)265. {266. case 1:267. {268. int num;269. printf(" \nEnter a Number to insert in the List: ");270. scanf("%d",&num);271. addBeg(num);272. display();273. break;274. }275.276. case 2:277. {278. int num;279. printf(" \nEnter the Number to insert: ");280. scanf("%d",&num);281. addEnd(num);282. display();283. break;284. }285.286. case 3:287. {288. int num, loc;289. printf("\nEnter the Number to insert: ");290. scanf("%d",&num);291. printf("\nEnter the location Number in List at which \292. the Number is inserted: ");293. scanf("%d",&loc);294. addAt(num,loc);295. display();296. break;297. }298.299. case 4:300. {301. display();302. break;303. }304.305. case 5:306. {307. display();308. printf(" \nTotal number of nodes in the List: %d",length());309. break;310. }311.312. case 6:313. {314. reverse();315. display();316. break;317. }318.319. case 7:320. {321. int num;322. printf(" \nEnter the number to be deleted from List: ");323. scanf("%d",&num);324. delNodeData(num);325. display();326. break;327. }328.329. case 8:330. {331. int num;

  • 7/29/2019 singly linked list programe.pdf

    6/6

    332. printf(" \nEnter the location of the node to \333. be deleted from List: ");334. scanf("%d",&num);335. delNodeLoc(num);336. display();337. break;338. }339.340. case 9:341. {342. struct Node *temp;343.344. while(Head!=NULL)345. {346. temp = Head->Next;347. free(Head);348. Head=temp;349. }350. exit(0);351. }352.353. default:354. {355. printf("\nWrong Option choosen");356. }357. }/* end if switch */358. }/* end of while */359.}/* end of main */