## Presentation on theme: "Linked Lists."— Presentation transcript:

B C Head A linked list is a series of connected nodes Each node contains at least A piece of data (any type) Pointer to the next node in the list Head: pointer to the first node The last node points to NULL node A data pointer

Linked lists are made up of Nodes Declare Node class for the nodes data: double-type data in this example next: a pointer to the next node in the list struct node { int data; struct node *next; /* pointer to next element in list */ } LLIST;

LLIST *list_add(LLIST **p, int i); void list_remove(LLIST **p); LLIST **list_search(LLIST **n, int i); void list_print(LLIST *n);

Inserting a new node Steps
Insert a node with data equal to x after the index’th elements. (i.e., when index = 0, insert the node as the first element; when index = 1, insert the node after the first element, and so on) If the insertion is successful, return the inserted node. Otherwise, return NULL. (If index is < 0 or > length of the list, the insertion will fail.) Steps Locate index’th element Allocate memory for the new node Point the new node to its successor Point the new node’s predecessor to the new node index’th element newNode

Inserting a new node Possible cases of InsertNode
Insert into an empty list Insert in front Insert at back Insert in middle But, in fact, only need to handle two cases Insert as the first node (Case 1 and Case 2) Insert in the middle or at the end of the list (Case 3 and Case 4)

{ if (p == NULL) return NULL; LLIST *n = (LLIST *) malloc(sizeof(LLIST)); if (n == NULL) n->next = *p; /* the previous element (*p) now becomes the "next" element */ *p = n; /* add new empty element to the front (head) of the list */ n->data = i; return *p; }

Finding a node Search for a node with the value equal to x in the list. If such a node is found, return its position. Otherwise, return 0. LLIST **list_search(LLIST **n, int i) { if (n == NULL) return NULL; while (*n != NULL) if ((*n)->data == i) return n; } n = &(*n)->next;

Deleting a node Steps Like InsertNode, there are two special cases
int DeleteNode(double x) Delete a node with the value equal to x from the list. If such a node is found, return its position. Otherwise, return 0. Steps Find the desirable node (similar to FindNode) Release the memory occupied by the found node Set the pointer of the predecessor of the found node to the successor of the found node Like InsertNode, there are two special cases Delete first node Delete the node in middle or at the end of the list

void list_remove(LLIST **p) /* remove head */
{ if (p != NULL && *p != NULL) LLIST *n = *p; *p = (*p)->next; free(n); }

Printing all the elements
void List_Print(void) Print the data of all the elements Print the number of the nodes in the list

void list_print(LLIST *n)
{ if (n == NULL) printf("list is empty\n"); } while (n != NULL) printf("print %p %p %d\n", n, n->next, n->data); n = n->next;

Using all these routines
int main(void) { LLIST *n = NULL; list_add(&n, 0); /* list: 0 */ list_add(&n, 1); /* list: 1 0 */ list_add(&n, 2); /* list: */ list_add(&n, 3); /* list: */ list_add(&n, 4); /* list: */ list_print(n); list_remove(&n); /* remove first (4) */ list_remove(&n->next); /* remove new second (2) */ list_remove(list_search(&n, 1)); /* remove cell containing 1 (first) */ list_remove(&n->next); /* remove second to last node (0) */ list_remove(&n); /* remove last (3) */ return 0; }

Circular linked lists The last node points to the first node of the list How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A B C Head