-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 12
More file actions
142 lines (134 loc) · 2.09 KB
/
Assignment 12
File metadata and controls
142 lines (134 loc) · 2.09 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Assignment no.:12
Title: Implement Generalized Linked List (GLL) to create and display the book index.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
union var
{
char data;
struct node *level_link;
}info;
int tag;
struct node *next;
}node;
int i=1;
char str[20];
int main()
{
int ch,flag=0;
struct node *x=NULL;
struct node *create();
void display(node *head, int level);
do
{
printf("\n1.create\n2.diplay\n3.quit");
printf("\n Enter your choice");
scanf("%d",&ch);
if(ch==1)
flag=1;
if(flag==1)
{
switch(ch)
{
case 1:i=1;
x=create();
break;
case 2:printf("index\n");
display(x,0);
break;
case 3:break;
}
printf("\n Do you want to continue");
scanf("%d",&ch);
}
}while(ch!=3);
return 0;
}
struct node *create()
{
node *create_gll(node *head);
node *head=NULL;
int count=0,i;
printf("\nEnter string:");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='(')
count++;
else if(str[i]==')')
count--;
else continue;
}
head=create_gll(head);
return(head);
}
struct node *create_gll(node *head)
{
node *n,*prev, *p=NULL;
head=NULL;
while(str[i]!='\0')
{
if(str[i]=='(')
{
n=(node *)malloc(sizeof(node));
n->tag=1;
n->next=NULL;
if(head==NULL)
head=n;
else
prev->next=n;
prev=n;
i++;
n->info.level_link=create_gll(p);
}
else if(str[i]==')')
{
i++;
return(head);
}
else
{
n=(node *)malloc(sizeof(node));
n->tag=0;
n->next=NULL;
n->info.data=str[i];
if(head==NULL)
head=n;
else
prev->next=n;
prev=n;
i++;
}
}
return(head);
}
void display(node *head, int level)
{
int local=0,i;
while(head!=NULL)
{
if(head->tag==0)
{
while((head!=NULL)&&(head->tag==0))
{
if(local!=level)
printf("\t");
printf("%c",head->info.data);
head=head->next;
printf("\n");
}
}
else
{
for(i=0;i<level;i++)
printf("\t");
++local;
display(head->info.level_link, local);
printf("\n");
--local;
head=head->next;
}
}
}