-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListStr.c
158 lines (140 loc) · 3.15 KB
/
ListStr.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
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
//
// ListStr.c
// Assignment2
//
// Created by JIHUI LIANG on 31/1/19.
// Copyright © 2019 JIHUI LIANG. All rights reserved.
//
#include "ListStr.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
typedef struct ListNode {
char value[100]; // value of this list item (string)
LNode next;
// pointer to next node in list
}ListNode;
typedef struct ListRep {
int nitems;// count of items in list
LNode first;
LNode last;
} ListRep;
// create a new DLListNode (private function)
LNode newLNode(char *val)
{
LNode new= malloc(sizeof(struct ListNode));
assert(new != NULL);
strcpy(new->value, val); // for int, new->value = it;
new->next = NULL;
return new;
}
// create a new empty DLListStr
List new2List()
{
List L = malloc(sizeof (struct ListRep));
assert (L != NULL);
L->nitems = 0;
L->first = NULL;
L->last = NULL;
return L;
}
/*
pre-reqisite: L is ordered (increasing) with no duplicates
post-condition: val is inserted in L, L is ordered (increasing) with no duplicates
*/
void insertSetOrd(List L, char *val){
char g[100];
strcpy(g,val);
assert(L!=NULL);
LNode new=newLNode(val);
assert(new!=NULL);
if (L->first==NULL){
L->first=L->last=new;
}
else {
LNode tempnode=L->first;
for (; tempnode; tempnode=tempnode->next) {
if (strcmp(tempnode->value,g)==0)
return;
}
L->last->next=new;
L->last=new;
}
L->nitems++;
/*
implement this function to
insert val in L (in order), no duplicates (set)
*/
if (L->nitems>=2) {
//qsortList(L->first, L->last);
bubblesort(L->first);
}
}
void bubblesort(struct ListNode *start){
int swapped;
struct ListNode *ptr1 = start;
struct ListNode *lptr = NULL;
/* Checking for empty list */
if (ptr1 == NULL)
return;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->next != lptr)
{
// if (ptr1->data > ptr1->next->data)
if (strcmp(ptr1->value,ptr1->next->value)>0)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
void swap(struct ListNode *a, struct ListNode *b)
{
char temp[100];
strcpy(temp,a->value);
// int temp = a->data;
strcpy(a->value,b->value);
// a->data = b->data;
strcpy(b->value,temp);
// b->data = temp;
}
void showLL(List L){
LNode tempnode=L->first;
while(tempnode){
printf("%s ",tempnode->value);
tempnode=tempnode->next;
}
printf("\n");
}
// display items from a DLListStr, comma separated
void free2LL(List L) {
assert(L!=NULL);
LNode cur,pre;
cur=L->first;
if (cur != NULL) {
pre=cur;
cur=cur->next;
free(pre);
}
free(L);
}
LNode first_node(List L){
return L->first;
}
LNode next_node(LNode N){
return N->next;
}
char *url2(LNode N){
return N->value;
}
int L_elements(List L){
return L->nitems;
}