Source Code:
//HEAD
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
struct dequeNode
{
int data;
struct dequeNode* next;
struct dequeNode* prev;
};
typedef struct dequeNode dequeNode;
dequeNode* newNode(int val)
{
dequeNode* t = (dequeNode*) malloc(sizeof(dequeNode));
t->next = t->prev = NULL;
t->data=val;
return t;
}
//BODY
dequeNode* head, *tail;
void push_back(int val)
{
dequeNode* node=newNode(val);
if(head==NULL)
{
tail=head=node;
}
else{
tail->next=node;
node->prev=tail;
tail=node;
}
}
void push_front(int val)
{
dequeNode* node=newNode(val);
if(head==NULL)
{
tail=head=node;
}
else{
node->next=head;
head->prev=node;
head=node;
}
}
int back()
{
return tail->data;
}
int front()
{
return head->data;
}
void pop_back()
{
if(head==NULL) return;
dequeNode* t=tail;
tail=tail->prev;
if(tail) tail->next=NULL;
if(tail==NULL) head=NULL;
free(t);
}
void pop_front()
{
if(head==NULL) return;
dequeNode* h=head;
head=head->next;
if(head) head->prev=NULL;
if(head==NULL) tail=NULL;
free(h);
}
int empty()
{
return(head==NULL);
}
// TAIL
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
char s[50];
scanf(" %s", s);
if (s[0] == 'b')
{
if (empty()) printf("invalid\n");
else printf("%d\n", back());
}
if (s[0] == 'f')
{
if (empty()) printf("invalid\n");
else printf("%d\n", front());
}
if (s[0] == 'p')
{
if (s[1] == 's')
{
if (s[2] == 'b')
{
int x; scanf(" %d", &x);
push_back(x);
}
if (s[2] == 'f')
{
int x; scanf(" %d", &x);
push_front(x);
}
}
else
{
if (s[2] == 'b')
{
if (empty()) printf("invalid\n");
else pop_back();
}
if (s[2] == 'f')
{
if (empty()) printf("invalid\n");
else pop_front();
}
}
}
}
return 0;
}
//HEAD
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
struct dequeNode
{
int data;
struct dequeNode* next;
struct dequeNode* prev;
};
typedef struct dequeNode dequeNode;
dequeNode* newNode(int val)
{
dequeNode* t = (dequeNode*) malloc(sizeof(dequeNode));
t->next = t->prev = NULL;
t->data=val;
return t;
}
//BODY
dequeNode* head, *tail;
void push_back(int val)
{
dequeNode* node=newNode(val);
if(head==NULL)
{
tail=head=node;
}
else{
tail->next=node;
node->prev=tail;
tail=node;
}
}
void push_front(int val)
{
dequeNode* node=newNode(val);
if(head==NULL)
{
tail=head=node;
}
else{
node->next=head;
head->prev=node;
head=node;
}
}
int back()
{
return tail->data;
}
int front()
{
return head->data;
}
void pop_back()
{
if(head==NULL) return;
dequeNode* t=tail;
tail=tail->prev;
if(tail) tail->next=NULL;
if(tail==NULL) head=NULL;
free(t);
}
void pop_front()
{
if(head==NULL) return;
dequeNode* h=head;
head=head->next;
if(head) head->prev=NULL;
if(head==NULL) tail=NULL;
free(h);
}
int empty()
{
return(head==NULL);
}
// TAIL
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
char s[50];
scanf(" %s", s);
if (s[0] == 'b')
{
if (empty()) printf("invalid\n");
else printf("%d\n", back());
}
if (s[0] == 'f')
{
if (empty()) printf("invalid\n");
else printf("%d\n", front());
}
if (s[0] == 'p')
{
if (s[1] == 's')
{
if (s[2] == 'b')
{
int x; scanf(" %d", &x);
push_back(x);
}
if (s[2] == 'f')
{
int x; scanf(" %d", &x);
push_front(x);
}
}
else
{
if (s[2] == 'b')
{
if (empty()) printf("invalid\n");
else pop_back();
}
if (s[2] == 'f')
{
if (empty()) printf("invalid\n");
else pop_front();
}
}
}
}
return 0;
}
Comments
Post a Comment