-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked list stack
More file actions
95 lines (95 loc) · 1.29 KB
/
linked list stack
File metadata and controls
95 lines (95 loc) · 1.29 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
89
90
91
92
93
94
95
#include<stdio.h>
#include<stdlib.h>
struct node
{
int rollno;
char name[10];
struct node *next;
}*head=NULL,*current=NULL;
void push();
void pop();
void traverse();
void main()
{
int choice,w;
do
{
printf("1-push\n2-pop\n3-traverse\n");
printf("enter your choice");
scanf("%d",&choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
traverse();
break;
default:
printf("your choice is wrong\n");
break;
}
printf("do you want to continue if yes enter 1");
scanf("%d",&w);
}while(w==1);
}
void push()
{
struct node *newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the details of student\n");
printf("enter the name");
scanf("%s",newnode->name);
printf("enter the rollno");
scanf("%d",&newnode->rollno);
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
current=newnode;
}
else
{
current->next=newnode;
current=newnode;
}
}
void pop()
{
struct node *ptr=head;
if(head==NULL)
{
printf("the list is empty\n");
}
else if(ptr->next==NULL)
{
ptr=NULL;
printf("stack is empty\n");
}
else
{
while(ptr->next!=current)
{
ptr=ptr->next;
}
ptr->next=NULL;
}
}
void traverse()
{
struct node *ptr;
ptr=head;
if(head!=NULL)
{
printf("the details of students are\n");
while(ptr!=NULL)
{
printf("name-%s\nrollno-%d\n",ptr->name,ptr->rollno);
ptr=ptr->next;
}
}
else
printf("empty linked list\n");
}