forked from aman-raza/Friends_Hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deletionOfList.c
120 lines (109 loc) · 2.68 KB
/
deletionOfList.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
struct node *createNode(int a){
struct node *p= (struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=NULL;
return p;
}
void addNodeAtTheEnd(int n) {
struct node *c = createNode(n);
struct node *ptr;
for(ptr = head;ptr->next!=head; ptr = ptr->next);
ptr->next = c;
c->next = head;
}
void Print(struct node *p){
if(p->next==head){
printf("You have only one element %d",p->data);
exit(0);
}
printf("%d\t",p->data);
while (p->next!=head)
{
p=p->next;
printf("%d\t",p->data);
}
}
void deleteAtEnd(){
printf("\nNow last element will be deleted!\n");
struct node *p;
struct node *q;
for(p = head,q =head;p->next!=head;q=p,p = p->next);
q->next = head;
free(p);
}
void deleteAtBeginning(){
printf("\nNow first element will be deleted!\n");
struct node *q=head,*p=head;
head= head->next;
while(q->next!=p)
q=q->next;
q->next = head;
free(p);
}
void deleteAtMid(){
int pos;
printf("\nEnter the position of element to be deleted after head!\n");
scanf("%d",&pos);
struct node *p=head;
struct node *q;
for(int i=0;i<pos;i++){
q=p;
p=p->next;
}
q->next = p->next;
free(p);
}
int main(){
int res;
printf("Enter your number of nodes!\n");
scanf("%d",&res);
printf("Enter the values of your list!\n");
int h;
scanf("%d", &h);
head = createNode(h);
head->next = head;
for(int i=1;i<res;i++){
int num;
scanf("%d",&num);
addNodeAtTheEnd(num);
}
Print(head);
int choice;
char ch= 'y';
while(ch == 'y'|| ch =='Y'){
printf("\nEnter 1 to delete the first !!");
printf("\nEnter 2 to delete the last !!");
printf("\nEnter 3 to delete node with a index!");
printf("\nEnter your choice!!\n");
int flag=1;
do{
flag=1;
scanf("%d",&choice);
switch(choice){
case 1: deleteAtBeginning();
Print(head);
break;
case 2: deleteAtEnd();
Print(head);
break;
case 3 :deleteAtMid();
Print(head);
break;
default: printf("\nEnter a valid choice!!\n");
flag=0;
break;
}
}while(flag==0);
printf("\nDo you wish to delete more??\n");
scanf(" %c",&ch);
}
return 0;
}