Skip to content

Commit ebc7892

Browse files
authored
fulls source code for insertion and deletion in singly linked list
fulls source code for insertion and deletion in singly linked list in c
1 parent 2d376fb commit ebc7892

File tree

1 file changed

+76
-45
lines changed

1 file changed

+76
-45
lines changed

Data Structures/linked_list/singly_link_list_deletion.c

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,96 @@
33
function can be modified according to the data type, easily.
44
deleteNode deletes a node when passed with a key of the node.
55
*/
6-
#include <stdio.h>
7-
#include <stdlib.h>
8-
9-
struct Node
10-
{
11-
int data;
12-
struct Node *next;
6+
#include<stdio.h>
7+
struct node
8+
{int info;
9+
struct node *link;
1310
};
14-
15-
void push(struct Node** head_ref, int new_data)
11+
struct node *start=NULL;
12+
///////////////////////////////////////////////////////////
13+
struct node * createnode()//function to create node
1614
{
17-
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
18-
new_node->data = new_data;
19-
new_node->next = (*head_ref);
20-
(*head_ref) = new_node;
15+
struct node *t;
16+
t=(struct node*)malloc(sizeof(struct node));
17+
return(t);
2118
}
22-
23-
void deleteNode(struct Node **head_ref, int key)
19+
////////////////////////////////////////////////////////
20+
void insert()//function to insert at first location
2421
{
25-
struct Node* temp = *head_ref, *prev;
26-
27-
if (temp != NULL && temp->data == key)
22+
struct node *p;
23+
p=createnode();
24+
printf("\nenter the number to insert");
25+
scanf("%d",&p->info);
26+
p->link=NULL;
27+
if(start==NULL)
28+
{
29+
start=p;
30+
}
31+
else
32+
{
33+
p->link=start;
34+
start=p;
35+
}
36+
}
37+
///////////////////////////////////////////////////////////
38+
void deleteion()//function to delete from first position
39+
{
40+
struct node *t;
41+
if(start==NULL)
2842
{
29-
*head_ref = temp->next;
30-
free(temp);
31-
return;
43+
printf("\nlist is empty");
3244
}
33-
34-
35-
while (temp != NULL && temp->data != key)
45+
else
3646
{
37-
prev = temp;
38-
temp = temp->next;
47+
struct node *p;
48+
p=start;
49+
start=start->link;
50+
free(p);
3951
}
40-
41-
if (temp == NULL) return;
42-
43-
prev->next = temp->next;
44-
45-
free(temp);
4652
}
47-
48-
void printList(struct Node *node)
53+
///////////////////////////////////////////////////////
54+
void viewlist()//function to display values
4955
{
50-
while (node != NULL)
56+
struct node *p;
57+
if(start==NULL)
5158
{
52-
printf(" %d ", node->data);
53-
node = node->next;
59+
printf("\nlist is empty");
60+
}
61+
else
62+
{ p=start;
63+
while(p!=NULL)
64+
{
65+
printf("%d ",p->info);
66+
p=p->link;
67+
}
5468
}
5569
}
70+
//////////////////////////////////////////////////////////////////////
5671

5772
int main()
5873
{
59-
/* new node can be created here as :-
60-
struct Node* head = NULL;
61-
push(&head, data);
62-
63-
and a node can be delete by using
64-
deleteNode(&head, key);
65-
*/
66-
return 0;
74+
int n;
75+
while(1)
76+
{
77+
printf("\n1.add value at first location");
78+
printf("\n2.delete value from first location");
79+
printf("\n3.view value");
80+
printf("\nenter your choice");
81+
scanf("%d",&n);
82+
switch(n)
83+
{
84+
case 1:
85+
insert();
86+
break;
87+
case 2:
88+
deleteion();
89+
break;
90+
case 3:
91+
viewlist();
92+
break;
93+
default:
94+
printf("\ninvalid choice");
95+
}
96+
}
97+
return(0);
6798
}

0 commit comments

Comments
 (0)