Chapter 3: Linked List Tutor: Angie Hui

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advanced Piloting Cruise Plot.
1
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
Copyright © 2003 Pearson Education, Inc. Slide 1.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 5 second questions
Year 6 mental test 10 second questions
Richmond House, Liverpool (1) 26 th January 2004.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Linked Lists.
Data Structures: A Pseudocode Approach with C
Chapter 24 Lists, Stacks, and Queues
Data Structures Using C++
LIST PROCESSING.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
ABC Technology Project
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
EU market situation for eggs and poultry Management Committee 20 October 2011.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
2 |SharePoint Saturday New York City
Green Eggs and Ham.
VOORBLAD.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
BIOLOGY AUGUST 2013 OPENING ASSIGNMENTS. AUGUST 7, 2013  Question goes here!
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
© 2012 National Heart Foundation of Australia. Slide 2.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Subtraction: Adding UP
Januar MDMDFSSMDMDFSSS
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Analyzing Genes and Genomes
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
1 Chapter 13 Nuclear Magnetic Resonance Spectroscopy.
Energy Generation in Mitochondria and Chlorplasts
Presentation transcript:

Chapter 3: Linked List Tutor: Angie Hui CS154 Data Structure in C Chapter 3: Linked List Tutor: Angie Hui

Chapter Objectives Definition of linked list Adding Nodes Deleting Nodes Interchanging Nodes Losing nodes Garbage Collection

What is a Linked List? A linked list is a dynamic variable that contains a number of nodes and each node is connected by pointers. Types of Linked list Unidirectional linked list (or Singly Linked List) Bidirectional linked list (or Doubly Linked List) Circular linked list Unidirectional + Circular linked list Bidirectional + Circular linked list

Unidirectional linked list head 11 13 15 38 The list goes in only one direction It can only go forward. It cannot go back. So, it’s important to have a fixed pointer to point to the first node of the list. Otherwise, when you go to the 3rd node, the previous 2 nodes will be lost. We normally call the fixed pointer, head.

Unidirectional linked list head 11 13 15 38 All the nodes in a linked list must have the same structure. Last node references to NULL. To implement the node for the above unidirectional linked list, we can use the struct in the next slide.

Unidirectional linked list struct Node { int item; struct Node *next; };

Unidirectional linked list node1 50 To create a new node, pointed by “node1”: struct Node *node1; node1 = (struct node*)malloc(sizeof(struct Node)); node1item = 50; node1next = NULL;

Unidirectional linked list node2 12 To create another new node, pointed by “node2”: struct Node *node2; node2 = (struct node*)malloc(sizeof(struct Node)); node2item = 12; node2next = NULL;

Unidirectional linked list node1 node2 50 12 To connect node1 and node2 together: node1next = node2; After that: node2 node1 50 12

To travel the entire linked list head 11 13 15 38 Because we cannot change the reference of head, we have to use a temporary pointer (let say temp) as a moving pointer to move from the 1st node to the last node.

To travel the entire linked list head 11 13 15 38 temp First of all, we should make temp to point to the 1st node temp = head;

To travel the entire linked list head 11 13 15 38 temp If we want to print out the item in the node printf(“%d”, tempitem); Or if we want to update the item in the node to 30 tempitem = 30;

To travel the entire linked list head 11 13 15 38 temp Then we need to move temp to the next node temp = tempnext;

To travel the entire linked list head 11 13 15 38 temp If we want to print out the item in the node printf(“%d”, tempitem); Or if we want to update the item in the node to 60 tempitem = 60;

To travel the entire linked list The step will continue until reaching the last node. We can sum up the steps in the previous slides to come out the the code in the next slide

To travel the entire linked list temp = head; do { printf(“%d ”, tempitem); temp = tempnext; }while(temp!=NULL); Q: Why can’t we set the condition to tempnext!=NULL ???

To travel the entire linked list Q: Why can’t we set the condition to tempnext!=NULL ??? Ans: The item of the last node cannot be printed out if you set the condition to tempnext!=NULL!!

Adding a node to a linked list

Adding a node to a linked list 3 cases Adding to the front Adding to the middle Adding to the end

Case 1: Adding to the front head 11 13 15 38 newNode 8 newNodenext = head; head = newNode;

Case 2: Adding to the middle head previous current 11 13 15 38 newNode 14 previousnext = newNode; newNodenext = current;

Case 3: Adding to the end tempnext = newNode; head temp 11 13 15 38 newNode 60 tempnext = newNode; temp = newNode; (Optional, depends on whether you need to update the temp)

Deleting a node from a linked list

Deleting a node from a linked list 3 cases Deleting at the front Deleting at the middle Deleting at the end

Case 1: Deleting at the front head 11 13 15 38 temp temp = head; head = headnext; free(temp);

Case 2: Deleting at the middle previous current head 11 13 15 38 previous current head 11 13 15 38 previousnext = currentnext; free(current);

Case 3: Deleting at the end previous current head 11 13 15 38 previous current head 11 13 15 38 previousnext = NULL; free(current);

Interchanging 2 nodes

Interchanging the 2nd and the 3rd nodes head previous current Original List: 11 13 15 38 2 Updated List: head previous current 11 13 15 38 3 1

Interchanging the 2nd and the 3rd nodes Code: headnext = current; currentnext = previous; previousnext = currentnext;

Terms Suppose the following struct is used for the example in next few slides struct Node { int item; struct Node* next; };

Terms Using typedef to name a struct Node pointer: typedef struct Node* NodePtr; Declare a new node pointed by node struct Node* node; Or NodePtr node; Remark: Every variable needs to be declared before use.

Terms Create a new node pointed by node Case 1: If the node hasn’t been declared struct Node* node; node = (struct Node*malloc(sizeof(struct Node)); Case 2: If the node has been declared already

Go to the nth node for(i=1; i<index; i++) temp = tempnext; Assume the variable “index” holds the position of the required node. If index=1, that means the 1st node, if 2, that means the 2nd one, and so on. for(i=1; i<index; i++) temp = tempnext; printf(“item = %d”, tempitem);

Empty List To indicate an empty list, we assign Head to NULL Head = NULL;

Losing Nodes After executing the below code Head 11 15 38 NULL Original Linked List After executing the below code Head = (struct Node*)malloc(sizeof(struct Node)); Head 10 11 15 38 NULL Losing Nodes

Losing Nodes If we do this way, Head will now point to a new location and this causes the other nodes in the linked list not pointed by any pointers anymore. These nodes are called losing nodes.

Losing Nodes These losing nodes becomes useless memory locations which cannot be reused again and this will waste memory locations

Comparison to Arrays Compare to array, linked list is easier to manipulate in terms of insertion and deletion. It is easier to insert and delete a node in a linked list compared to array because for array, we will have to copy all the data over to make room for a new data

Garbage Collection Garbage is useless memory location which is lost during manipulation of linked list. Those memory locations are not accessed by any pointers any more and cannot be reused again. Garbages will cause wastage of memory Too many garbages will cause memory overflow To locate and remove all garbages so that they can be reused again are called Garbage Collection

A summary of a linked list Each node in the linked list contains 2 members, an information field and a next address(pointer) field. The information field contains the actual data and the pointer field contains the address of the next node in the list. The entire linked list is accessed by a pointer variable called Head. Head is NULL if the linked list is empty.

~ END ~