-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack_operations.c
105 lines (105 loc) · 2.37 KB
/
stack_operations.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
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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
struct node* prev;
};
struct node *create(struct node *start)
{
struct node *newnode, *ptr;
int info;
scanf("%d",&info);
while(info!=-1)
{
newnode=(struct node *)malloc(sizeof(struct node *));
newnode->data=info;
if(start==NULL)
{
start=newnode;
printf("enter -1 to end\n");
scanf("%d",&info);
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
newnode->next=NULL;
printf("enter -1 to end\n");
scanf("%d",&info);
}
}
return start;
}
void display(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void insert_beg(struct node *start)
{
struct node *ptr, *newnode;
int info;
printf("enter the insertion value\n");
scanf("%d",&info);
newnode=(struct node *)malloc(sizeof(struct node *));
newnode->data=info;
newnode->next=start;
start=newnode;
}
void insert_end(struct node *start)
{
struct node *ptr, *newnode;
int info;
printf("enter the insertion value\n");
scanf("%d",&info);
newnode=(struct node *)malloc(sizeof(struct node *));
newnode->data=info;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode;
newnode->prev=ptr;
newnode->next=NULL;
}
void insert_at(struct node *start)
{
struct node *ptr, *newnode, *pre;
int info,val;
printf("enter the insertion value\n");
scanf("%d",&info);
newnode=(struct node *)malloc(sizeof(struct node *));
newnode->data=info;
printf("enter value after which insertion has to be done\n");
scanf("%d",&val);
ptr=start;
pre=ptr;
while(pre->data!=val)
{
pre=ptr;
ptr=ptr->next;
}
pre->next=newnode;
newnode->prev=pre;
newnode->next=ptr;
ptr->next=newnode;
}
int main()
{
struct node *start=NULL;
start=create(start);
insert_beg(start);
display(start);
/*insert_end(start);
insert_end(start);
display(start);*/
return 0;
}