Documente Academic
Documente Profesional
Documente Cultură
Pointers and structures can be used to build data structures that expand and
shrink during execution, e.g., lists, stacks, queues, trees,
struct node {
int value;
struct node *link;
};
Declares a structure type with two fields
value
link
holds a integer
holds a pointer to a struct node
value
link
n1
4
n2
5
n3
6
NULL
15-1
Lists
Use a pointer to traverse a list follow the link fields until you reach NULL
4
5
6
To add a new node at the end of the list, walk a pointer down to the last node
n1
4
newnode
6
NULL
8
NULL
8
NULL
15-2
List Headers
struct intlist {
struct node *head;
struct node *tail;
};
head tail
4
value link
NULL
NULL NULL
alist
NULL
List headers can be allocated, too, if you need an arbitrary number of lists (as
opposed to a list of arbitrary length)
15-3
This kind of interface is an abstract data type because it defines a type and the
operations on values of that type
15-4
15-5
NULL
Wrong! Why?
15-6
Sorting Revisited
<stdio.h>
"quicksort.h"
"intlist.h"
"misc.h"
int main(void) {
int i, n, *ptr, x;
struct intlist input = { NULL, NULL };
for (n = 0; scanf("%d", &x) == 1; n++)
intlist_addtail(&input, x);
ptr = emalloc(n*sizeof (int));
for (i = 0; i < n; i++)
ptr[i] = intlist_remhead(&input);
quicksort(ptr, 0, n - 1);
for (i = 0; i < n; i++)
printf("%d\n", ptr[i]);
return 0;
}
15-7
pop
push
pop
intlist_addhead
intlist_remhead
Queues: Add nodes at the tail, remove nodes from the head
put
NULL
get
put
get
intlist_addtail
intlist_remhead
What about intlist_remtail? Need a doubly linked list for efficient removal
Deques: Add/remove nodes at either end
push
put
NU
LL
NU
LL
get
push
get
intlist_addhead
intlist_remhead
pull
put
pull
intlist_addtail
intlist_remtail
15-8