-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinked list implementation of stack.cpp
More file actions
108 lines (96 loc) · 2.35 KB
/
linked list implementation of stack.cpp
File metadata and controls
108 lines (96 loc) · 2.35 KB
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
100
101
102
103
104
105
106
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include<stdlib.h>
//#define max 100
int n,m;
using namespace std;
struct node{
int data;
node* next;
};
node* head=NULL;
void push(int da)
{
if (head==NULL)
{
node* temp=new node();
temp->data=da;
temp->next=NULL;
head=temp;
}
else{
node *temp=new node();
temp->data=da;
temp->next=head;
head=temp;
}
}
void pop()
{
if(head==NULL)
{
cout<<"stack underflow";
return;
}
node* temp=head;
head=temp->next;
delete temp;
}
void print()
{
cout<<"Your stack is:\n";
node* temp=head;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}cout<<endl;
}
void choice()
{ while(1)
{
int ch,d;
cout<<"ENTER YOUR CHOICE FROM BELOW:-"<<endl;
cout<<"1.push element in stack:\n";
cout<<"2.pop element from the stack\n";
cout<<"3.print stack element\n";
cout<<"4.to Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1: cout<<"how many elements you want to enter in stack:";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"enter "<<i<<"st element:";
cin>>d;
push(d);
cout<<"Pushed in stack"<<endl;
}
break;
case 2: cout<<"how many element you want to pop:";
cin>>m;
for(int j=1;j<=m;j++)
{
pop();
cout<<"poped "<<j<<" times "<<endl;
}
break;
case 3: print();
break;
case 4: exit(1);
default:
cout<<"Invalid choice!"<<endl;
break;
} cout<<endl;
}
}
int main()
{
choice();
return 0;
}