How to create a deque using 2 stacks

Hi!I am trying to create a deque using these stacks but I failes again and again.If anyone can help:

struct deque {

int key;

dek *next;

} *left = NULL, *right=NULL;




struct elem{

int key;


elem *next;

} *start1=NULL,*start2=NULL,*p;



void add_1(int n) //Adds elements in the first stack

{

elem *p = start1;

start1 = new elem;

start1->key = n;

start1->next = p;

}

void add_2(int n) //Adds elements in the second stack

{

elem *p = start2;

start2 = new elem;

start2->key = n;

start2->next = p;
Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/

What failes?
What is dek in deque?

From the structure I'd expect something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct elem{

int key;


elem *next;

};


struct deque {

elem *left;
elem *right;

};
The structures are ok,I am trying to form a deque using the 2 stacks.I think it will be done by combining the elements that I add in the stacks
So what's your problem?
The problem is that the function,which forms the deque from the 2 stacks doesn`t work:


void build (int n)

{

elem * n1 = start1; //pointer to the first stack

elem* n2 = start2; //pointer to the second stack

while (n1 != NULL || n2 != NULL)

{


push_l(n);

n1 = n1->next;


push_r(n);

n2 = n2->next;

}

}
Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/


if start1/start2 have different amount of subsequent elements it will crash for that with less elements.

if push_l(n); and/or push_r(n); appends an element you will have an infinite loop

apart from that i have no idea what you mean with 'doesn`t work'
Topic archived. No new replies allowed.