I'm trying to implement a Stack (FILO) with a doubly linked list. I have it working with a Queue (FIFO) but when I try changing things around it don't work.
If anyone has any tips please let me know I would greatly appreciate it. Also how to check if the list is empty is another thing I'm having a problem with.
Here is my code with the Queue function but I need it to work with a Stack function.
// libraries
#include <iostream>
usingnamespace std;
struct Item
{
// declare variables
Item* previous; // pointer
char aChar;
Item* next; // the link
}; // close struct
// ------------FIX------------
// So it is a Stack not a Queue
void push(Item* tail, int aInt)
{
int i;
char aChar;
for ( i = 0; i < aInt; i++)
{
cout << endl;
cout << "Enter a Char. : ";
cin >> aChar;
tail->aChar = aChar;
tail->next = new Item;
tail->next->previous = tail;
tail = tail->next;
} // close for
tail->next = 0;
} // close push
void printList(const Item* h, int aInt)
{
int i = 0;
for (const Item* p = h; i < aInt; p = p->next)
{
cout << endl;
cout << p->aChar << endl;
i++;
}
} // close void
Item* deleteList(Item* h)
{
while(h)
{
Item* next = h->next;
delete h;
h = next;
}
return h; // equals 0
}
int main()
{
Item* top = new Item; // dynamic memory allocation
Item* tail = top;
top->previous = 0;
int i, aInt = 0;
char aChar;
cout << endl;
cout << "How many chars do you want to enter? : ";
cin >> aInt;
if (aInt > 0)
{
// function call
push(tail, aInt); // fill list
} // close if
cout << "\nPrinted List: " << endl;
// function call
printList(top, aInt); // print list
// set top = to the function value
top = deleteList(top); // function call to deallocate memory
// return value
return 0;
} // close main
How many chars do you want to enter? : 4
Enter a char. : a
Enter a char. : b
Enter a char. : c
Enter a char. : d
Printed list:
a
b
c
d
I want the output to be:
How many chars do you want to enter? : 4
Enter a char. : a
Enter a char. : b
Enter a char. : c
Enter a char. : d
Printed list:
d
c
b
a
Stack needs two methods: push and pop. You only have push.
A stack is used when you only care about the top item, so if you iterate through it, you're using it like a normal list.
The way to use a stack would be
Also, note that If you use a linked list, it is best to have a class List which would hold pointers to the first and last elements, also deal with insertions and etc.
Thanks but I'm just doing what I've learned in class and I'm a Beginner. I guess I should of said I'm trying to create a stack with a linked list. I just haven't finished so thats why I don't have the "Pop" function. Thanks again but I haven't got to the stuff in your code. I can get a stack to work in main but when I try it in a function it doesn't work. What I understand a stack to be is "LIFO", we haven't got to the [#include <stack>] library
Thanks again for your time & help.
This works as a stack but not when I put it into a function.
Works in main() but when I put it into a function It don't work.
Node* top = new Node; // dynamic memory allocation
Node* tail = top;
tail->next = 0;
int i, aNum;
// This is the part I can't get to work in a function
for (i = 0; i < 5; i++)
{
cout << "Enter int : ";
cin >> aNum;
cin.ignore(1000, 10);
tail->aInt = aNum;
tail->prev = new Node;
tail->prev->next = tail;
tail = tail->prev;
} // close for
top->prev = 0;