fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5. int val;
  6. struct node *next;
  7. }Node;
  8.  
  9. Node *head = NULL;
  10. Node *tail = NULL;
  11.  
  12. Node* createN(int x){
  13. Node *newnode;
  14. newnode = (Node *)malloc(sizeof(Node));
  15. newnode->val = x;
  16. newnode->next = NULL;
  17. return newnode;
  18. }
  19.  
  20. void initL(int n){
  21. int x,i;
  22. Node *p;
  23. scanf("%d",&x);
  24. head = createN(x);
  25. p = head;
  26. for(i=1;i<n;i++){
  27. scanf("%d",&x);
  28. p->next = createN(x);
  29. p = p->next;
  30. }
  31. }
  32.  
  33. void freeL(){
  34. Node *p;
  35. while(head!=NULL){
  36. p = head->next;
  37. free(head);
  38. head = p;
  39. }
  40. }
  41.  
  42. void printN(Node *a){
  43. if(a == NULL) printf("NULL\n");
  44. else printf("%d\n",a->val);
  45. }
  46.  
  47. void printL(){
  48. Node *p = head;
  49. while(p != NULL){
  50. printf("%d ",p->val);
  51. p = p->next;
  52. }
  53. printf("\n");
  54. }
  55.  
  56. Node* getN(int n){
  57. int i;
  58. Node *p;
  59. p = head;
  60. for(i=1;i<n;i++) p = p->next;
  61. return p;
  62. }
  63.  
  64. int countL(){
  65. int ret = 0;
  66. Node *p = head;
  67. while(p!=NULL){
  68. p = p->next;
  69. ret++;
  70. }
  71. return ret;
  72. }
  73.  
  74. Node* searchX(int x){
  75. Node *p;
  76. for(p=head; p!=NULL; p=p->next){
  77. if(p->val == x) break;
  78. }
  79. return p;
  80. }
  81.  
  82. void insHead(int x){
  83. Node *p; //1
  84. p = createN(x); //1
  85. p->next = head; //2
  86. head = p; //3
  87. }
  88.  
  89. void insMiddle(int n, int x){
  90. int i;
  91. Node *p,*q;
  92. p = head; //1
  93. for(i=1;i<n;i++){ //2
  94. p = p->next; //2
  95. }
  96. q = createN(x); //3
  97. q->next = p->next; //4
  98. p->next = q; //5
  99. }
  100.  
  101. void insTail(int x){
  102. Node *p;
  103. p = head; //1
  104. if(p==NULL){
  105. head = createN(x);
  106. return;
  107. }
  108. while(p->next != NULL){ //2
  109. p = p->next; //2
  110. }
  111. p->next = createN(x); //3
  112. }
  113.  
  114. void delHead(){
  115. Node *p;
  116. p = head; //1
  117. head = head->next; //2
  118. free(p); //3
  119. }
  120.  
  121. void delMiddle(int n){
  122. int i;
  123. Node *p,*q;
  124. p = head; //1
  125. for(i=1;i<n-1;i++){ //2
  126. p = p->next; //2
  127. }
  128. q = p->next; //3
  129. p->next = q->next; //4
  130. free(q); //5
  131. }
  132.  
  133. void delTail(){
  134. Node *p;
  135. p = head; //1
  136. while(p->next->next != NULL){ //2
  137. p = p->next; //2
  138. }
  139. free(p->next); //3
  140. p->next = NULL; //4
  141. }
  142.  
  143. void push(int x){
  144. Node *p=createN(x);
  145. p->next=head;
  146. head=p;
  147. }
  148.  
  149. int pop(){
  150. if(head==NULL){
  151. return 0;
  152. }
  153. Node *p=head;
  154. int value=p->val;
  155. head=head->next;
  156. free(p);
  157. return value;
  158. }
  159.  
  160. void enqueue(int x){
  161. Node *p=createN(x);
  162. if(tail==NULL){
  163. head=tail=p;
  164. }else{
  165. tail->next=p;
  166. tail=p;
  167. }
  168. }
  169.  
  170. int dequeue(){
  171. if(head==NULL){
  172. return 0;
  173. }
  174. Node *p=head;
  175. int value=p->val;
  176. head=head->next;
  177. if(head==NULL){
  178. tail=NULL;
  179. }
  180. free(p);
  181. return value;
  182. }
  183.  
  184. int main(void){
  185. int s1,s2,s3,q1,q2,q3;
  186.  
  187. push(1);
  188. push(2);
  189. push(3);
  190. s1 = pop();
  191. s2 = pop();
  192. s3 = pop();
  193. printf("%d %d %d\n",s1,s2,s3);
  194. enqueue(1);
  195. enqueue(2);
  196. enqueue(3);
  197. q1 = dequeue();
  198. q2 = dequeue();
  199. q3 = dequeue();
  200. printf("%d %d %d\n",q1,q2,q3);
  201. freeL();
  202. return 0;
  203. }
  204.  
Success #stdin #stdout 0.01s 5288KB
stdin
1 2 3
1 2 3
stdout
3 2 1
1 2 3