Full width home advertisement

HTML

Tech News

Ad

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node{
  5. int data;
  6. struct node *next;
  7. };
  8.  
  9. struct node *temp,*tail=0;
  10. struct node *head=0;
  11.  
  12. //Function Declaractions
  13. void CreateCll(); //create Circular Link list
  14. void DisplayCll(); //Dislpay circular Link List
  15. void InsAtBeg(); //Insert At Begning
  16. void InsAtPos(); //Insert At Given position
  17. void InsAfterPos(); //Insert After Given Position
  18. void InsAtEnd(); //Insert At End
  19. void DelAtBeg(); //Delete At Begning
  20. void DelAtPos(); //Delete at Given position
  21. void DelAfterPos(); //Delete After Given Position
  22. void DelAtEnd(); //Deleta At end
  23. void GetLen(); //Gets The Length Of Circular Link list
  24. void ReverseCll(); //Reverse The circular Link list
  25.  
  26. int main(){
  27. int choice;
  28. while(choice!=13){
  29. printf("\n\n***MAIN MENU***\n\n");
  30. printf("1. Create Circular Link List\n");
  31. printf("2. Insert At Begning\n");
  32. printf("3. Insert at Given Position\n");
  33. printf("4. Insert After Given Position\n");
  34. printf("5. Insert At End\n");
  35. printf("6. Delete At Begning\n");
  36. printf("7. Delete At Given Position\n");
  37. printf("8. Delete After Given Position\n");
  38. printf("9. Delete At End\n");
  39. printf("10. Find The Length Of The List\n");
  40. printf("11. Reverse The List\n");
  41. printf("12. Display Link List\n");
  42. printf("13. Exit\n");
  43. printf("\nEnter choice ");
  44. scanf("%d",&choice);
  45. switch(choice){
  46. case 1:
  47. CreateCll();
  48. break;
  49. case 2:
  50. InsAtBeg();
  51. break;
  52. case 3:
  53. InsAtPos();
  54. break;
  55. case 4:
  56. InsAfterPos();
  57. break;
  58. case 5:
  59. InsAtEnd();
  60. break;
  61. case 6:
  62. DelAtBeg();
  63. break;
  64. case 7:
  65. DelAtPos();
  66. break;
  67. case 8:
  68. DelAfterPos();
  69. break;
  70. case 9:
  71. DelAtEnd();
  72. case 10:
  73. GetLen();
  74. break;
  75. case 11:
  76. ReverseCll();
  77. break;
  78. case 12:
  79. DisplayCll();
  80. break;
  81. case 13:
  82. exit(0);
  83. break;
  84. default:
  85. printf("\n Enter Valid Choice\n");
  86. }
  87. }
  88. return 0;
  89. }
  90. /* //Created With Both Head And Tail
  91. void CreateCll(){
  92. struct node *newnode;
  93. int choice=1;
  94. while(choice){
  95. newnode=(struct node*)malloc(sizeof(struct node));
  96. printf("\nEnter Data ");
  97. scanf("%d",&newnode->data);
  98. newnode->next=0;
  99. if(head==0){
  100. head=tail=newnode;
  101. //tail->next=head;
  102. }
  103. else{
  104. tail->next=newnode;
  105. tail=newnode;
  106. //tail->next=head;
  107. }
  108. tail->next=head;
  109. printf("\n0 To Exit 1 To Add");
  110. scanf("%d",&choice);
  111. }
  112. }
  113. */
  114.  
  115. /* This Function Will Create Circular List Only By Tail Pointer */
  116. void CreateCll(){
  117. struct node *newnode;
  118. int choice=1;
  119. while(choice){
  120. newnode=(struct node*)malloc(sizeof(struct node));
  121. printf("\nEnter Data ");
  122. scanf("%d",&newnode->data);
  123. newnode->next=0;
  124. if(tail==0){
  125. tail=newnode;
  126. tail->next=newnode;
  127. }
  128. else{
  129. newnode->next=tail->next;
  130. tail->next=newnode;
  131. tail=newnode;
  132. }
  133. printf("\n0 To Exit 1 To Add ");
  134. scanf("%d",&choice);
  135. }
  136. }
  137.  
  138. void DisplayCll(){
  139. //temp=head;
  140. temp=tail->next; /*Only if List Is Created Only With Tail Pointer */
  141. printf("Displaing Circular Link List\n");
  142. do{
  143. printf("%d ",temp->data);
  144. temp=temp->next;
  145. }while(temp!=tail->next);
  146. }
  147.  
  148.  
  149. //To Insert At Begning
  150. void InsAtBeg(){
  151. struct node *newnode;
  152. if(tail==0) {
  153. printf("\nCreate A List Firt\n");
  154. }
  155. else{
  156. temp=tail->next;
  157. newnode=(struct node*)malloc(sizeof(struct node));
  158. printf("Enter Data ");
  159. scanf("%d",&newnode->data);
  160. newnode->next=temp;
  161. tail->next=newnode;
  162. printf("\nNode Created Successfully");
  163. }
  164. }
  165.  
  166. //To insert At Given Position
  167. void InsAtPos(){
  168. int pos,i;
  169. struct node *newnode;
  170. if(tail==0){
  171. printf("\nCreate A List Firt\n");
  172. }
  173. else{
  174. temp=tail->next;
  175. printf("Enter position ");
  176. scanf("%d",&pos);
  177. if(pos==1){
  178. printf("You've Chosen Wrong Option\n");
  179. printf("if you want to insert at begning then choose '2' \n");
  180. }
  181. else{
  182. newnode=(struct node*)malloc(sizeof(struct node));
  183. printf("Enter Data ");
  184. scanf("%d",&newnode->data);
  185. for(i=1;i<pos-1;i++){
  186. temp=temp->next;
  187. }
  188. newnode->next=temp->next;
  189. temp->next=newnode;
  190. printf("Node Created Successfully\n");
  191. }
  192. }
  193. }
  194.  
  195. //To insert Atfrt Given position
  196. void InsAfterPos(){
  197. int pos,i;
  198. struct node *newnode;
  199. if(tail==0){
  200. printf("\nCreate A List First");
  201. }
  202. else{
  203. printf("Enter Position ");
  204. scanf("%d",&pos);
  205. if(pos==1){
  206. printf("You've Chosen Wrong Option\n");
  207. printf("if you want to insert at begning then choose '2' \n");
  208. }
  209. else{
  210. temp=tail->next;
  211. newnode=(struct node*)malloc(sizeof(struct node));
  212. printf("Enter Data ");
  213. scanf("%d",&newnode->data);
  214. for(i=1;i<pos;i++){
  215. temp=temp->next;
  216. }
  217. newnode->next=temp->next;
  218. temp->next=newnode;
  219. printf("\n Node created successfully\n");
  220. }
  221. }
  222. }
  223.  
  224. //To insert at end
  225. void InsAtEnd(){
  226. struct node *newnode;
  227. if(tail==0){
  228. printf("Create List Firt");
  229. }
  230. else{
  231. newnode=(struct node*)malloc(sizeof(struct node));
  232. printf("Enter Data ");
  233. scanf("%d",&newnode->data);
  234. newnode->next=tail->next;
  235. tail->next=newnode;
  236. tail=newnode;
  237. printf("\nNode Created Successfully\n");
  238. }
  239. }
  240.  
  241. //Delete At begning
  242. void DelAtBeg(){
  243. if(tail==0){
  244. printf("Create List First\n");
  245. }
  246. else{
  247. temp=tail->next;
  248. tail->next=temp->next;
  249. free(temp);
  250. printf("\nNode Deleted Successfully\n");
  251. }
  252. }
  253.  
  254. //Delete At Given position
  255. void DelAtPos(){
  256. int pos,i;
  257. struct node *del;
  258. if(tail==0){
  259. printf("You can't Delete Node Without Creating It\n");
  260. }
  261. else{
  262. printf("Enter Position ");
  263. scanf("%d",&pos);
  264. if(pos==1){
  265. DelAtBeg();
  266. }
  267. else{
  268. temp=tail->next;
  269. for(i=1;i<pos-1;i++){
  270. temp=temp->next;
  271. }
  272. del=temp->next;
  273. temp->next=del->next;
  274. free(del);
  275. printf("Node Deleted Successfully");
  276. }
  277. }
  278. }
  279.  
  280. //Delete after given Position
  281. void DelAfterPos(){
  282. int pos,i;
  283. struct node *del;
  284. if(tail==0){
  285. printf("You can't Create Node Without Creating It\n");
  286. }
  287. else{
  288. printf("Enter Position ");
  289. scanf("%d",&pos);
  290. if(pos==1){
  291. DelAtBeg();
  292. }
  293. else{
  294. temp=tail->next;
  295. for(i=1;i<pos;i++){
  296. temp=temp->next;
  297. }
  298. del=temp->next;
  299. temp->next=del->next;
  300. free(del);
  301. printf("Node Deleted Successfully");
  302. }
  303. }
  304. }
  305.  
  306. //Delete At end
  307. void DelAtEnd(){
  308. struct node *prev;
  309. if(tail==0){
  310. printf("You can't delete Node Without Creating ");
  311. }
  312. else{
  313. temp=tail->next;
  314. while(temp->next!=tail->next){
  315. prev=temp;
  316. temp=temp->next;
  317. }
  318. prev->next=tail->next;
  319. tail=prev;
  320. free(temp);
  321. printf("\n node deleted success fully\n");
  322. }
  323. }
  324.  
  325. //To Get The Length of Circular Link list
  326. void GetLen(){
  327. int count=1;
  328. temp=tail->next;
  329. while(temp->next!=tail->next){
  330. temp=temp->next;
  331. count++;
  332. }
  333. printf("Lenghth Of List Is %d ",count);
  334. }
  335.  
  336. //To Reverse Circular Link List
  337. void ReverseCll(){
  338. struct node *current,*prev,*nextnode;
  339. current=tail->next;
  340. nextnode=current->next;
  341. if(tail==0){
  342. printf("List is Empty");
  343. }
  344. else if(tail->next==tail){
  345. DisplayCll();
  346. }
  347. else{
  348. while(current!=tail){
  349. prev=current;
  350. current=nextnode;
  351. nextnode=current->next;
  352. current->next=prev;
  353. }
  354. nextnode->next=tail;
  355. tail=nextnode;
  356. printf("Reversed Successfully\n");
  357. }
  358. }
  359.  
  360. //This Is All About Singly Circular Link List
  361. //Created By SMIT

No comments:

Post a Comment

Bottom Ad [Post Page]