fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct node {
  6. int val;
  7. struct node *next;
  8. }Node;
  9.  
  10. Node *head = 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. insHead(x);
  145. }
  146.  
  147. int pop(){
  148. int n = head->val;
  149. delHead();
  150. return n;
  151. }
  152.  
  153. void enqueue(int x){
  154. insTail(x);
  155. }
  156.  
  157. int dequeue(){
  158. Node ret = *getN(1);
  159. delHead();
  160. return ret.val;
  161. }
  162.  
  163. int main(void){
  164. int s1,s2,s3,q1,q2,q3;
  165.  
  166. push(1);
  167. push(2);
  168. push(3);
  169. s1 = pop();
  170. s2 = pop();
  171. s3 = pop();
  172. printf("%d %d %d\n",s1,s2,s3);
  173. enqueue(1);
  174. enqueue(2);
  175. enqueue(3);
  176. q1 = dequeue();
  177. q2 = dequeue();
  178. q3 = dequeue();
  179. printf("%d %d %d\n",q1,q2,q3);
  180.  
  181. freeL();
  182. return 0;
  183. }
  184.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
3 2 1
1 2 3