-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklist.c
57 lines (56 loc) · 1.22 KB
/
linklist.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "linklist.h"
node *create(char *name,int val){
node *top=(node*)malloc(sizeof(node));
top->label=(char*)malloc(strlen(name)*sizeof(char));
strcpy(top->label,name);
top->target=val;
top->next=NULL;
return top;
}
void insertN(node **top,node *e){
node *end=*top;
if(*top==NULL){
*top=e;
return;
}
while(end->next!=NULL)
end=end->next;
end->next=e;
return;
}
void insert(node **top,char *name,int val){
node *temp=(node*)malloc(sizeof(node));
temp->label=(char*)malloc(strlen(name)*sizeof(char));
strcpy(temp->label,name);
temp->target=val;
temp->next=NULL;
node *end=*top;
if(*top==NULL){
*top=temp;
return;
}
while(end->next!=NULL)
end=end->next;
end->next=temp;
return;
}
int sub(node *top,char *s){
node *temp=top;
while(temp){
if(strcmp(temp->label,s)==0)
return temp->target;
temp=temp->next;
}
printf("Error:Unknown Label %s\n",s);
exit(1);
}
void printlist(node *top){
node *temp=top;
while(temp){
printf("%s,%d->",temp->label,temp->target);
temp=temp->next;
}
}