1 / 34

CSEB324 Data Structures & Algorithms

CSEB324 Data Structures & Algorithms. Chapter 2 Pointer & Linked List. Introduction. If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow Size can’t be extended Wasted if unused To overcome this – we use linked list.

hesper
Download Presentation

CSEB324 Data Structures & Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSEB324Data Structures & Algorithms Chapter 2 Pointer & Linked List

  2. Introduction • If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed • Overflow • Size can’t be extended • Wasted if unused • To overcome this – we use linked list. • To understand linked list, we must first understand the fundamentals – the pointer.

  3. Pointer ? • Variable concept • Declaring a variable • Memory allocation • Pointer ? • A variable which give location of other variable. • Linked List? • Put a pointer in a structure, giving the next location of the next structure. • Static variable vs. Dynamic variable • Static variable – declared and named in program • Dynamic – created during program execution.

  4. Understanding Pointers • Declare a pointer int *aPtr; int *aPtr = null; • Assigning a pointer aPtr = &aVar; • Read the pointer value printf(“%d “, *aPtr);

  5. Exercise • Declare an integer variable a and b • Declare pointer variables aPtr and bPtr • Assign a to has a value of 100, and b to has a value of 200 • Assign aPtr to points to a • Assign bPtr to points to aPtr • By using bPtr, change value of a to be the same as b.

  6. 123 A C Linked List • a pointer in a structure, giving the next location of the next structure.

  7. Temp Item Next Item Next NULL Llist Linked List - Declaration • Linked List consist of structure • Llist – is a pointer, pointing to a linked list structure • Next is a pointer in the linked list structure

  8. Structure Declaration typedef char item; typedef struct node{ item data; struct node *next; } Node;

  9. NULL Llist Item Next Declaring the linked list • To declare a linked list Node *Llist; //Llist –pointer pointing to a node type

  10. Linked List Operation • Create a Nod • Verify for an empty list • Traversal along the linked nodes • Insert new nodes • Delete a node

  11. Creating New Node Node *newnode (item c) { Node *n; n = (Node *) malloc (sizeof (Node)); if ( n != NULL) { n-> data = c; n->next = NULL; } return n; }

  12. NULL Temp X Temp Item Next Item Next Item Next Item Next Item Next Item Next NULL NULL Llist Llist Insert New Node at the begining

  13. NULL Temp CurrPtr Temp NULL Llist CurrPtr NULL Llist Item Next Item Next Item Next Item Next Item Next Item Next Item Next Item Next Inserting node in the middle

  14. Insert New Node - implementation void InsertNode( Node *Llist, Node *temp, Node *CurrPtr){ { if (CurrPtr ==NULL) { temp->next = Llist; Llist = temp; } else { temp->next = CurrPtr->next; CurrPtr –>next = temp; } }

  15. Item Next Item Next Item Next Item Next NULL NULL Llist Llist Traverse The List void Traversal ( Node *Llist) { Node *Temp; Temp = Llist; while ( Temp != NULL) { printf (“data = %c", Temp->data); Temp = Temp-> next; } } Temp

  16. Temp Temp X NULL Llist Item Next Item Next Item Next Item Next NULL Llist Deleting first node

  17. Temp CurrPtr Temp NULL Llist CurrPtr NULL Llist Item Next Item Next Item Next Item Next Item Next Item Next Deleting middle or last node

  18. Deleting Node - implementation void DeleteNode( Node *Llist, Node *CurrPtr) { Node *temp; if (CurrPtr ==NULL) { temp = Llist; Llist = temp->next; } else { temp = CurrPtr->next; CurrPtr –>next = temp->next; } free(temp) }

  19. Type of Linked List • Simple one-way linked list x4 x1 x3 x2 L

  20. Type of Linked List • Circular Linked List • Formed by having the link in the l ast node of a one way linked list point back to the first node. x4 x1 x3 x2 L

  21. Type of Linked List • Two Way Linked List • Formed from nodes that have pointers to both their left and right neighbours in the list L x2 x3 x1

  22. Type of Linked List • Linked List with Header Node • Header points to the first node • As a marker / stopping place • Ease the deletion process of a node Header Node x4 x1 x3 x2 L L

  23. Exercise 2 typedefstructbmi { int age; float weight; structbmi *next; } BMI; BMI *mybmi; Indicate whether the statement is TRUE or FALSE • The following code segment is to traverse and display all data from the linked list. while (mybmi != NULL) printf(“%d %.2f”, mybmi->age, mybmi->weight);

  24. Exercise 2 (cont..) typedefstructbmi { int age; float weight; structbmi *next; } BMI; BMI *mybmi; Indicate whether the statement is TRUE or FALSE • Assume q has been declared as a pointer of type BMI, then the statement to create a dynamic storage of type BMI is as follows: q = (BMI*) malloc (sizeof(BMI));

  25. Exercise 3 • Write code segment to delete a node with value 15.00. • Given, ListGame *temp; temp = (ListGame*) malloc (sizeof(ListGame)); temp -> data = 12.0 ; temp->link = NULL; Write a code segment to insert this node after the first node in above linked list.

  26. Exercise 4 Given, struct Student { char name [20]; float cgpa; Student *next; }; Refer to the diagram, write code segment to delete the node Siew Chee.

  27. Linked List Using Array • Older and widely used computer language (COBOL, Fortran, BASIC) do not provide facilities for dynamic storage allocation (pointers) • Workspace (several arrays hold different part of a logical record) is used for programming languages which do not support records.

  28. Linked List Using Array • Implementation of linked list using array is preferred if: • Number of entries is known in advance • Few insertions or deletions • Data are sometimes best treated as a linked list and other times as a contiguous

  29. Linked List Using Array typedef char ListEntry; typedef int ListIndex; typedef struct listnode{ ListEntry entry; ListIndex next; } ListNode; typedef int Position; typedef struct list{ ListIndex head; int count; }List; ListIndex avail,lastused; ListNode workspace[10]

  30. Array Linked List – New Node ListIndex NewNode (void) { ListIndex newindex = -1; if(avail != -1) { newindex = avail; avail = workspace[avail].next; workspace[newindex].next = 0; } else if (lastused < MAXLIST - 1) { newindex = ++lastused; workspace[newindex].next = 0; } else printf (“ Error Overflow : workspace for linked list is full”); return newindex; }

  31. Array Linked List - Insert void InsertList ( Position p, ListEntry x, List *list) { ListIndex newindex, previous; if ( p <0 || p > list->count) printf(“ Error inserting into a nonexistent position”); else { newindex = NewNode(); workspace[newindex].entry = x; if (p == 0) { workspace[newindex].next = list->head; list->head = newindex; } else { SetPosition(p-1, &previous, list); workspace[newindex].next=workspace[previous].next; workspace[previous].next = newindex; } list->count ++; } }

  32. Array Linked List - Dispose void DisposeNode (ListIndex oldindex, List *list) { ListIndex previous; if( oldindex == -1) printf(“Error : Disposing a nonexistent node”); else { if ( oldindex == list-> head) list->head = workspace[oldindex].next; else { SetPosition(CurrentPosition(oldindex,list)– 1, &previous, list); workspace[previous].next=workspace[oldindex].next; } workspace[oldindex].next = avail; avail = oldindex; } }

  33. Array Linked List - Traverse void TraverseList (List *list) { ListIndex current; for (current= list->head ; current != -1; workspace[current].next) printf(”data of workspace[%d].entry = %c”, current, workspace[current].entry); }

  34. Class Exercise • Write a complete program to create, insert and delete a linked list. Each list will consist of ID (an integer) and Grade (a character) • Guides • Declare linked list structure • Writes all the ADT funtions (newNode, InsertNode, DeleteNode, TraverseNode) • In main program: • Create new node • Traverse the list • Create another node • Insert into the existing list • Traverse list • Delete the last node • Traverse list • Insert new node • Traverse list

More Related