forked from happy522/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-using-linkedlist
More file actions
102 lines (92 loc) · 2.51 KB
/
stack-using-linkedlist
File metadata and controls
102 lines (92 loc) · 2.51 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
/* head is first node that is null initially, it'll be first node and it'll point to null,
after adding second node, second node will point to first node and it'll be new head now.
it's diffrent than usaual where we tend to start with first node and first will point to
second and last node will point to null.*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node{ //created structure named as node
int data; //for storing data
struct Node* next; //for pointing to next noded
}*head; //pointer head for the node
void push(int data){
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
if(head==NULL){
temp -> next = NULL;
head = temp;
}
else{
temp->next = head;
head = temp;
}
printf("Element pushed");
}
void pop(){
if(head==NULL){
printf("\nStack is empty");
}
else{
printf("\nPopped element is %d",head->data);
head = head->next;
}
}
void peep(){
if(head==NULL){
printf("\nStack is empty");
}
else{
printf("\nPeeked element is %d",head->data);
}
}
void change(int ele){
int temp;
if(head==NULL){
printf("\nStack is empty");
}
else{
temp = head->data;
head->data = ele;
printf("\nElement %d is changed to %d",temp,head->data);
}
}
void display(){
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp = head;
if(head==NULL){
printf("Underflow");
}
else{
while(temp!=NULL){
printf("\nValue is %d",temp->data);
temp = temp->next;
}
}
}
int main(){
char yn;
int ch,ele;
do{
printf("\n 1: For Push\n 2: For Pop\n 3: For Peek\n 4: Change\n 5: Display\n Your choice:");
scanf("%d",&ch);
switch(ch){
case 1: printf("\nEnter Element you want to push: ");
scanf("%d",&ele);
push(ele);
break;
case 2: pop();
break;
case 3: peep();
break;
case 4: printf("\nEnter Element you want to change:");
scanf("%d",&ele);
change(ele);
break;
case 5: display();
break;
}
printf("\n\nDo you want to continue? Enter Y for yes: ");
scanf(" %c",&yn);
}while(yn=='Y'||yn=='y');
return 0;
}