Stacks and queues for C++

Hello my friends. I coded a doubly linked list here and i have implemented stacks and queues.....or so i think. Im posting my code to see if I really did implement Stacks and Queues in my code or if im just doing a print and reverse print of my nodes. Sorry if my explanation is off but my code will help you better understand what I mean.

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


#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>

using namespace std;

	struct node
	{
		string data;
		node* next;
		node* prev;
	};

	void stack(node* head);
	void queue(node* tail);
int _tmain(int argc, _TCHAR* argv[])
{
	node* head;
	node* tail;
	node* n;
	

	n= new node;
	n->data = "Maria";
	n->prev = 0;
	head = n;
	tail = n;

	n = new node;
	n->data = "Pablo";
	n->prev = tail;
	tail->next = n;
	tail = n;

	n = new node;
	n->data = "Andrea";
	n->prev = tail;
	tail->next = n;
	tail = n;

	n = new node;
	n->data = "Carolina";
	n->prev = tail;
	tail->next = n;
	tail = n;

	n = new node;
	n->data = "Alejandro";
	n->prev = tail;
	tail->next = n;
	tail = n;
	tail->next = 0;

	stack(head);
	queue(tail);

	system ("pause");
}

void stack(node* head)
	{
		node* temp = head;
		cout << "Stack :\n" << endl;
		while(temp != 0)
		{
			cout << temp->data << "\n";
			temp = temp->next;
		}
		cout << "\n";
	}
	void queue(node* tail)
	{
		node* temp = tail;
		cout << "Queue :\n" << endl;
		while(temp != 0)
		{
			cout << temp->data << "\n";
			temp = temp->prev;
		}
		cout << "\n";
	}



would I need a counter? I have decided to use names instead of numbers is that ok???? im really doubting myself for some reason....well thanks for looking at my post and for the help, in advance.
Last edited on
The difference between stacks and queues:

Stacks can only be accessed from one end.
- insert on top and remove on top
or
- insert on bottom and remove on bottom

Queues can be accessed from both ends, but only for one purpose.
- insert on top and remove on bottom
or
- insert on bottom and remove on top

Is that what you have?
if I really did implement Stacks and Queues

No, you didn't.
if im just doing a print and reverse print of my nodes

Yes you did.

A Stack is a LIFO container.
By definition , you can
push to it,consequently adding an element to the top of it.
pop from it , consequently removing the top element and retrieve it
(optional)peek the top element.
Reference : http://en.wikipedia.org/wiki/Stack_(abstract_data_type)
A Queue is a FIFO (optionally ordered) container.
By definition , you can
enqueue an element at its back,
dequeue an element from its front , consequently removing it and retrieve it.
(optional)peek at the front element.
Reference:http://en.wikipedia.org/wiki/Queue_(data_structure)
So you need to add those respective functions,like
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
struct my_stack
{
    node* head;
    my_stack() : head(nullptr) {}
    ~my_stack()
    {
        ...for each node starting from head deallocate memory...
    }
};
void push_stack(my_stack* _stack,string toinsert)
{
    if(head == nullptr)
    {
        _stack->head = new node;
        _stack->head->prev = nullptr;
        _stack->head->data = (toinsert);
    }
    else
    {
        node *newnode = new node(toinsert);
        newnode->next = _stack->head;
        newnode->prev = nullptr;
        _stack->head = newnode;
    }
}

Note: The above function is just an example I hastily made,its not tested,may not work as expected.
would I need a counter?
I don't know what that means.

Hope that helps.
Last edited on
thanks i appreciate the help. when i said counter i meant an accumulator but i dont need one. yea im still trying to teach myself about queue and stacks but to implement them gets a little confusing for me as you can see in my code. Although i find it a little bit easier to code it in c++ than c#.
man im really not getting it.....looking your example is that i have to create another struct then i would have to create a void function and from the void, pass the stack by reference to my main and then call the function "void push_stack" from my main?
Look at my previous post,a stack(see std::stack) and a queue(see std::queue) are containers ,look at the respective Wikipedia articles.
They should have the given functions , that said i gave you an example of how it could be implemented using your doubly-linked list's node.While there could be other functions like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void push_stack(mystack& st,string data);//c-style independent data structures and functions
string pop_stack(mystack& st);
int size_stack(mystack& st);
//other useful functions
//...
//or using classes/structs
template<class data_type>
struct stack
{
 private:
 //a stack is just wrapper,
 //underlying implementation could be linked list etc.
 public:
 data_type pop();
 void push(data_type data);
 size_t size();
 //other useful functions...
}

Also your doubly-linked lists could really use some functions like insert(),remove() ,see std::list,using them you could more easily implement stack and queue.
References :
http://www.cplusplus.com/reference/queue/queue/
http://www.cplusplus.com/reference/stack/stack/
Last edited on
Topic archived. No new replies allowed.