News
  • Submission of Theory Assignments 6,7,8 of OS on 1st April
  • Case Study Presentation of OS on 4st April
  • CN and PNS Journal Submission on 6th April

DS (RM) Program - Stack And Queue: Stack – Linked List Implementation

DS (RM) Program - Stack And Queue: Stack – Linked List Implementation






 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
#include<iostream.h>
#include<conio.h>
#include<process.h>

class stack
{
 int max;
 int st[10];
 int top;
 int count,size;

 public :
 stack()
 {
  top=-1;
  count =0;
  size=10;
 }

 void push(int var)
 {
if(top>=size-1)
  {
   cout<<"\nError : Stack Full ....!!!!\n";
   exit(1);
  }
  else
   st[++top]=var;
 }

 int pop()
 {
  if(top<0)
  {
   cout<<"\nError : Stack is empty .....!!!!\n";
   exit(1);
  }
  return st[top--];
 }

 int stacktop()
 {
  if(top<0)
  {
   cout<<"\nError : Stack is empty ....!!!!";
   exit(1);
  }
return st[top];
 }
};

void main()
{
 stack s1;
 int num, n, i;

 clrscr();

 cout<<"Enter the number of element : \n";
 cin>>n;

 for(i=0; i<n; i++)
 {
  cout<<"Enter the number : \n";
  cin>>num;
  s1.push(num);
 }

 cout<<"\nThe element at top : "<<s1.stacktop()<<endl;
 cout<<"\nThe pop element is : ";

 for(i=0; i<n; i++)
 {
cout<<s1.pop()<<endl;
  getch();
 }

 cout<<endl<<s1.pop();
 cout<<endl;
}

Output:
Enter the number of element :
3
Enter the number :
56
Enter the number :
2
Enter the number :
33

The element at top : 33

The pop element is : 33
2
56

© Copyright NMITD MCA Notes Published.. Blogger Templates
Back To Top