-
Notifications
You must be signed in to change notification settings - Fork 0
/
beep.c
168 lines (167 loc) · 3.95 KB
/
beep.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node* previous;
int data;
struct node* next;
};
struct node* head=NULL,*start=NULL,*temp=NULL;
int addpos()
{
int data;
printf("\nEnter position to be node added :");
scanf("%d",&data);//scanf always end with a newline
//printf("\n %d",data);
return data;
}
int getdel()
{
int data;
printf("\nEnter data to be deleted in node :");
scanf("%d",&data);//scanf always end with a newline
//printf("\n %d",data);
return data;
}
int getdata()
{
int data;
printf("\nEnter data to be added in node :");
scanf("%d",&data);//scanf always end with a newline
//printf("\n %d",data);
return data;
}
int getoption()
{
int option;
printf("\nAppend an element to list : 1");
printf("\nAdd a node at specific position : 2");
printf("\nDelete an element from list by data : 3");
printf("\nDisplay list : 5");
printf("\n\nChoose option : ");
scanf("%d",&option);
return option;
}
void display_list(struct node *start)
{
printf("\n");
while(start!=NULL)
{
printf("%d",start->data);
if(start->next!=NULL)
printf("->");
start=start->next;
}
}
void addnode(struct node *new_node)
{
start=head;
if(head==NULL)
{
head=new_node;
start=new_node;
return;
}
while(start->next!=NULL)
{
start=start->next;
}
if(start->next==NULL)
{
start->next=new_node;
new_node->previous=start->next;
return;
}
}
void delnode(struct node *delete,int key)
{
if(delete != NULL && delete->data == key)//check whether key is present in head
{
head = delete->next; // Changed head
free(delete); // free old head
return;
}
while(delete != NULL && delete->data != key)//traversing to find the key
{
temp=delete;
delete=delete->next;
}
if(delete==NULL)
{
printf("\n\"the element is not found in list\"\n");
return;
}
temp->next = delete->next;
delete->next->previous=temp;//unlink thenode to be deleted
free(delete);//freeing memory not used
}
void posadd(struct node* posnode,int key)
{
start=head;
int i=0;
if(start==NULL && key==0)//if position is head node
{
head=posnode;
head->previous=NULL;
head->next=NULL;
return;
}
while(i<(key-1) && start!=NULL)
{
start=start->next;
i++;
}
if(start==NULL)
{
printf("\n\"position is out of scope\"\n");
return ;
}
temp=start->next;
start->next=posnode;
posnode->next=temp;
}
int main()
{
int data;
int pos;
while(1)
{
switch (getoption())
{
case 1:
data =getdata();
struct node *new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=data;
new_node->previous=NULL;
new_node->next=NULL;
addnode(new_node);
break;
case 2:
data =getdata();
pos =addpos();
struct node *posnode=(struct node*)malloc(sizeof(struct node));
posnode->data=data;
posnode->next=NULL;
posadd(posnode,pos);
break;
case 3:
data=getdel();
struct node* delete=(struct node*)malloc(sizeof(struct node));
start=head;
delete=start;
delnode(delete,data);
break;
case 5:
if(head==NULL)
{
printf("\nlist is empty,try creating a new list\n");
break;
}
printf("\n");
display_list(head);
break;
default :
exit(0);
}
}
}