Implement a stack with a Doubly linked list

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.

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
// libraries
#include <iostream>
using namespace 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


Thanks!
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
1
2
3
4
5
6
7
stack s;
s.push('a); s.push('b); s.push('c');

while(!s.empty()){
   std::cout << s.top();
   s.pop();
}
Though iterating just isn't what a stack is meant to do.

See http://en.wikipedia.org/wiki/Stack_(data_structure)
and http://www.cplusplus.com/reference/stl/stack/
to understand what a stack is and what properties should it have.

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.

Code Block:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  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;


Last edited on
It's not a matter of your skill, it's just that you don't see the difference between a stack and a normal linked list.

main() is a function. What goes wrong when you put it into another function (errors? wrong results?)
This is how you push data on the stack
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
void push(Item** top, int aInt)
{
  int i;
  char aChar;
  
  for ( i = 0; i < aInt; i++)
  {
    cout << endl;
    cout << "Enter a Char. : ";
    cin >> aChar;

    Item *new_top = new Item;
    new_top->aChar = aChar;
    new_top->previous = 0;
    new_top->next = (*top);
    (*top)->previous = new_top;
    (*top) = new_top;
  } // close for

} // close push

...

Node* top = NULL;
push(&top, ...);
Topic archived. No new replies allowed.