0% found this document useful (0 votes)
18 views

Implement Queue Using Link List

This C++ program implements a stack using a linked list. It defines a StackNode class to represent nodes, and functions like push(), pop(), peek() and isEmpty() to manipulate the stack. The main() function demonstrates pushing elements onto the stack, popping them off, and printing the remaining elements.

Uploaded by

BHOPA SWAMI
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Implement Queue Using Link List

This C++ program implements a stack using a linked list. It defines a StackNode class to represent nodes, and functions like push(), pop(), peek() and isEmpty() to manipulate the stack. The main() function demonstrates pushing elements onto the stack, popping them off, and printing the remaining elements.

Uploaded by

BHOPA SWAMI
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// C++ program for linked list implementation of stack

#include <bits/stdc++.h>
using namespace std;

// A structure to represent a stack


class StackNode {
public:
int data;
StackNode* next;
};

StackNode* newNode(int data)


{
StackNode* stackNode = new StackNode();
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}

int isEmpty(StackNode* root)


{
return !root;
}

void push(StackNode** root, int data)


{
StackNode* stackNode = newNode(data);
stackNode->next = *root;
*root = stackNode;
cout << data << " pushed to stack\n";
}

int pop(StackNode** root)


{
if (isEmpty(*root))
return INT_MIN;
StackNode* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);

return popped;
}

int peek(StackNode* root)


{
if (isEmpty(root))
return INT_MIN;
return root->data;
}

// Driver code
int main()
{
StackNode* root = NULL;

push(&root, 10);
push(&root, 20);
push(&root, 30);
cout << pop(&root) << " popped from stack\n";

cout << "Top element is " << peek(root) << endl;

cout<<"Elements present in stack : ";


//print all elements in stack :
while(!isEmpty(root))
{
// print top element in stack
cout<<peek(root)<<" ";
// remove top element in stack
pop(&root);
}

return 0;
}

You might also like