<<

• Sequence of nodes, each containing arbitrary data fields • One or two references ("links") pointing to the next Data and/or previ ous no des Linked List & Binary

Node 0 1 Node 2

Single link linked list Based on slides © McGraw-Hill Additional material © 2004/2005 Lewis/Martin

CIT593 2

Linked List vs. Array Link List Implementation in

Advantage of linked list struct listNode { • Dynamic size (easy to add or remove nodes) int val; Advantage of array struct listNode * next; • Easy/fast element access };

Element access for a list typedef struct listNode item; • Linked list elements can only be accessed sequentially e.g., to find the 5th element, you must start from head and follow the links through four other nodes

CIT593 3 CIT593 4

1 Create a list of 10 elements Print the linked list element item * curr, * head, *tail; int i; //reassign curr to head head = tail = NULL; curr = head; for(i=1;i<=10;i++) { 1 curr = (item *)malloc(sizeof(item)); //allocate one item 2 curr->val = i; while(curr != NULL) { 3 curr->next = NULL; printf("%d\n", curr->val); 4 5 if(head == NULL){ Note: head is pointing curr = curr->next ; 6 head = curr; to first ever node. Tail } 7 tail = head; points to the last item 8 } added else{ 9 tail->next = curr; 10 tail = tail->next; } CIT593} 5 CIT593 6

Deallocate all memory used by list Sorted List Example: Car Lot

Goal //Deallocate • Create inventory for used car lot item * temp = head; • Support the following operations while(head != NULL){ ¾ShSearch dtbdatabase f or a parti tilcular vehi hilcle ¾Add new vehicle to database Make sure before head= head->next; ¾Delete vehicle from database you delete the free(temp); node you have temp = head; reference to its Implementation next node • Since we don’ know how many cars might be on lot at one time, } we chlikdlitttihoose a linked list representation • In order to have “faster” search, the database must remain sorted by vehicle ID

CIT593 7 CIT593 8

2 CarNode Scanning the List

Each vehicle has the following characteristics Scanning • Searching, adding, and deleting require us to find a particular • Vehicle ID, make, model, year, mileage, cost node in the list • (And… pointer to next node) • Scan list until we find node whose ID is >= one we ’re looking for typedef struct car_node CarNode; CarNode *ScanList(CarNode *head, int searchID) { struct car_node { CarNode *previous, *current; int vehicleID; previous = head; char make[20]; current = head->next; char model[20]; /* Traverse until ID >= searchID */ int year; while ((current!=NULL ) && (current->vehicleID < searchID)) { int mileage; previous = current; double cost; current = current->next; CarNode *next; /* ptr to next car in list */ } } return previous; } CIT593 9 CIT593 10

Adding a Node Excerpts from Code to Add a Node void addCar(CarNode ** head, CarNode * newNode){..} newNode = (CarNode*) malloc(sizeof(CarNode)); Steps /* initialize node with new car info */ ... • Create new node with ppproper info ( via malloc) prevNode = ScanList(* head, newNode->vehicleID); • Find node (if any) with a greater vehicleID (via ScanList) nextNode = prevNode->next; • “Splice” the new node into the list (update next fields) ... if ((nextNode == NULL) new node || (nextNode->vehicleID != newNode->vehicleID)) prevNode->next = newNode; newNode->next = nextNode; } else { Node 0 Node 1 Node 2 printf(“Car already exists in database.”); free(newNode); } NULL

CIT593 11 CIT593 12

3 Deleting a Node Trees • Hierarchical structure with a of linked nodes Steps • Find the node that points to the desired node (via ScanList) • Redirect that node’s pointer to the next node (or NULL) • A node may contain a value or a condition or • Free the deleted node’s memory represent a separate

• A node has zero or more child nodes • Most common type is with each node Node 0 Node 1 Node 2 havinggg a left and right child

NULL

CIT593 13 CIT593 14

Binary Tree E.g. Sorted Binary Tree

Node 0 Structure • At each node, value of the left child is less than value at node struct tnode{ Node 1 Node 2 • Also, value of the right child is greater than the value at node

itint val; NULL NULL struct tnode * left; Node 3 Node 4 Implications • Faster lookup NULL NULL struct tnode * right; NULL NULL • Slower insertion }; (can’t just prepend) typedef struct tnode node; Optimizations • Balancing • Redistributing

15 CIT593 CIT593 16

4 Inserting an element to keep tree sorted Inserting an element to keep tree sorted (contd..) node * root = NULL; //global ptr else if(item ->val > curr->val){ if(curr->right == NULL){ void insert(node * item){ curr->right = item; if(root == NULL){ break; rootitt = item; } } else{ else{ curr = curr->right; node * curr = root; //curr is your reference that keeps moving } while(1){ }//end else if if(item->val < curr->val){ else{ if(curr->left == NULL){ //duplicate curr->lftitleft = item; break; break; } } }//end while else{ }//end else curr = curr->left; }//end insert } CIT593 CIT593 } 17 18

5