-
Notifications
You must be signed in to change notification settings - Fork 449
/
Pointer implementation using c++
73 lines (61 loc) · 1.47 KB
/
Pointer implementation using c++
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
// A Program that exercise the operations on Stack
// Implementing POINTER (Linked Structures) (Dynamic Binding) // This program provides you the concepts that how STACK is
// implemented using Pointer/Linked Structures
#include <iostream.h.h>
#include <process.h> struct node {
int info;
struct node *next;
};
struct node *TOP = NULL; void push (int x)
{ struct node *NewNode;
NewNode = new (node); // (struct node *) malloc(sizeof(node));
if(NewNode==NULL) { cout<<"\n\n Memeory Crash\n\n";
return; }
NewNode->info = x;
NewNode->next = NULL;
if(TOP == NULL) TOP = NewNode;
else
{ NewNode->next = TOP; TOP=NewNode;
}
}
struct node* pop ()
{ struct node *T;
T=TOP;
TOP = TOP->next;
return T;
}
void Traverse()
{ struct node *T;
for( T=TOP ; T!=NULL ;T=T->next) cout<<T->info<<endl;
}
bool IsEmpty()
{ if(TOP == NULL) return true; else return false; } int main ()
{ struct node *T;
int item, ch;
while(1)
{ cout<<"\n\n\n\n\n\n ***** Stack Operations *****\n"; cout<<"\n\n 1- Push Item \n 2- Pop Item \n";
cout<<" 3- Traverse/Print stack-values\n 4- Exit\n\n"; cout<<"\n Your Choice --> ";
cin>>ch;
switch(ch)
{ case 1:
cout<<"\nPut a value: "; cin>>item;
Push(item);
break;
case 2:
if(IsEmpty()) {cout<<"\n\n Stack is Empty\n";
break;
}
T= Pop();
cout<< T->info <<"\n\n has been deleted \n";
break;
case 3:
if(IsEmpty()) {cout<<"\n\n Stack is Empty\n";
break;
}
Traverse();
break;
case 4:
exit(0);
} // end of switch block } // end of loop
return 0;
} // end of main function