Advertisement
Guest User

linked list

a guest
Jan 16th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. typedef struct node
  7. {
  8.     char *name;
  9.     struct node* nxt;
  10.     struct node* pre;
  11. }Node;
  12.  
  13. typedef struct dqueue
  14. {
  15.     struct node* first;
  16.     struct node* last;
  17.     int amount;
  18. }Dqueue;
  19.  
  20.  
  21. Node *baslangic = NULL;
  22. Dqueue *dq;
  23.  
  24. void add_to_front(char *ad)
  25. {
  26.     printf(">add_to_front%s\n",ad);
  27.     Node *added = (Node*)malloc(sizeof(Node));
  28.     added->name=ad;
  29.     added->nxt=NULL;
  30.     added->pre=dq->last;
  31.     if (baslangic==NULL)
  32.     {
  33.         baslangic=added;
  34.         dq->first=added;
  35.         dq->last=added;
  36.         added->pre=NULL;
  37.         added->nxt=NULL;
  38.         dq->amount=1;
  39.     }
  40.     else
  41.     {
  42.         dq->last->nxt=added;
  43.         dq->last=added;
  44.         dq->amount++;
  45.     }
  46.  
  47. }
  48.  
  49. void add_to_back(char *ad)
  50. {
  51.     printf(">add_to_back%s\n",ad);
  52.     Node *added = (Node*)malloc(sizeof(Node));
  53.     added->name=ad;
  54.     added->pre=NULL;
  55.     added->nxt=dq->first;
  56.     if (baslangic==NULL)
  57.     {
  58.         baslangic=added;
  59.         dq->first=added;
  60.         dq->last=added;
  61.         added->pre=NULL;
  62.         added->nxt=NULL;
  63.         dq->amount=1;
  64.     }
  65.     else
  66.     {
  67.         dq->first->pre=added;
  68.         dq->first=added;
  69.         dq->amount++;
  70.     }
  71.  
  72. }
  73.  
  74.  
  75. void print()
  76. {
  77.     printf(">print\n");
  78.     if (dq->amount==0)
  79.     {
  80.         return;
  81.     }
  82.     Node *tmp;
  83.     tmp=dq->last;
  84.     while (tmp!=dq->first)
  85.     {
  86.         printf("%s\n",tmp->name);
  87.         tmp=tmp->pre;
  88.     }
  89.     printf("%s\n",tmp->name);
  90. }
  91.  
  92. void d_free()
  93. {
  94.  
  95. }
  96.  
  97. void check_front()
  98. {
  99.     printf(">check_front\n");
  100.     if (dq->amount==0)
  101.     {
  102.         return;
  103.     }
  104.  
  105.     printf("%s\n",dq->last->name);
  106. }
  107.  
  108. void check_back()
  109. {
  110.     printf(">check_front\n");
  111.     if (dq->amount==0)
  112.     {
  113.         return;
  114.     }
  115.  
  116.     printf("%s\n",dq->first->name);
  117. }
  118.  
  119. void remove_from_front()
  120. {
  121.     printf(">remove_from_front\n");
  122.     if (dq->amount==1)
  123.     {
  124.         dq->first=NULL;
  125.         dq->last=NULL;
  126.         dq->amount=0;
  127.         return;
  128.     }
  129.     if (dq->amount==0)
  130.     {
  131.         return;
  132.     }
  133.     dq->last->pre->nxt=NULL;
  134.     dq->last=dq->last->pre;
  135.     dq->amount--;
  136. }
  137.  
  138.  
  139. void remove_from_back()
  140. {
  141.     printf(">remove_from_back\n");
  142.     if (dq->amount==1)
  143.     {
  144.         dq->first=NULL;
  145.         dq->last=NULL;
  146.         dq->amount=0;
  147.         return;
  148.     }
  149.     if (dq->amount==0)
  150.     {
  151.         return;
  152.     }
  153.     dq->first->nxt->pre=NULL;
  154.     dq->first=dq->first->nxt;
  155.     dq->amount--;
  156. }
  157.  
  158. int main()
  159. {
  160.     dq = malloc(sizeof(Dqueue));
  161.     dq->amount=0;
  162.     char command[50];
  163.     char parameter[1000];
  164.     while (scanf("%s",command)!=EOF)
  165.     {
  166.         if (strcmp(command,"add_to_front")==0)
  167.         {
  168.             gets(parameter);
  169.             add_to_front(parameter);
  170.         }
  171.         else if (strcmp(command,"add_to_back")==0)
  172.         {
  173.             gets(parameter);
  174.             add_to_back(parameter);
  175.         }
  176.         else if (strcmp(command,"remove_from_back")==0)
  177.             remove_from_back(parameter);
  178.         else if (strcmp(command,"remove_from_front")==0)
  179.             remove_from_front();
  180.         else if (strcmp(command,"check_front")==0)
  181.             check_front();
  182.         else if (strcmp(command,"check_back")==0)
  183.             check_back();
  184.         else if (strcmp(command,"print")==0)
  185.             print();
  186.         else
  187.         {
  188.             printf(">%s\n",command);
  189.             printf(" HUH?\n");
  190.         }
  191.     }
  192.  
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement