-
Notifications
You must be signed in to change notification settings - Fork 0
/
STACK_IMPLEMENTATION_LINKEDLIST.c
110 lines (93 loc) · 1.98 KB
/
STACK_IMPLEMENTATION_LINKEDLIST.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
106
107
108
109
110
//STACK IMPLEMENTATION USING SINGLE LINKED LIST
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
// Defining struct
struct node{
int data;
struct node *next;
};
struct node *head;
void display(){
if(!head){
printf("\nStack is empty\n");
}
else{
struct node *temphead;
temphead=head;
printf("\n***STARTED PRINTING DATA*********\n\n");
while(temphead){
printf("%d\n",temphead->data);
temphead=temphead->next;
}
printf("\n******ALL DATA PRINTED******\n\n");
}
};
void insert(int data){
struct node *temp,*temphead;
temp=(struct node *)malloc(sizeof(struct node *));
temp->data=data;
temp->next=NULL;
if(!head){
head=temp;
}
else{
temphead=head;
while(temphead->next!=NULL){
temphead=temphead->next;
}
temphead->next=temp;
}
};
void pop(){
struct node *last,*secondlast;
last=head;
secondlast=head;
if(!last){
printf("\nStack is empty, nothing to delete\n");
}
else if(last->next==NULL){
printf("\ndeleted data is:%d\n\n",last->data);
free(last);
head=NULL;
}
else{
while(last->next!=NULL){
last=last->next;
}
while(secondlast->next!=last){
secondlast=secondlast->next;
}
secondlast->next=NULL;
printf("\ndeleted data is:%d",last->data);
free(last);
}
};
void main(){
while(1){
printf("\n**************CHOOSE A OPTION**********************\n");
printf("\n****** 0 : Exit***************");
printf("\n****** 1 : Insert*************");
printf("\n****** 2 : Delete*************");
printf("\n****** 3 : Display************");
printf("\n Type your choice: ");
int choice;
scanf("%d",&choice);
switch(choice){
case 0:printf("\nExiting");
return;
case 1: printf("\nWhat to insert? ");
int num;
scanf("%d",&num);
insert(num);
printf("\nInserted successfully");
break;
case 2:pop();
break;
case 3:display();
break;
default:printf("\nWrong choice");
return;
}
}
};