Stacks&Queues Linked List [Problem with pop and deque]
Apr 1, 2010 at 10:04pm UTC
Alright so it works fine, but the proble is that it pops/deques out all the elements instead of one element. Say for instance I pushed in 5 then 4. The stack would give me 4 and 5 instead of just 4. Vise versa with deque, if I put in 5 then 4. It deques 5 and 4 instead of just 5.
This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <Stack>
#include <Queue>
using namespace std;
typedef struct A
{
int key;
struct A *stackpop;
struct A *dequel;
}l;
void push( int );
void enque( int );
void dequee( );
void pop( );
char menu()
{
char vect, Q;
cout << "\n A) Push " << endl;
cout << "\n B) Enque " << endl;
cout << "\n C) Pop " << endl;
cout << "\n D) Deque " << endl;
cout << "\n E) Exit " << endl;
cin>>vect;
Q=vect;
return Q;
}
int main()
{
int x;
while (1)
{
char c; c=menu();
switch (c)
{
case 'a' :
cout<<"Please enter in #:" <<endl;
cin>>x;
push(x);
break ;
case 'b' :
cout<<"Please enter in #:" <<endl;
cin>>x;
enque(x);
break ;
case 'c' :
pop();
break ;
case 'd' :
dequee();
break ;
case 'e' :
exit(0);
break ;
}
}
system("pause" );
return 0;
}
l *head;
l *tail;
l *temp1;
l *temp2;
l *temp3;
l *temp4;
void push( int n )
{
temp1 = head;
temp2 = tail;
if (temp1 == NULL)
{
temp1 = (l *)malloc(sizeof (l));
temp1 -> key = n;
temp1 -> stackpop = NULL;
head = temp1;
temp2 = (l *)malloc(sizeof (l));
temp2 -> key = n;
temp2 -> dequel = NULL;
tail = temp2;
}
else
{
temp3 = (l *)malloc(sizeof (l));
temp3 -> key = n;
temp3 -> stackpop = head;
head = temp3;
while (temp2 -> dequel != NULL)
temp2 = temp2 -> dequel;
temp4 = (l *)malloc(sizeof (l));
temp4 -> key = n;
temp4 -> dequel = NULL;
temp2 -> dequel = temp4;
}
}
void enque(int n)
{
temp1 = head;
temp2 = tail;
if (temp2 == NULL)
{
temp2 = (l *)malloc(sizeof (l));
temp2 -> key = n;
temp2 -> dequel = NULL;
tail = temp2;
temp1 = (l *)malloc(sizeof (l));
temp1 -> key = n;
temp1 -> stackpop = NULL;
head = temp1;
}
else
{
temp4 = (l *)malloc(sizeof (l));
temp4 -> key = n;
temp4 -> dequel = tail;
tail = temp4;
while (temp1 -> stackpop != NULL)
temp1 = temp1 -> stackpop;
temp3 = (l *)malloc(sizeof (l));
temp3 -> key = n;
temp3 -> stackpop = NULL;
temp1 -> stackpop = temp3;
}
}
void pop()
{
temp1 = head;
if (temp1 == NULL)
{
cout << "The stack is empty." <<endl;
return ;
}
cout << "Popping: " <<endl;
while (temp1 != NULL)
{
printf("\n\t%d" ,temp1 -> key);
temp1 = temp1 -> stackpop;
}
}
void dequee()
{
temp2 = tail;
if (temp2 == NULL)
{
cout << "The queue is empty" <<endl;
return ;
}
cout << "Dequing: " <<endl;
while (temp2 != NULL)
{
printf("\n\t%d" ,temp2 -> key);
temp2 = temp2 -> dequel;
}
}
How would I make it so it only pops/deques one element and still holds the other numbers in standby if I ask it again?
Apr 1, 2010 at 10:23pm UTC
See lines 158 and 176. What do you see?
Apr 1, 2010 at 10:29pm UTC
Ahhh.. a while loop. Now I feel stupid XD
Thanks!
Apr 1, 2010 at 10:32pm UTC
I think the structure may have memory leaks. I see malloc() but no calls to free().
Apr 1, 2010 at 10:42pm UTC
I didn't really see a difference in it. It didn't seem to have memory leaks, but I'll add it in case. Thanks again ^^
Apr 2, 2010 at 12:17pm UTC
helios was just being nice. Your data structure does have memory leaks.
Apr 2, 2010 at 8:53pm UTC
I know. Sorry if I seemed mean O.o I didn't mean to.
Topic archived. No new replies allowed.