linkedlist_demo.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct astack {
  4. int top;
  5. unsigned size;
  6. int* array;
  7. };
  8. struct queue{
  9. int front, rear, size;
  10. unsigned actualSize;
  11. int* arr;
  12. };
  13. void enqueue(struct queue * que, int item); // 在队列尾部插入元素
  14. int dequeue(struct queue * que); // 从队列头部删除一个元素
  15. int front(struct queue * que); // 获取队列的第一个元素(头部)
  16. int rear(struct queue * que); // 获取队列的最后一个元素(尾部)
  17. // linked list
  18. struct node{
  19. int data;
  20. struct node *next;
  21. };
  22. struct node * insert_end(struct node *p, int element);
  23. struct node * insert_begin(struct node *p, int element);
  24. void delete_begin(struct node *p);
  25. void delete_end(struct node *p);
  26. void l_delete(struct node *p, int element);
  27. void display(struct node *p); // 显示所有元素
  28. int main()
  29. {
  30. int val,n;
  31. char c;
  32. struct node *p;
  33. p = NULL;
  34. do{
  35. printf("\n************************* MENU ************************");
  36. printf("\n1.INSERT AT END");
  37. printf("\n2.INSERT AT BEG");
  38. printf("\n3.DELETE A PARTICULAR ELE");
  39. printf("\n4.DELETE FROM BEG");
  40. printf("\n5.DELETE FROM END");
  41. printf("\n6.DISPLAY");
  42. printf("\n7.EXIT");
  43. printf("\nenter ur choice: ");
  44. scanf("%d",&n);
  45. switch(n){
  46. case 1: printf("\nenter the value: ");
  47. scanf("%d",&val);
  48. p = insert_end(p, val);
  49. break;
  50. case 2: printf("\nenter the value: ");
  51. scanf("%d",&val);
  52. p = insert_begin(p, val);
  53. break;
  54. case 3: printf("\nenter the value: ");
  55. scanf("%d",&val);
  56. l_delete(p, val);
  57. break;
  58. case 4:
  59. delete_begin(p);
  60. break;
  61. case 5:
  62. delete_end(p);
  63. break;
  64. case 6:
  65. display(p);
  66. break;
  67. case 7:
  68. exit(0);
  69. break;
  70. default:
  71. printf("\n Wrong Choice!");
  72. break;
  73. }
  74. printf("\n do u want to cont(y/n)... ");
  75. fflush(stdin);
  76. c = getchar();
  77. }while('y' == c || 'Y' == c || '\n' == c);
  78. }
  79. struct node * insert_end(struct node *p1, int ele)
  80. {
  81. struct node *tmp = p1; // 指向节点的指针
  82. struct node *tmp1 = (struct node*)malloc(sizeof(struct node));
  83. tmp1->data = ele; // 为创建的新节点 tmp1 设置 data 属性的值
  84. tmp1->next = NULL;
  85. if(p1 == NULL)
  86. p1 = tmp1;
  87. else { // 遍历到最后的叶子节点, tmp 指针移动到最后一个子节点
  88. while(tmp->next != NULL)
  89. tmp = tmp->next;
  90. tmp->next = tmp1; // 在叶子节点上追加新的节点
  91. }
  92. return p1;
  93. }
  94. struct node * insert_begin(struct node *p, int ele)
  95. {
  96. struct node *tmp = p;
  97. struct node *tmp1=(struct node*)malloc(sizeof(struct node));
  98. tmp1->data=ele;
  99. tmp1->next=p;
  100. p=tmp1;
  101. return p;
  102. }
  103. void l_delete(struct node *p, int ele)
  104. {
  105. struct node *tmp = p;
  106. struct node *pre = tmp;
  107. while(tmp!=NULL)
  108. {if(tmp->data==ele)
  109. { if(tmp==p)
  110. {p=tmp->next;
  111. free(tmp);
  112. return;
  113. }
  114. else
  115. {pre->next=tmp->next;
  116. free(tmp);
  117. return;
  118. }
  119. }
  120. else
  121. { pre=tmp;
  122. tmp=tmp->next;
  123. }
  124. }
  125. printf("\n no match found!! ");
  126. }
  127. void delete_begin(struct node *p)
  128. {
  129. struct node *tmp = p;
  130. if(p==NULL)
  131. printf("\n no element to be deleted!! ");
  132. else
  133. {
  134. printf("\nelement deleted - %d", p->data);
  135. p=p->next;
  136. }
  137. }
  138. void delete_end(struct node *p)
  139. {
  140. struct node *tmp = p;
  141. struct node* pre;
  142. if(p==NULL)
  143. printf("\n no element to be deleted!! ");
  144. else if(p->next==NULL)
  145. {
  146. printf("\nelement deleted - %d", p->data);
  147. p=NULL;
  148. }
  149. else
  150. {
  151. while(tmp->next!=NULL){
  152. pre=tmp;
  153. tmp=tmp->next;
  154. }
  155. pre->next=NULL;
  156. printf("\nelement deleted - %d", tmp->data);
  157. }
  158. }
  159. void display(struct node *p)
  160. {
  161. printf("display elements in p\n");
  162. struct node *tmp = p;
  163. while(tmp!=NULL){
  164. printf("\n %d",tmp->data);
  165. tmp=tmp->next;
  166. }
  167. }