Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.

Similar presentations

Presentation on theme: "Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use."— Presentation transcript:

1 Programming Linked Lists

2 COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List of ten students marks int studentMarks[10]; List of temperatures for the last two weeks double temperature[14];

3 COMP104 Linked Lists / Slide 3 Motivation * list using static array int myArray[10]; We have to decide in advance the size of the array (list) * list using dynamic array int n, *myArray; cin >> n; myArray = new int[n]; We allocate an array (list) of any specified size while the program is running * linked-list (dynamic size) size = ?? The list is dynamic. It can grow and shrink to any size.

4 COMP104 Linked Lists / Slide 4 Linked Lists: Basic Idea * A linked list is an ordered collection of data * Each element of the linked list has n Some data n A link to the next element * The link is used to chain the data * Example: A linked list of integers: 20457585 Data Link

5 COMP104 Linked Lists / Slide 5 * The list can grow and shrink Linked Lists: Basic Ideas 20457585 2045 add(75), add(85) delete(85), delete(45), delete(20) 75

6 COMP104 Linked Lists / Slide 6 * Original linked list of integers: * Insertion: * Deletion Linked Lists: Operations 20457585 20457585 20457585 60 old value deleted item

7 #include using namespace std; struct Node{ int data; Node *next; }; typedef Node* NodePtr;

8 COMP104 Linked Lists / Slide 8 typedef * typedef allows you to make new shortcuts to existing types typedef int WAH; WAH k;// same as:int k; typedef int* WAHPTR; WAHPTR p;// same as: int *p; typedef Node* NodePtr; NodePtr Head;// same as: Node* Head;

9 COMP104 Linked Lists / Slide 9 Linked List Structure * Node : Data + Link n Definition struct Node { int data;//contains useful information Node* next;//points to next element or NULL }; n Create a Node Node* p; p = new Node;//points to newly allocated memory n Delete a Node delete p;

10 COMP104 Linked Lists / Slide 10 Linked List Structures n Access fields in a node (*p).data;//access the data field (*p).next;//access the pointer field Or it can be accessed this way p->data//access the data field p->next//access the pointer field

11 COMP104 Linked Lists / Slide 11 Representing and accessing Linked Lists * We define a pointer NodePtr Head; that points to the first node of the linked list. When the linked list is empty then Head is NULL. 20457585 Head

12 COMP104 Linked Lists / Slide 12 Passing a Linked List to a Function  When passing a linked list to a function it should suffice to pass the value of Head. Using the value of Head the function can access the entire list.  Problem: If a function changes the beginning of a list by inserting or deleting a node, then Head will no longer point to the beginning of the list.  Solution: When passing Head always pass it by reference (or using a function to return a new pointer value) 20457585 Head

13 COMP104 Linked Lists / Slide 13 Manipulation of a Unsorted Linked List

14 COMP104 Linked Lists / Slide 14 Start the first node from scratch NodePtr newPtr; newPtr = new Node; newPtr->data = 20; newPtr->next = NULL; Head = newPtr; Head newPtr 20 Head Head = NULL;

15 COMP104 Linked Lists / Slide 15 Inserting a Node at the Beginning newPtr = new Node; newPtr->data = 13; newPtr->next = Head; Head = newPtr; Head newPtr 13 20

16 COMP104 Linked Lists / Slide 16 Keep going … Head newPtr 50401320

17 void addHead(NodePtr& Head, int newdata){ NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = Head; Head = newPtr; } Adding an element to the head:

18 COMP104 Linked Lists / Slide 18 (to delete) Deleting the Head Node NodePtr cur; cur = Head; Head = Head->next; delete cur; Head cur 50401320

19 void delHead(NodePtr& Head){ if(Head != NULL){ NodePtr cur = Head; Head = Head->next; delete cur; }

20 COMP104 Linked Lists / Slide 20 Displaying a Linked List cur = Head; cur = cur->next; 2045 Head cur 2045 Head cur

21 COMP104 Linked Lists / Slide 21 * A linked list is displayed by walking through its nodes one by one, and displaying their data fields. void DisplayList(NodePtr Head){ NodePtr cur; cur = Head; while(cur != NULL){ cout data << endl; cur = cur->next; }

22 //return the pointer of the node has data=item //return NULL if item does not exist NodePtr searchNode(NodePtr Head, int item){ NodePtr Cur = Head; NodePtr Result = NULL; while(Cur != NULL){ if(Cur->data == item) Result = Cur; Cur = Cur->next; } return Result; } Searching for a node

23 COMP104 Linked Lists / Slide 23 * Original linked list of integers: * Add to the end (insert at the end): More operation: adding to the end 50401320 50401320 60 Last element The key is how to locate the last element or node of the list!

24 void addEnd(NodePtr& Head, int newdata){ NodePtr last = Head; NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = NULL; if(Head != NULL){ // non-empty list case while(last->next != NULL) last = last->next; last->next = newPtr; } else// deal with the case of empty list Head = newPtr; } Add to the end: Link new object to last->next Link a new object to empty list

25 COMP104 Linked Lists / Slide 25 Manipulation of a Sorted Linked List

26 COMP104 Linked Lists / Slide 26 Inserting a Node Head cur 20 33 4575 prev... newPtr

27 COMP104 Linked Lists / Slide 27 * To insert a new node into the list 1. (a) Create a new node using: NodePtr newPtr = new node; (b) Fill in the data field correctly. 2. Find “ prev ” and “ cur ” such that the new node should be inserted between *prev and *cur. 3. Connect the new node to the list by using: (a) newPtr->next = cur; (b) prev->next = newPtr;

28 COMP104 Linked Lists / Slide 28 Finding prev and cur  Suppose that we want to insert or delete a node with data value newValue. Then the following code successfully finds prev and cur. prev = NULL; cur = Head; while(cur!=NULL && newValue > cur->data){ prev = cur; cur = cur->next; }

29 //insert item into linked list in ascending order void insertNode(NodePtr& Head, int item){ NodePtr New, Cur, Pre; New = new Node; New->data = item; Pre = NULL; Cur = Head; while(Cur != NULL && item > Cur->data){ Pre = Cur; Cur = Cur->next; } if(Pre == NULL){//insert to head of linked list New->next = Head; Head = New; } else { Pre->next = New; New->next = Cur; }

30 COMP104 Linked Lists / Slide 30 (to delete) Deleting a Node * To delete a node from the list 1. Locate the node to be deleted (a) cur points to the node. (b) prev points to its predecessor 2. Disconnect node from list using: prev->next = cur->next; 3. Return deleted node to system: delete cur; Head cur 20457585 prev...

31 void deleteNode(NodePtr& Head, int item){ NodePtr prev, cur = Head; while(cur!=NULL && item > cur->data){ prev = cur; cur = cur->next; } if(cur==NULL || cur->data!=item){ cout << "Delete error: " << item << " not in list!" << endl; return; } if(cur==Head) Head = Head->next; else prev->next = cur->next; delete cur; } Delete an element in a sorted linked list:

Download ppt "Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use."

Similar presentations

Ads by Google