-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathStack(LL).cpp
More file actions
85 lines (77 loc) · 1.35 KB
/
Stack(LL).cpp
File metadata and controls
85 lines (77 loc) · 1.35 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
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int data;
struct stack* prev;
};
struct stack *top=NULL;
//pop
void pop(struct stack *ptr)
{
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=top;
top=top->prev ;
printf("\nThe deleted element is :%d\t",ptr->data);
free(ptr);
}
}
//push
void push(struct stack *ptr, int data)
{
struct stack *temp=(struct stack*)malloc(sizeof(struct stack));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data:\t" );
scanf("%d",&temp->data);
temp->prev =NULL;
if(top==NULL)
top=temp;
else
{
temp->prev=top;
top=temp;
}
}
//print
void printStack(struct stack *ptr)
{
if(top==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=top;
printf("\nThe List elements are:\n\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->prev;
}
}
}
//main
int main()
{
struct stack *ptr;
int n;
ptr=(struct stack*)malloc(sizeof(struct stack));
for(int i=0;i<5;i++)
push(ptr,n);
printStack(ptr);
push(ptr,6) ;
printStack(ptr);
pop(ptr);
printStack(ptr);
}