-
Notifications
You must be signed in to change notification settings - Fork 2
/
DSA_Ass2_1.c
123 lines (120 loc) · 2.28 KB
/
DSA_Ass2_1.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
struct node* Create_node()
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
n->next=NULL;
return n;
}
void insert()
{
struct node *t,*p;
t=Create_node();
printf("Enter Data :");
scanf("%d",&t->data);
if(start==NULL)
{
start=t;
}
else
{
p=start;
while(p->next!=NULL)
{
p=p->next;
}
p->next=t;
}
}
void swap()
{
int m,n;
struct node *t,*r,*prev1,*prev2;
printf("Enter Two Elements to Be Swapped :");
scanf("%d%d",&n,&m);
if(start==NULL)
printf("\nList Is Empty");
else if(n==m)
{
printf("\nSwapping Not Possible for single element\n");
}
else
{
t=start;
r=start;
while(t->data!=n)
{
prev1=t;
t=t->next;
if(t==NULL)
{
printf("\nUnable to Swap due to less Data Available\n");
return;
}
}
while(r->data!=m)
{
prev2=r;
r=r->next;
if(r==NULL)
{
printf("\nUnable to swap due to less Data Available\n");
return;
}
}
prev1->next=t->next;
prev2->next=r->next;
r->next=prev1->next;
prev1->next=r;
t->next=prev2->next;
prev2->next=t;
}
}
void view_list()
{
struct node *t;
if(start==NULL)
printf("\nList Is Empty\n");
else
{
t=start;
while(t!=NULL)
{
printf("%d\n",t->data);
t=t->next;
}
}
}
int main()
{
int choice;
printf("1.Insert_node");
printf("\n2.Swap Links");
printf("\n3.View List");
printf("\n4.Exit\n");
while(1)
{
printf("\nEnter Your Choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
swap();
break;
case 3:
view_list();
break;
case 4:
exit(0);
}
}
}