Repository navigation
Expand file tree
/
Copy pathA9a_CLLstack.c
More file actions
88 lines (77 loc) · 1.61 KB
/
Copy pathA9a_CLLstack.c
File metadata and controls
88 lines (77 loc) · 1.61 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
86
87
88
/* Implement stack using Circular Linked List. */
#include<stdlib.h>
#include<stdio.h>
typedef struct cllstack {
int data;
struct cllstack * ptr;
}cllstack;
cllstack * head = NULL;
cllstack * createnode() {
cllstack * newnode = (cllstack *)malloc(sizeof(cllstack));
printf("Enter the data element : ");
scanf("%d" , &newnode->data);
newnode->ptr = NULL;
return newnode;
}
void push() {
if(head == NULL) {
head = createnode();
head->ptr = head;
}
else {
cllstack * temp = createnode();
temp->ptr = head->ptr;
head->ptr = temp;
}
printf("Element successfully pushed.\n");
}
void pop() {
if(head == NULL)
printf("Stack is empty.\n");
else if(head->ptr == head) {
printf("Element %d successfully popped out.\n" , head->data);
free(head);
head = NULL;
}
else {
cllstack * temp = head->ptr;
printf("Element %d successfully deleted.\n" , temp->data);
temp = temp->ptr;
head->ptr = temp;
}
}
void display() {
if(head == NULL)
printf("Stack is empty.\n");
else {
cllstack * strt = head->ptr ;
printf("Elements of stack are : ");
while(strt != head) {
printf("%d " , strt->data);
strt = strt->ptr;
}
printf("%d " , strt->data);
printf("\n");
strt = NULL;
}
}
void main() {
int choice , flag = 1 ;
printf("Enter the operation to be performed.\n");
while(flag) {
printf("1.Push \t 2.Pop \t 3.Display \t 4.Exit : ");
scanf("%d" , &choice);
switch(choice) {
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : flag = 0 ;
break;
default : printf("Invalid input.\n");
break;
}
}
}