-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.c
139 lines (134 loc) · 2.38 KB
/
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
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
136
137
138
139
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node* create(int n);
void print(node *Head);
void count(node *Head);
void search(node *Head);
int main()
{
node *Head;
Head=NULL;
int N,ch;
printf("\n how much node linked list do you want to create :");
scanf("%d",&N);
Head=create(N);
print(Head);
printf("1.print a linked list!!");
printf("2.count a element in linked list!!");
printf("3.search a element in linked list!!");
printf("4.thank you for using a software!!");
do{
printf("\n enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:void print(node *Head);
break;
case 2:void count(node *Head);
break;
case 3:void search(node *Head);
break;
case 4:printf("\nexit");
break;
default:printf("\n invalid choice");
}while(ch!=4)
return 0;
}
node* create(int n)
{
node *Head,*p;
int x,i;
printf("\n enter the data :");
scanf("%d",&x);
Head=(node*)malloc(sizeof(node));
Head->data=x;
Head->next=NULL;
p=Head;
for(i=2;i<=n;i++)
{
printf("\n enter the data :");
scanf("%d",&x);
p->next=(node*)malloc(sizeof(node));
p=p->next;
p->data=x;
p->next=NULL;
}
printf("\n create a linked list successfully!!!");
return (Head);
}
void print(node *Head)
{
node *p;
if(Head==NULL)
{
printf("\nlinked list is empty!!!");
}
else
{
p=Head;
printf("\nsingly linked list nodes :");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
}
}
void count(node *Head)
{
node *p;
int count=0;
if(Head==NULL)
{
printf("\sinngly linked list is empty!!!");
}
else
{
p=Head;
printf("\nsingly linked list nodes :");
while(p!=NULL)
{
count++;
p=p->next;
}
printf("\n no. of nodes in singly linked list :%d",count);
}
}
void search(node *Head)
{
node *p;
int key,flag=0;
printf("\n enter the element to be search :");
scanf("%d",&key);
if(Head==NULL)
{
printf("\n singly linked list is empty !!!");
}
else
{
p=Head;
orintf("\nsingly linked list nodes :");
while(p!=NULL)
{
if(key==p->data)
{
flag=1;
break;
}
p=p->next;
}
if(flag==1)
{
printf("\n element is found!!");
}
else
{
printf("\n element is not found !!");
}
}
}