Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct node
- {
- char *name;
- struct node* nxt;
- struct node* pre;
- }Node;
- typedef struct dqueue
- {
- struct node* first;
- struct node* last;
- int amount;
- }Dqueue;
- Node *baslangic = NULL;
- Dqueue *dq;
- void add_to_front(char *ad)
- {
- printf(">add_to_front%s\n",ad);
- Node *added = (Node*)malloc(sizeof(Node));
- added->name=ad;
- added->nxt=NULL;
- added->pre=dq->last;
- if (baslangic==NULL)
- {
- baslangic=added;
- dq->first=added;
- dq->last=added;
- added->pre=NULL;
- added->nxt=NULL;
- dq->amount=1;
- }
- else
- {
- dq->last->nxt=added;
- dq->last=added;
- dq->amount++;
- }
- }
- void add_to_back(char *ad)
- {
- printf(">add_to_back%s\n",ad);
- Node *added = (Node*)malloc(sizeof(Node));
- added->name=ad;
- added->pre=NULL;
- added->nxt=dq->first;
- if (baslangic==NULL)
- {
- baslangic=added;
- dq->first=added;
- dq->last=added;
- added->pre=NULL;
- added->nxt=NULL;
- dq->amount=1;
- }
- else
- {
- dq->first->pre=added;
- dq->first=added;
- dq->amount++;
- }
- }
- void print()
- {
- printf(">print\n");
- if (dq->amount==0)
- {
- return;
- }
- Node *tmp;
- tmp=dq->last;
- while (tmp!=dq->first)
- {
- printf("%s\n",tmp->name);
- tmp=tmp->pre;
- }
- printf("%s\n",tmp->name);
- }
- void d_free()
- {
- }
- void check_front()
- {
- printf(">check_front\n");
- if (dq->amount==0)
- {
- return;
- }
- printf("%s\n",dq->last->name);
- }
- void check_back()
- {
- printf(">check_front\n");
- if (dq->amount==0)
- {
- return;
- }
- printf("%s\n",dq->first->name);
- }
- void remove_from_front()
- {
- printf(">remove_from_front\n");
- if (dq->amount==1)
- {
- dq->first=NULL;
- dq->last=NULL;
- dq->amount=0;
- return;
- }
- if (dq->amount==0)
- {
- return;
- }
- dq->last->pre->nxt=NULL;
- dq->last=dq->last->pre;
- dq->amount--;
- }
- void remove_from_back()
- {
- printf(">remove_from_back\n");
- if (dq->amount==1)
- {
- dq->first=NULL;
- dq->last=NULL;
- dq->amount=0;
- return;
- }
- if (dq->amount==0)
- {
- return;
- }
- dq->first->nxt->pre=NULL;
- dq->first=dq->first->nxt;
- dq->amount--;
- }
- int main()
- {
- dq = malloc(sizeof(Dqueue));
- dq->amount=0;
- char command[50];
- char parameter[1000];
- while (scanf("%s",command)!=EOF)
- {
- if (strcmp(command,"add_to_front")==0)
- {
- gets(parameter);
- add_to_front(parameter);
- }
- else if (strcmp(command,"add_to_back")==0)
- {
- gets(parameter);
- add_to_back(parameter);
- }
- else if (strcmp(command,"remove_from_back")==0)
- remove_from_back(parameter);
- else if (strcmp(command,"remove_from_front")==0)
- remove_from_front();
- else if (strcmp(command,"check_front")==0)
- check_front();
- else if (strcmp(command,"check_back")==0)
- check_back();
- else if (strcmp(command,"print")==0)
- print();
- else
- {
- printf(">%s\n",command);
- printf(" HUH?\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement