1// A complete working C program
2// to demonstrate deletion in
3// singly linked list
4#include <stdio.h>
5#include <stdlib.h>
6
7// A linked list node
8struct Node {
9 int data;
10 struct Node* next;
11};
12
13/* Given a reference (pointer to pointer) to the head of a
14 list and an int, inserts a new node on the front of the
15 list. */
16void push(struct Node** head_ref, int new_data)
17{
18 struct Node* new_node
19 = (struct Node*)malloc(sizeof(struct Node));
20 new_node->data = new_data;
21 new_node->next = (*head_ref);
22 (*head_ref) = new_node;
23}
24
25/* Given a reference (pointer to pointer) to the head of a
26 list and a key, deletes the first occurrence of key in
27 linked list */
28void deleteNode(struct Node** head_ref, int key)
29{
30 // Store head node
31 struct Node *temp = *head_ref, *prev;
32
33 // If head node itself holds the key to be deleted
34 if (temp != NULL && temp->data == key) {
35 *head_ref = temp->next; // Changed head
36 free(temp); // free old head
37 return;
38 }
39
40 // Search for the key to be deleted, keep track of the
41 // previous node as we need to change 'prev->next'
42 while (temp != NULL && temp->data != key) {
43 prev = temp;
44 temp = temp->next;
45 }
46
47 // If key was not present in linked list
48 if (temp == NULL)
49 return;
50
51 // Unlink the node from linked list
52 prev->next = temp->next;
53
54 free(temp); // Free memory
55}
56
57// This function prints contents of linked list starting
58// from the given node
59void printList(struct Node* node)
60{
61 while (node != NULL) {
62 printf(" %d ", node->data);
63 node = node->next;
64 }
65}
66
67// Driver code
68int main()
69{
70 /* Start with the empty list */
71 struct Node* head = NULL;
72
73 push(&head, 7);
74 push(&head, 1);
75 push(&head, 3);
76 push(&head, 2);
77
78 puts("Created Linked List: ");
79 printList(head);
80 deleteNode(&head, 1);
81 puts("\nLinked List after Deletion of 1: ");
82 printList(head);
83 return 0;
84}
1#include<conio.h>
2#include<stdio.h>
3
4struct node
5{
6 int i;
7 struct node *next;
8};
9
10void main()
11{
12
13 struct node *first;
14 struct node *last;
15
16 struct node *temp;
17 int ch,user,add,cnt=0,t=0;
18 struct node *p;
19
20 clrscr();
21
22 printf("\n\t 1.CREATION");
23 printf("\n\t 2.INSERT AT STARTING");
24 printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)");
25 printf("\n\t 4.INSERT AT END");
26 printf("\n\t 5.DELETE 1ST NODE");
27 printf("\n\t 6.DELETE LAST NODE");
28 printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)");
29 printf("\n\t 8.DISPLAY");
30 printf("\n\t 10.EXIT");
31 scanf("%d",&user);
32
33 while(user!=10)
34 {
35 if(user==1)
36 {
37 printf("\n\t ENTER DATA ::: ");
38 first=(struct node*)malloc(sizeof(struct node));
39 scanf("%d",&ch);
40
41 first->i=ch;
42 first->next=0;
43 p=first;
44 cnt=1;
45 }
46 if(user==2)
47 {
48 p=(struct node*)malloc(sizeof(struct node));
49 printf("\n\t ENTER DATA FOR 1ST NODE");
50 scanf("%d",&p->i);
51 p->next=first;
52 first=p;
53 cnt++;
54 }
55 if(user==4)
56 {
57 p=(struct node*)malloc(sizeof(struct node));
58 printf("\n\t ENTER DATA FOR LAST NODE");
59 scanf("%d",&p->i);
60 temp=first;
61 while(temp->next!=0)
62 {
63 temp=temp->next;
64 }
65 temp->next=p;
66 p->next=0;
67 cnt++;
68 }
69 if(user==3)
70 {
71 printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt);
72 scanf("%d",&add);
73
74 t=1;
75 p=first;
76 while(t!=add)
77 {
78 p=p->next;
79 t++;
80 }
81 temp=(struct node*)malloc(sizeof(struct node));
82 printf("\n\t ENTER DATA FOR NODE");
83 scanf("%d",&temp->i);
84 temp->next=p->next;
85 p->next=temp;
86 cnt++;
87 }
88 if(user==5)
89 {
90 p=first;
91 first=p->next;
92 free(p);
93 }
94 if(user==6)
95 {
96 p=first;
97 while(p->next->next!=0)
98 {
99 p=p->next;
100 }
101 p->next=0;
102 free(p->next->next);
103 }
104 if(user==8)
105 {
106 p=first;
107 while(p!=0)
108 {
109 printf("\n\t %d",p->i);
110 p=p->next;
111 }
112 }
113 if(user==7)
114 {
115 printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt);
116 scanf("%d",&add);
117
118 t=1;
119 p=first;
120 while(t<add-1)
121 {
122 p=p->next;
123 t++;
124 }
125 temp=p->next;
126 p->next=temp->next;
127 free(temp);
128 cnt--;
129
130 }
131 printf("\n\t 1.CREATION");
132 printf("\n\t 2.INSERT AT STARTING");
133 printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)");
134 printf("\n\t 4.INSERT AT END");
135 printf("\n\t 5.DELETE 1ST NODE");
136 printf("\n\t 6.DELETE LAST NODE");
137 printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)");
138 printf("\n\t 8.DISPLAY");
139 printf("\n\t 10.EXIT");
140 scanf("%d",&user);
141 }
142 getch();
143}
1typedef struct node{
2 int value; //this is the value the node stores
3 struct node *next; //this is the node the current node points to. this is how the nodes link
4}node;
5
6node *rmvNode(node *head, int index){
7 node *tmp = head;
8 node *rmv;
9 int count = 0;
10
11 //base case for if the user enters a value greater then or equal to the length of the head
12 //base case for if the user enters a value with a list of length 1. because in this library a list MUST contain one value minimum
13 if(index >= len(head) || len(head) == 1){
14 return NULL;
15 }
16
17 //if you want to remove the first value
18 if(index == 0){
19 rmv = head; //stores the head at this given moment in time
20 head = tmp->next; //this jumps the position of the head making sure that the beginning is no longer part of the head
21 free(rmv); //this frees the memory given to the initial head
22 return head;
23 }
24
25 //if you want to remove index position 1
26 if(index == 1){
27 rmv = head->next;
28 head->next = tmp->next->next;
29 free(rmv);
30 return head;
31 }
32
33 //if you want to remove the last value
34 if(index == -1){
35
36 while(count < len(head)-2){ //we do -2 because we want to access the node before the last one
37 tmp = tmp->next;
38 count += 1;
39 }
40 rmv = tmp->next;
41 tmp->next = NULL;
42 free(rmv);
43 return head;
44 }
45
46 //remove anything else
47 while(count < index-1){
48 tmp = tmp->next;
49 count += 1;
50 }
51 rmv = tmp->next;
52 tmp->next = tmp->next->next;
53 free(rmv);
54 return head;
55
56}