Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS246 Lec15 Section 1 Today’s Goals Self-referential Structures • A basic data type (building block) for complex data structures such as trees and linked lists. • Self-referential Structures • Linked Lists single • Structure tags (i.e. tnode, lnode) are required for self-referential structure declarations. double general purpose memory issues CS246 typedef struct tnode { int x; struct tnode *left; struct tnode *right; } Treenode; 1 CS246 Lec15 typedef struct lnode { int x; struct lnode *next; } Listnode; 2 Lec15 Section 2 Linked Lists Singly and Doubly Linked Lists • A linked list stores a lists of items (structs). • Linked lists are typically unbounded, that is, they can grow infinitely. • A singly linked list has each struct containing only one pointer to the next. • A doubly linked list has each struct containing both a pointer to the previous as well as the next struct in the list. • An array is a single consecutive piece of memory, a linked list is made of many pieces. single head tail • A linked list offers quick insertion, deletion and reordering of the items CS246 3 head double NULL tail NULL NULL CS246 Lec15 4 struct node Lec15 makenode Node *makenode (int x) { struct node { singlelist.c Node *new; int num; if ( (new = (Node *) malloc( sizeof(Node) ) )!= NULL) { struct node *next; new->num = }; x; new -> next = NULL; } typedef struct node Node; else { // typedef Node *Nodeptr; printf("Out of memory!\n"); Node *head = NULL; exit(0); Node *tail = NULL; } return new; } CS246 5 CS246 Lec15 1 6 Lec15 CS246 Lec15 append delete void delete (Node *p) { void append (Node *p) { Node *tmp, *prev; if ((p == head) && (p == tail)) if (head == NULL) { head = tail = NULL; head = p; else if (p == head) tail = p; head = p->next; } else { else { for(tmp=head, prev=NULL; tmp!=p; prev=tmp, tmp=tmp->next); tail->next = p; if (p == tail) tail = p; tail = prev; } prev->next = p->next; } } CS246 7 CS246 Lec15 8 insert_after Lec15 print/search /* insert a node p after p2 */ void print() { void insert_after (Node *p, Node *p2) { Node *tmp; p->next = p2->next; for (tmp = head; tmp != NULL; tmp = tmp->next) if (p2 == tail) printf("\n"); printf("%d ", tmp->num); tail = p; } Node *search(int x) { p2->next = p; Node *tmp; } for (tmp = head; tmp != NULL; tmp = tmp->next) if (tmp->num == x) return tmp; return NULL; } CS246 9 CS246 Lec15 main Lec15 clear int main() { • Note that this only works if structure Node does not contain any other pointers to memory Node *tmp; int i; for (i = 0; i < 10; i++) { tmp = makenode(i); void clear() { append(tmp); Node *tmp, *tmp2; } for (tmp = head; tmp != NULL; tmp = tmp2) { print(); tmp2 = tmp->next; tmp = makenode(9); free(tmp); insert_after(tmp, head->next->next); } delete(head->next); head = tail = NULL; print(); } } CS246 10 11 CS246 Lec15 2 12 Lec15 CS246 Lec15 Section 3 General Purpose Linked Lists General Purpose makenode Lnode *makenode (void *data) { • void * Lnode *new = NULL; Generic pointer – just a memory address if ((new = (Lnode *) malloc(Lnode)) != NULL) { Can be casted to any type new->prev = NULL; struct llist_node { new->next = NULL; new->data = data; void *data; struct llist_node *prev; } struct llist_node *next; return new; }; } typedef struct llist_node Lnode; CS246 13 CS246 Lec15 14 Lec15 Section 4 Linked List in Java Avoid Memory Leaks Like Plagues • In Java, a linked list and the nodes in the list are of different types. class ListNode{ • This can be simulated in C. }; • However, there are advantages to keeping the same type. CS246 • Whenever dynamically allocated storages are in use, memory leaks are plentiful • The problem is more evident when complicated data structures are used int item; ListNode next; ... Mixing: list of trees, trees of lists, etc class List { Nesting: list of lists of lists ListNode head; ... • When implementing complex data structures, plan your clear/release functions very carefully } 15 CS246 Lec15 Shallow and Deep Copying • Linked lists are the most commonly used data structure in programming • Learning how to implement a proper linked list is essential The new list consist of duplicated pointers only t1 t2 Lec15 Summary • There are two ways to make a copy of a linked list • Shallow copy: h1 16 • Watch out for memory leaks! • Explore general purpose linked lists h2 • Deep copy: The new list consist of duplicated data as well as pointers CS246 17 CS246 Lec15 3 18 Lec15