You are on page 1of 22

Simple Linked List.

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

//Simple Linked List with insert, delete and reverse functions.


#include <stdio.h>
#include<stdlib.h>
struct node {int info; struct node *link;};
typedef struct node node;
//-------------------NODE GENERATOR-----------------node* makenew()
{
node *p= (node*)malloc(sizeof(node));
return p;
}
//--------------------HEADMAKER----------------node* headmaker(node *head,int x)
{
if (head==NULL)
{head=makenew();
head->info=x;}
return head;
}
//-----------INSERT AFTER-------------node* insertafter(node * head, int x, int y)
{
node *p,*q;
p=head;
q=makenew();
q->info=y;
if (x==-1)
{
q->link=head;
head=q;
return head;
}
while (p!=NULL)
{
if (p->info==x)//found element where insertion is supposed to be done
{
q->link=p->link;
p->link=q;
return head;
}
p=p->link;
}
}
//-------------DISPLAY------------------------void display(node *head)
{
node *p=head;
while (p!=NULL)
{
printf("%d --> ",p->info);
p=p->link;
}
}
//-------------DELETION--------------------node *del(node *head,int x)
{
node *q;
node *p=head;
if (p->info==x)

63 {
64
q=head;
65
head=p->link;
66
free (q);
67
return head;
68 }
69 while (p!=NULL)
70 {
71 if (p->link->info==x){
72 q=p->link;
73 p->link=p->link->link;
74 free(q);
75 return head;
76 }
77 p=p->link;
78 }
79 }
80 //-------------------REVERSE THE LIST--------------------81 node * reverse(node * head)
82 {
83 node *p=head,*q=NULL,*r;
84 while (p!=NULL)
85 {
86 r=p->link;
87 p->link=q;
88 q=p;
89 p=r;
90 }
91 head=q;
92 printf("\nReversed\n");
93 return head;
94 }
95 //-------------MAIN FUNCTION-----------------96 void main ()
97 {
98 node *head;
99 int ch,a,e;
100 head=NULL;
101
102 do {
103
printf("\n\n----Menu----\n1-Insert after\t2-Insert
Display\n5-Exit\t\t6-Reverse\n\n");
104
scanf ("%d",&ch);
105
if (!(ch==4||ch==5||ch==6)){
106
printf ("Enter the element to insert/delete\n");
107
scanf ("%d",&e);}
108
if (head==NULL&&ch!=4&&ch!=5&&ch!=6)
109
{
110
head=headmaker(head,e);
111
printf("First element inserted!\n");
112
continue;
113
}
114
switch (ch)
115
{
116
case 1:
117
printf("Insert after?\n");
118
scanf ("%d",&a);
119
head=insertafter(head,a,e);
120
break;
121
case 3:
122
head=del(head,e);
123
break;
124
case 4:
125
display(head);
126
break;

Before\n3-Delete\t\t4-

127
128
129
130
131
132
133
134
135

case 6:
head=reverse(head);
break;
default:
printf("Exiting\n");
break;
}
}while (ch!=5);

Output:
----Menu---1-Insert after
3-Delete
5-Exit

2-Insert Before
4-Display
6-Reverse

1
Enter the element to insert/delete
5
First element inserted!

----Menu---1-Insert after
3-Delete
5-Exit

2-Insert Before
4-Display
6-Reverse

1
Enter the element to insert/delete
7
Insert after?
5

----Menu---1-Insert after
3-Delete
5-Exit
4
5 --> 7 -->
----Menu---1-Insert after
3-Delete
5-Exit

2-Insert Before
4-Display
6-Reverse

2-Insert Before
4-Display
6-Reverse

1
Enter the element to insert/delete
10
Insert after?
5

----Menu---1-Insert after
3-Delete
5-Exit

2-Insert Before
4-Display
6-Reverse

4
5 --> 10 --> 7 -->
----Menu---1-Insert after
3-Delete
5-Exit

2-Insert Before
4-Display
6-Reverse

6
Reversed

----Menu---1-Insert after
3-Delete
5-Exit

2-Insert Before
4-Display
6-Reverse

4
7 --> 10 --> 5 -->
----Menu---1-Insert after
3-Delete
5-Exit

2-Insert Before
4-Display
6-Reverse

Binary Search
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

#include <stdio.h>
void main ()
{
int a[100],m,l=0,h,i,c=0,n,p,j,t;
printf ("Enter the limit\n");
scanf ("%d",&n);
printf ("Enter the array elements\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for (i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("\n------Sorted array is-------\n");
for (i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
printf ("enter the element to search\n");
scanf("%d",&p);
h=n-1;
t=0;
while(l<=h)
{
m=(l+h)/2;
c++;
if(a[m]==p)
{
printf("Element found at %d\nComaprisions:%d\n",m+1,c);
t=1;
break;
}
else if (a[m]<p)
l=m+1;
else
h=m-1;
}
if (t==0)
printf("No match found!\n");
}

Output:
Enter the limit
4

Enter the array elements


2
5
7
9

------Sorted array is------2 5 7 9


enter the element to search
5
Element found at 2
Comaprisions:1

Magic Square
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

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, c, a[9][9] ;
printf("Enter the size of the magic square : ") ;
scanf("%d", &n) ;
if (n % 2 == 0)
{
printf("\nMagic square is not possible") ;
goto end ;
}
printf("\nThe magic square for %d x %d is :\n\n", n, n) ;
j = (n + 1) / 2 ;
i = 1 ;
for(c = 1 ; c <= n * n ; c++)
{
a[i][j] = c ;
if(c % n == 0)
{
i = (i + 1);
goto loop ;
}
if(i == 1)
i = n ;
else
i = i - 1 ;
if(j == n)
j = 1;
else
j = j + 1 ;
loop : ;
}
for (i = 1 ; i <= n ; i++)
{
for (j = 1 ; j <= n ; j++)
{
printf("%d\t", a[i][j]) ;
}
printf("\n\n") ;
}
end : ;
}

Output:
Enter the size of the magic square : 3
The magic square for 3 x 3 is :
8

Binary Search Tree


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

#include <stdio.h>
/**BINARY SEARCH TREE FEAT. DELETION**/
struct node {int info;int count; struct node *left,*right;};
typedef struct node node;
node * findleast(node * );
//MAKE NEW NODE function
node* makenew()
{
node *p=(node *) malloc(sizeof(node));
p->count=0;
p->left=NULL;p->right=NULL;
return p;
}
//INSERT FUNCTION
node * insertnode(node *head, int x)//x is to be inserted
{
node *p=head,*q;
if (p==NULL)
{
q=makenew();
q->info=x;
q->count++;
return q;
}
if (x>p->info)
p->right=insertnode(p->right,x);
if (x<p->info)
p->left=insertnode(p->left,x);
if(x==p->info)
p->count++;
return head;
}
//INORDER TRAVERSAL
node * inorder(node * head)
{
if (head!=NULL)
{
inorder (head->left);
printf("%d(%d) ",head->info,head->count);
inorder (head->right);
}
}
//DELETE FUNCTION
node * del(node*root,int x)
{
node *p=root;
if (p!=NULL)
{
if (x==p->info)
{
if (p->right==NULL&&p->left==NULL)
free (p);
else if ((p->right==NULL&&p->left!=NULL)||(p->right!=NULL&&p->left==NULL))
{
int inf=(root->left!=NULL)?root->left->info:root->right->info;
// printf("\nturnery worked...output %d\n",inf);

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

root->info=inf;
if (root->left!=NULL)
{free (root->left);root->left=NULL;
}
else{
free (root->right);
root->right=NULL;
}
}
else//BOTH CHILDREN ARE PRESENT
{
node *td=findleast(p->right);
p->info=td->info;
del(td,td->info);
}
}
else
if (x>p->info)
del(p->right,x);
else
del (p->left,x);
}
}
//FINDLEAST
node * findleast(node * n)
{
if (n->left!=NULL)
n=findleast(n->left);
return n;
}
void main ()
{
node *root=NULL;
int ch,d;
do {
printf("Enter the function you wish to perform\n\n1-Insert\t\t2-Inorder\t\t3-Delete\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the element to insert.\n");
scanf("%d",&d);
root=insertnode(root,d);
break;
case 2:
inorder(root);
break;
case 3:
printf("\nEnter the number to delete.\n");
scanf ("%d",&d);
del(root,d);
break;
default:
printf("Invalid choice/Under construction.");
}
}while (ch!=4);
}

Output:
Enter the function you wish to perform

1-Insert
1

2-Inorder

3-Delete

Enter the element to insert.


6
Enter the function you wish to perform
1-Insert
1

2-Inorder

3-Delete

Enter the element to insert.


5
Enter the function you wish to perform
1-Insert
1

2-Inorder

3-Delete

Enter the element to insert.


7
Enter the function you wish to perform
1-Insert
1

2-Inorder

3-Delete

Enter the element to insert.


8
Enter the function you wish to perform
1-Insert
1

2-Inorder

3-Delete

Enter the element to insert.


4
Enter the function you wish to perform
1-Insert
2-Inorder
3-Delete
2
4(1) 5(1) 6(1) 7(1) 8(1) Enter the function you wish to perform
1-Insert
3

2-Inorder

3-Delete

Enter the number to delete.


8
Enter the function you wish to perform
1-Insert
2
4(1) 5(1) 6(1) 7(1)

2-Inorder

3-Delete

Circular Header Linked List:


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

#include <stdio.h>
#include<stdlib.h>
struct node {int info; struct node *link;};
typedef struct node node;
//-------------------NODE GENERATOR-----------------node* makenew()
{
node *p= (node*)malloc(sizeof(node));
p->link=NULL;
return p;
}
//-----------INSERT AFTER-------------node* insertafter(node * head, int x, int y)
{
node *p,*q;
p=head->link;
q=makenew();
q->info=y;
if (x==-1)
{
q->link=head->link; //MADE CIRCULAR
head->link=q;
head->info++;
return head;
}
while (p!=head)
{
if (p->info==x)//found element where insertion is supposed to be done
{
q->link=p->link;
p->link=q;
head->info++;
return head;
}
p=p->link;
}
}
//-------------DISPLAY------------------------void display(node *head)
{
node *p=head->link;
if (p==head)
{
printf("\nEmpty List\n");
return;
}

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

while (p!=head)
{
printf("%d ",p->info);
p=p->link;
if (p!=head)
printf(" --> ");
}
}
//-------------DELETION--------------------node *del(node *head,int x)
{
node *q;
node *p=head->link;
if (p==head)
{
printf("\nEmpty List\n");
return head;
}
if (p->info==x)
{
q=p;
head->link=p->link;//changing the first element
free (q);
head->info--;
return head;
}
while (p!=head)
{
if (p->link->info==x){
q=p->link;
p->link=p->link->link;
free(q);
head->info--;
return head;
}
p=p->link;
}
}

Circular Doubly Linked List


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

node* insertafter(node * head, int x, int y)


{
node *p,*q;
p=head;
q=makenew();
q->info=y;
if (x==-1)
{
if (head==NULL)
head=q;
q->next=head; //CIRCULAR
q->prev=head->prev;
head->prev=q;
head=q;
return head;
}

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

do{
if (p->info==x)//found element where insertion is supposed to be done
{
q->next=p->next;
p->next=q;
q->prev=p;
q->next->prev=q;
return head;
}
p=p->next;
} while(p!=head);
}
//-------------DISPLAY------------------------void display(node *head)
{
node *p=head,*q;
do{
q=p;
printf(%d ,p->info);
p=p->next;
if (p!=head)
printf( );
}while(p!=head);
printf(\nReverse Traversal\n);
do
{
p=q;
printf(%d ,q->info);
q=q->prev;
if (p!=head)
printf( );
}while (p!=head);
}
//-------------DELETION--------------------node *del(node *head,int x)
{
node *q;
node *p=head;
if (p->info==x)
{
q=head;
head=p->next;
if (head!=NULL)
head->prev=NULL;
free (q);
return head;
}
while (p!=NULL)
{
if (p->next->info==x){
q=p->next;
p->next=q->next;
if (q->next!=NULL)
q->next->prev=p;
free(q);
return head;
}
p=p->next;
}

84

Graphs:
1 #include <stdio.h>
2
3 struct node {
4 union {
5 int weight;
6 char name;
7 }info;
8 struct node *point, *link;
9 };
10 typedef struct node node;
11
12 node * makenew()
13 {
14
node *p=(node*) malloc (sizeof(node));
15
p->link=NULL;
16
p->point=NULL;
17
return p;
18 }
19 //ONE TIME USE ONLY FUNCTION
20 node * generator(int x)
21 {
22
int i;node *G,*p=NULL;
23
for (i=x;i>=1;i--)
24
{
25
G=makenew();
26
G->info.name=(char) (64+i);
27
G->link=p;
28
p=G;
29
}
30
printf("\nNodes Generated successfully\n");
31
return G;
32 }
33
34 void joinweight(node *p,node *q,int weight)//JOIN P WITH Q AND THE WEIGHT OF THE EDGE IS
PROVIDED...ARC LIST OF P NEEDS TO BE CREATED/MODIFIED
35 {
36
node *r,*s;
37
if (p->point==NULL)
38
{
39
r=makenew();
40
p->point=r;
41
r->point=q;
42
r->info.weight=weight;
43
}
44
else{
45
r=p->point;

46
while (r!=NULL)
47
{
48
s=r;
49
if (r->point==q)//IF THE EDGE ALREADY EXISTS, UPDATE THE WEIGHT
50
{
51
r->info.weight=weight;
52
printf("\nWeight of the previously existing edge has been updated.\n");
53
return;
54
}
55
r=r->link;
56
}
57
//EDGE DOESNOT EXIST....S CONTAINS LAST ELEMENT OF THE LIST WHERE THE NEW
CONNECTION WILL BE APPENDED.
58
r=makenew();
59
s->link=r;
60
r->point=q;
61
r->info.weight=weight;
62
63
}
64 }
65
66 void guigen(char a,char b, int wt,node *G) //This will convert chars to pointers.
67 {
68
node *p=G,*q,*r;
69
do
70
{
71
if (p->info.name==a)
72
q=p;
73
if (p->info.name==b)
74
r=p;
75
p=p->link;
76
}while (p!=NULL);
77
joinweight(q,r,wt); //joinweight function will be called with those pointers.
78
printf("\nNode joined from %c to %c with %d\n",a,b,wt);
79 }
80
81 void display(node *G)
82 {
83
node *p=G,*r;
84
printf("\nList of nodes...\n");
85
while (p!=NULL)
86
{
87
printf("%c ",p->info.name);
88
p=p->link;
89
if (p!=NULL)
90
printf("-->");
91
}
92
93
p=G;
94
while (p!=NULL)
95
{
96
r=p->point;
97
printf("\nArc List of %c : ",p->info.name);
98
if (r==NULL)
99
printf("NULL\n");
100
while (r!=NULL)
101
{
102
printf(" %c(%d) ",r->point->info.name,r->info.weight);
103
r=r->link;
104
}
105
printf("\n");
106
p=p->link;
107
}
108 }

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

int flag=0;
//DELETE FUNCTION
node* delElement(node *head,char b)//DELETE AN EDGE IN A GIVEN ARC LIST LEADING TO B
{
node *q,*r;
q=head;
r=q;
while (q!=NULL)
{
if (q->point->info.name==b)
{
if (r==q)
head= q->link;
else
r->link=q->link;
free (q);
flag=1;
return head;
}
r=q;
q=q->link;
}
return head;
}
//REMOVE EDGE
int delEdge(node *G,char a, char b)
{
flag=0;
node *p=G,*q,*r;
while (p!=NULL)
{
if (p->info.name==a)
break;
p=p->link;
}
q=p->point;
if (q==NULL)
return 0;//ELEMENT NOT FOUND...USUALLY CAN BE SOLVED WITH A SEXOND CALL
p->point=delElement(p->point,b);
return 1;
}
//DESTROY METHOD!
void destroy(node *head)
{
if (head!=NULL)
{
destroy(head->link);
free (head);
}
}
//DELETE A NODE
node * delNode(node *G,char a)
{
node *p=G,*q,*r=NULL,*s;
while (p!=NULL)
{
if (p->info.name!=a&&p->point!=NULL)
{
p->point=delElement(p->point,a);
}
p=p->link;
}
//TO REMOVE THE ARC LIST OF A AND ALSO REMOVE A FROM THE MAIN LINKED LIST.
p=G;

173 while (p!=NULL)


174 {
175
if (p->info.name==a)
176
{
177
if (p==G)
178
G=p->link;
179
else
180
r->link=p->link;
181
if (p->point!=NULL)
182
destroy(p->point);
183
free(p);
184
r=p;
185
}
186
p=p->link;
187 }
188
printf("\nNode has been successfully deleted!\n");
189
return G;
190 }
191 //MAIN function
192
193 void main ()
194 {
195
node *G;
196
int n,wt;
197
char a,b;
198
printf("Number of nodes?\n");
199
scanf("%d",&n);
200
G=generator(n);
201
202
do
203
{
204
printf("\nEnter- your choice\n1-Join 2 nodes\t\t2-Display\n3-Delete Edge\t\t4Delete Node\n7-Exit\n\n");
205
scanf("%d",&n);
206
switch (n)
207
{
208
case 1:
209
printf("\nEnter the source node\n");
210
a=getche();
211
printf("\nEnter the destination node\n");
212
b=getche();
213
printf("\nEnter the weight\n");
214
scanf("%d",&wt);
215
guigen(a,b,wt,G);
216
break;
217
case 2:
218
display(G);
219
break;
220
case 3:
221
printf("\nEnter the edge you wish to delete.\n");
222
printf("\nBetween\n");
223
a=getche();
224
printf("\nAnd\n");
225
b=getche();
226
flag=0;
227
delEdge(G,a,b);
228
if (flag==0)
229
delEdge(G,b,a);
230
break;
231
case 4:
232
printf("\nEnter the Node you wish to delete.\n");
233
234
a=getche();
235
G=delNode(G,a);
236
break;

237
238
239
240
241
242
243
244
245

case 7:
printf("Exiting");
break;
default:
printf("Invalid option");
break;
}
}while (n!=7);
}

Output:
Enter- your choice
1-Join 2 nodes
3-Delete Edge
7-Exit

2-Display
4-Delete Node

1
Enter the source node
G
Enter the destination node
F
Enter the weight
6
Node joined from G to F with 6
Enter- your choice
1-Join 2 nodes
3-Delete Edge
7-Exit

2-Display
4-Delete Node

2
List of nodes...
A -->B -->C -->D -->E -->F -->G
Arc List of A : B(4) F(6) G(7)
Arc List of B :

C(5)

Arc List of C :
Arc List of D :

D(7)
F(5)

G(10)

Arc List of E :

B(3)

C(11)

Arc List of F :

B(9)

E(8)

Arc List of G :

E(9)

F(6)

Enter- your choice


1-Join 2 nodes
3-Delete Edge
7-Exit

E(3)

D(8)

2-Display
4-Delete Node

4
Enter the Node you wish to delete.
F
Node has been successfully deleted!
Enter- your choice

1-Join 2 nodes
3-Delete Edge
7-Exit

2-Display
4-Delete Node

2
List of nodes...
A -->B -->C -->D -->E -->G
Arc List of A : B(4) G(7)
Arc List of B :

C(5)

Arc List of C :

D(7)

Arc List of D :

G(10)

Arc List of E :

B(3)

Arc List of G :

E(9)

Enter- your choice


1-Join 2 nodes
3-Delete Edge
7-Exit

C(11)

E(3)

D(8)

2-Display
4-Delete Node

3
Enter the edge you wish to delete.
Between
E
And
G
Enter- your choice
1-Join 2 nodes
3-Delete Edge
7-Exit

2-Display
4-Delete Node

2
List of nodes...
A -->B -->C -->D -->E -->G
Arc List of A : B(4) G(7)
Arc List of B :

C(5)

Arc List of C :

D(7)

Arc List of D :

G(10)

Arc List of E :

B(3)

C(11)

E(3)

D(8)

Arc List of G : NULL

Enter- your choice


1-Join 2 nodes
3-Delete Edge
7-Exit

2-Display
4-Delete Node

4
Enter the Node you wish to delete.
A
Node has been successfully deleted!
Enter- your choice
1-Join 2 nodes
3-Delete Edge
7-Exit

2-Display
4-Delete Node

2
List of nodes...
B -->C -->D -->E -->G
Arc List of B : C(5)
Arc List of C :

D(7)

Arc List of D :

G(10)

Arc List of E :

B(3)

C(11)

D(8)

Arc List of G : NULL

Sparse Matrix
1 #include<stdio.h>
2
3 struct node {
4 int row, col,info;
5 struct node *r,*d;
6 };
7
8
9 typedef struct node node;
10
11
12 void insertaf(node*, node*);
13 void insertbelow(node*, node*);
14 node * makenew(int row, int col, int info)
15 {
16
node * p= (node * ) malloc (sizeof(node));
17
p->r=NULL;
18
p->d=NULL;
19
//p->t=NULL;
20
//p->l=NULL;
21
p->row=row;
22
p->col=col;
23
p->info=info;
24
return p;
25 }
26 /**INSERT METHOD**/
27
28 node * insertElement(int info, int row, int col, node *head)//ASSUMING ALL THE HEADER
NODES HAVE BEEN CONSTRUCTED
29 {
30
node *p=makenew(row,col,info);
31
head->info++;
32
node *q;
33
q=head;
34
while (q->row!=row)//REACHING THE ROW...INCREASE COUNT IN ROWS LL
35
q=q->d;

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

q->info++;
do
{
if (q->r->col>col||q->r->col==-1)
{
insertaf(q,p);
break;
}
q=q->r;
}while (q->col!=-1);
q=head;
while (q->col!=col)
q=q->r;
do
{
if (q->d->row>row||q->d->row==-1)
{
insertbelow(q,p);
break;
}
q=q->d;
}while(q->row!=-1);
}
void insertaf(node * q, node *p)
{
p->r=q->r;
q->r=p;
}
void insertbelow(node * q, node *p)
{
p->d=q->d;
q->d=p;
}
void skeleton(node * head, int row, int col)//ASSUMING HEAD EXISTS WITH -1 -1 0
{
node *q=head;
int i;
for (i=1;i<=row;i++)
{
q->d=makenew(i,-1,0);
q=q->d;
q->r=q;//MAKING EACH ROW HEADER NODE CIRCULAR INITIALLY
}
q->d=head;
//----^^making new header row nodes and linking last with head-- circular
q=head;
for (i=1;i<=col;i++)
{
q->r=makenew(-1,i,0);
q=q->r;
q->d=q;
}
q->r=head;
}
void display(node * head, int row, int col)
{
int i=0,j=1;
node *q=head,*p;

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

q=q->d;
p=q->r;
while (q->row!=-1)
{
for (i=1;i<=col;i++)
{
if (p->col==i)
printf(" %d ",p->info);
else
printf(" %d ",0);
if (p->col<=i)
p=p->r;
}
printf("\n");
q=q->d;
p=q->r;
}
}
// MAIN FUNCTION
void main ()
{
node *head=makenew(-1,-1,0);
skeleton(head,5,5);
insertElement(5,1,3,head);
insertElement(7,2,3,head);
insertElement(9,1,1,head);
insertElement(9,5,1,head);
insertElement(9,3,3,head);
display(head,5,5);
printf("\n%d",head->info);
}

Output:
9
0
0
0
9

0
0
0
0
0

5
7
9
0
0

0
0
0
0
0

0
0
0
0
0

5
Process returned 2 (0x2)
execution time : 1.234 s
Press any key to continue.

You might also like