-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_using_LinkedList2.cpp
121 lines (115 loc) · 2.1 KB
/
stack_using_LinkedList2.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
using namespace std;
class Node
{
public:
int data;
class Node *next;
};
class Node *top = NULL;
int isEmpty(class Node *ptr)
{
if (ptr == NULL)
{
//cout<<"Stack is Full";
return 1;
}
else
{
return 0;
}
}
int isFull(class Node *ptr)
{
class Node *newNode = (class Node *)malloc(sizeof(class Node));
if (newNode == NULL)
{
//cout<<"Stack is Full";
return 1;
}
else
{
return 0;
}
}
class Node *push(class Node *ptr, int value)
{
if (isFull(ptr))
{
cout << "Stack Overflow" << endl;
}
else
{
class Node *newNode = (class Node *)malloc(sizeof(class Node));
newNode->data = value;
newNode->next = ptr;
ptr = newNode;
return ;
}
}
void linkedListTraversal(class Node *ptr)
{
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
void peek(class Node* ptr , int position)
{ int counter = 1 ;
while(ptr != NULL)
{
if (counter == position)
{
cout<<"Element is :"<<ptr->data<<endl;
break ;
}
counter++ ;
ptr = ptr->next;
}
}
void top_of_stack(class Node* ptr)
{
cout<<"Top element is :"<<ptr->data<<endl;
}
void bottom_of_stack(class Node* ptr)
{
while (ptr != NULL)
{
if (ptr->next == NULL)
{
cout<<"Bottom element is :"<<ptr->data<<endl;
}
ptr = ptr->next;
}
}
int pop(class Node *tp)
{
if (isEmpty(tp))
{
cout << "Stack Underflow";
return -1;
}
else
{
class Node* temp = tp;
top = (tp)->next;
int data = temp->data;
free(temp);
return data;
}
}
int main()
{
top = push(top, 78);
top = push(top, 7);
top = push(top, 8);
// linkedListTraversal(top);
// int element = pop(top);
// printf("Popped element is %d\n", element);
// linkedListTraversal(top);
peek(top , 2);
//top_of_stack(top);
//bottom_of_stack(top);
return 0;
}