-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
69 lines (63 loc) · 1.19 KB
/
Stack.c
File metadata and controls
69 lines (63 loc) · 1.19 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
#include<stdio.h>
#include<stdlib.h>
typedef struct Stack{
int data;
struct Stack *next;
}stack;
stack *top=NULL;
void traversal(){
stack *ptr=top;
while(ptr!=NULL){
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
void push(int data){
stack *newstack=(stack*)malloc(sizeof(stack));
newstack->data=data;
newstack->next=NULL;
if(top==NULL){
top=newstack;
}
else{
stack *current=top;
while(current->next!=NULL){
current=current->next;
}
current->next=newstack;
}
}
void pop(){
if(top==NULL){
printf("Stack is empty\n");
return;
}else{
stack *current=top;
stack *delete=current->next;
while(delete->next!=NULL){
current=current->next;
delete=delete->next;
}
current->next=delete->next;
free(delete);
}
}
int main(){
int n,x;
printf("How many data you insert ? : ");
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&x);
push(x);
}
traversal();
printf("How many data you delete ? = ");
int y;
scanf("%d",&y);
for(int i=1;i<=y;i++){
pop();
}
traversal();
return 0;
}