KEMBAR78
Stack With Queues PDF | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
136 views3 pages

Stack With Queues PDF

This C++ program implements a stack using a linked list. It defines a Stack_LL class with methods to push, pop, check if empty, return the top element, and return the count of elements. These methods manipulate a linked list with nodes containing value and pointer fields to implement the stack functionality.

Uploaded by

Usama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views3 pages

Stack With Queues PDF

This C++ program implements a stack using a linked list. It defines a Stack_LL class with methods to push, pop, check if empty, return the top element, and return the count of elements. These methods manipulate a linked list with nodes containing value and pointer fields to implement the stack functionality.

Uploaded by

Usama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1: #include<iostream>

2: #include<queue>
3: using namespace std;
4: class Stack_LL{
5: struct node{
6: int valu;
7: node *next;
8: node *prev;
9: };
10: queue<int> que;
11: node *Top;
12: node *head = new node ;
13: bool empty = true;
14: public:
15: void push(int valu)
16: {
17: node *new_node = new node;
18: new_node->valu = valu;
19: new_node->next = NULL;
20: if(empty){
21: head = new_node;
22: head->prev = NULL;
23: Top = head;
24: empty = false;
25: }
26: else{
27: node *ptr = head;
28: while(ptr->next != NULL){
29: ptr = ptr->next;
30: }
31: new_node->prev = ptr;
32: ptr->next = new_node;
33: Top = ptr->next;
34: }
35: }
36: int top(){
37: if(Top!=NULL)
38: {
39: return Top->valu;
40: }
41: else
42: {
43: cout<<"Stack is empty";
44: return 0;
45: }
46:
47: }
48: void pop(){
49: if(Top>=0)
50: Top = Top->prev;
51: }
52: bool isEmpty()
53: {
54: node *ptr = Top;
55: while(ptr!= NULL){
56: que.push(ptr->valu);
57: ptr = ptr->prev;
58: }
59: if(que.empty())
60: return true;
61: else
62: {
63: while(!que.empty()){
64: que.pop();
65: }
66: false;
67: }
68: }
69: int count(){
70: node *ptr = Top;
71: while(ptr!= NULL){
72: que.push(ptr->valu);
73: ptr = ptr->prev;
74: }
75: int count = 0;
76: while(!que.empty())
77: {
78: que.pop();
79: count++;
80: }
81: return count;
82: }
83: };
84: int main()
85: {
86: Stack_LL obj1;
87: obj1.push(100);
88: obj1.push(200);
89: obj1.push(300);
90: cout<<"Count : "<<obj1.count()<<endl;
91: cout<<obj1.top()<<endl;
92: obj1.pop();
93: ; cout<<
94: obj1.pop();
95: if(obj1.isEmpty())
96: cout<<"Yes! Stack is empty\n";
97: else
98: cout<<"Not Empty\n";
99: cout<<"Count : "<<obj1.count();
100:
101:
102: }

You might also like