-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_link_list_and_add_delete_from_first.c
100 lines (99 loc) · 1.54 KB
/
create_link_list_and_add_delete_from_first.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
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int data;
struct node *next;
}*start;
void create(int);
void display();
void first_add(int);
int first_delete();
int main()
{
int num,i,choice,pos;
start=NULL;
do
{
printf(" \n1.create 2.display 3.add 4 delete 5.Exit \n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter item to be add in a list\n");
scanf("%d",&num);
create(num);
break;
case 2:
display();
break;
case 3:printf("Add newnode and item in begning of list\n");
scanf("%d",&num);
first_add(num);
break;
case 4:
printf("Your deleted item is : %d ",first_delete());
break;
case 5:exit(0);
default: printf("wrong choice\n");
}
}while(1);
}
void create(num)
{
struct node *q,*t;
if(start==NULL)
{
q=(struct node*)malloc(sizeof(struct node));
q->data=num;
q->next=NULL;
start=q;
}
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
t=(struct node *)malloc(sizeof(struct node));
t->data=num;
t->next=NULL;
q->next=t;
}
}
void display()
{
struct node *q;
q=start;
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->next;
}
}
void first_add(int num)
{
struct node*q;
q=(struct node*)malloc(sizeof(struct node));
q->data=num;
q->next=start;
start=q;
}
int first_delete()
{
struct node *q,*t;
q=start;
if(q==NULL)
{
printf("List is Empty\n");
return;
}
start=start->next;
q->next=NULL;
int num=q->data;
free(q);
return num;
}