-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackLinkedList.c
135 lines (129 loc) · 2.59 KB
/
StackLinkedList.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//StackLinkedList
//
// main.c
// StackLinkedList
//
// Created by test on 27/06/19.
// Copyright © 2019 test. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
struct node *prev;
};
struct node *top = NULL;
void push();
void topp();
void displayList();
int size();
void pop();
void isEmpty();
int main() {
int ch;
while(1)
{
printf("Enter Your option:\n");
printf("1 to insert data\n2 to print top data\n3 to display list of data\n4 to print size\n5 to remove top element\n6 to check if the list is empty or not\n7 to exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:topp();
break;
case 3:displayList();
break;
case 4:size();
printf("Number of data are :%d \n",size());
break;
case 5:pop();
break;
case 6:isEmpty();
break;
case 7:exit(1);
break;
default:printf("Enter a valid option\n");
break;
}
}
return 0;
}
void push ()
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
printf("Enter data of the node:\n");
scanf("%d",&temp -> data);
// temp -> next = NULL;
temp -> next = top;
top = temp;
printf("Data %d is inserted successfully\n",temp->data);
}
void topp()
{
if (top == NULL){
printf("Empty List\n");
}
else
{
printf("%d\n",top -> data);
}
}
int size()
{
int count = 0;
struct node *temp = top;
if (top == NULL)
{
printf("List is Empty:\n");
}
else
{
while (temp != NULL)
{
temp = temp -> next;
count++;
}
}
return count;
}
void displayList()
{
struct node *temp = top;
int length = size();
if(temp == NULL){
printf("List is Empty:\n\n");
}
else
{
while(length--)
{
printf("%d ",temp -> data);
temp = temp -> next;
}
printf("\n\n");
}
}
void pop(){
if(top == NULL){
printf("List is EMPTY , 1st Enter Data then Pop\n");
}
else{
printf("Top Data %d Deleted Successfully\n",top -> data);
struct node *temp = top;
top = temp -> next;
temp -> next = NULL;
free(temp);
}
}
void isEmpty()
{
if(top == NULL){
printf("Empty\n");
}
else
{
printf("Not Empty\n");
}
}