-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program - 2.c
100 lines (95 loc) · 2.29 KB
/
Program - 2.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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
struct node
{ int data;
struct node *nxt;
}*head,*newnode,*prenode,*ptr;
void llmake()
{ int i=1;
char j;
head=(struct node *)malloc(sizeof(struct node));
printf("Input data for node %d : ",i);
scanf("%d",&head->data);
head->nxt=NULL;
prenode=head;
printf("\tWant to enter more ? (y/n) : ");
scanf("%s",&j);
while(j=='y')
{ i++;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data for node %d : ",i);
scanf("%d",&newnode->data);
newnode->nxt=NULL; //here nxt=next node pointor
prenode->nxt=newnode; //here nxt=next node pointor
prenode=newnode;
printf("\tWant to enter more ? (y/n) : ");
scanf("%s",&j);
}
}
void displayll()
{ int i=1;
ptr=head;
printf("\n\nLinked list is :-\n\n");
printf("----------------------------------------------------\n");
do
{ printf("Node - %d | Data - %d | Address - %d | Next - %d \n",
i,ptr->data,ptr,ptr->nxt);
printf("----------------------------------------------------\n");
ptr=ptr->nxt;
i++;
delay(750);
}while(ptr!=NULL);
}
int deletion()
{ int del;
char c;
do
{ printf("\n\nEnter data to delete : ");
scanf("%d",&del);
prenode=head;
do
{ if(head==NULL)
{ printf("\nList is empty !!!\n");
return 0;
}
else if(prenode->data==del)
{ head=prenode->nxt; //here nxt=next node pointor
printf("Node with matching data deleted successfully.\n");
if(head==NULL)
{ printf("List is now empty !\n");
return 0;
}
break;
}
else if((prenode->nxt)->data==del)
{ prenode->nxt=(prenode->nxt)->nxt;
printf("Node with matching data deleted successfully.\n");
if(head==NULL)
{ printf("List is now empty !\n");
return 0;
}
break;
}
else
{ prenode=prenode->nxt; //here nxt=next node pointor
if(prenode==NULL)
{ printf("\nElement not found !!!\n"); }
}
}while(prenode!=NULL);
displayll();
printf("Want to delete more ? (y/n) : ");
scanf("%s",&c);
}while(c=='y');
return 0;
}
void main()
{ clrscr();
printf("Enter data :-\n\n");
llmake();
displayll();
deletion();
printf("Press any key to EXIT ...");
getch();
}