Last active
September 6, 2017 19:28
-
-
Save lsiddiqsunny/f76dce9b1f4b737f6c9263a8e0be3d79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#define NULL_VALUE -99999 | |
#define SUCCESS_VALUE 99999 | |
struct listNode | |
{ | |
int item; | |
struct listNode * next; | |
}; | |
class dequeue | |
{ | |
struct listNode * list; | |
struct listNode * tail; | |
public: | |
dequeue() | |
{ | |
list=0; | |
tail=0; | |
} | |
int Push_front(int Item) | |
{ | |
struct listNode * newNode ; | |
newNode = (struct listNode*) malloc (sizeof(struct listNode)) ; | |
newNode->item = Item ; | |
if(list==0) | |
{ | |
list=newNode; | |
list->next=0; | |
tail=newNode; | |
tail->next=0; | |
return SUCCESS_VALUE; | |
} | |
if(list->next==0) | |
{ | |
tail=list; | |
newNode->next=list; | |
list=newNode; | |
return SUCCESS_VALUE; | |
} | |
newNode->next=list; | |
list=newNode; | |
return SUCCESS_VALUE; | |
} | |
int Push_back(int item) | |
{ | |
struct listNode * newNode ; | |
newNode = (struct listNode*) malloc (sizeof(struct listNode)) ; | |
newNode->item = item ; | |
if(tail==0) | |
{ | |
tail=newNode; | |
tail->next=0; | |
return SUCCESS_VALUE; | |
} | |
tail->next=newNode; | |
newNode->next=0; | |
tail=newNode; | |
return SUCCESS_VALUE; | |
} | |
int Pop_front() | |
{ | |
if(list==0) | |
{ | |
return NULL_VALUE; | |
} | |
listNode *temp=list; | |
list=list->next; | |
free(temp); | |
return SUCCESS_VALUE; | |
} | |
int Pop_back() | |
{ | |
if(tail==0) | |
{ | |
return NULL_VALUE; | |
} | |
listNode *temp=list; | |
while(temp->next!=tail) | |
{ | |
temp=temp->next; | |
} | |
tail=temp; | |
tail->next=0; | |
return SUCCESS_VALUE; | |
} | |
void Print() | |
{ | |
struct listNode * temp; | |
temp = list; | |
while(temp!=0) | |
{ | |
printf("%d->", temp->item); | |
temp = temp->next; | |
} | |
printf("\n"); | |
} | |
}; | |
int main(void) | |
{ | |
dequeue s; | |
s.Push_front(4); | |
s.Print(); | |
s.Push_back(-1); | |
s.Print(); | |
s.Pop_back(); | |
s.Print(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment