1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Lists CS 3358.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists CENG 213 Data Structures.
Data Structures: A Pseudocode Approach with C
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked Lists
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Starting Out with C++, 3 rd Edition 1 Chapter 17 Linked Lists.
Data Structures Using C++ 2E
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
 DATA STRUCTURE DATA STRUCTURE  DATA STRUCTURE OPERATIONS DATA STRUCTURE OPERATIONS  BIG-O NOTATION BIG-O NOTATION  TYPES OF DATA STRUCTURE TYPES.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
Data Structure and Algorithms
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
UNIT-II Topics to be covered Singly linked list Circular linked list
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Unit – I Lists.
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
List ADT & Linked Lists.
Chapter 4 Linked Lists.
Linked lists.
Data Structures Interview / VIVA Questions and Answers
CSCI 3333 Data Structures Linked Lists.
Data Structures and Algorithms IT12112
Chapter 18: Linked Lists.
Data Structures & Algorithms
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Linked lists.
Linked Lists.
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1

2 Last Lecture Summary 2 Algorithms and Complexity Criteria for Algorithm Analysis Complexity Analysis Various Complexity Functions  Big O, Big Omega, Big, Theta, Little O Properties of Big O notation Growth of functions

3 Why Data Structures are Required? Data may be organized in many different ways Logical or mathematical model of a particular organization of data is called a data structure The choice of a particular data model depends on two considerations.  First, it must be rich enough in structure to mirror the actual relationships of the data in the real world.  Secondly, the structure should be simple enough that one can effectively process the data when necessary

4 Data Structure Operations data appearing in our data structures are processed by means of certain operations In fact, the particular data structure that one chooses for a given situation depends largely on the frequency with which specific operations are performed

5 Data Structure Operations (Cont…) Following are the major operations: Traversing: Accessing each record exactly once so that certain items in the record may be processed. (This accessing and processing is sometimes called "visiting" the record.) Searching: Finding the location of the record with a given key value, or finding the locations of all records that satisfy one or more conditions Inserting: Adding a new record to the structure Deleting: Removing a record from the structure

6 Data Structure Operations (Cont…) Sometimes two or more of the operations may be used in a given situation;  e.g., we may want to delete the record with a given key, which may mean we first need to search for the location of the record. Following two operations, which are used in special situations, are also be considered: Sorting: Arranging the records in some logical order  (e.g., alphabetically according to some NAME key, or in numerical order according to some NUMBER key, such as social security number or account number) Merging: Combining the records in two different sorted files into a single sorted file Other operations, e.g., copying and concatenation, are also used

7 Options for implementing an ADT List Array has a fixed size  Data must be shifted during insertions and deletions Linked list is able to grow in size as needed  Does not require the shifting of items during insertions and deletions Size  Increasing the size of a resizable array can waste storage and time Storage requirements  Array-based implementations require less memory than a pointer-based ones

8 What’s wrong with Array and Why lists? Disadvantages of arrays as storage data structures:  slow searching in unordered array  slow insertion in ordered array  Fixed size Linked lists solve some of these problems Linked lists are general purpose storage data structures and are versatile.

9 Comparing Array and Pointer-Based Implementations Access time  Array-based: constant access time  Pointer-based: the time to access the i th node depends on i Insertion and deletions  Array-based: require shifting of data  Pointer-based: require a list traversal

10 Array Limitations Arrays  Simple,  Fast but  Must specify size at construction time  Murphy’s law Construct an array with space for n  n = twice your estimate of largest collection Tomorrow you’ll need n+1  More flexible system?

11 Linked Lists Flexible space use  Dynamically allocate space for each element as needed  Include a pointer to the next item ç Linked list  Each node of the list contains the data item (an object pointer in our ADT) a pointer to the next node DataNext

12 Linked Lists Each data item is embedded in a link. Each Link object contains a reference to the next link in the list of items. In an array items have a particular position, identified by its index. In a list the only way to access an item is to traverse the list Is LL an ADT?

13 Linked List A Flexible structure, because can grow and shrink on demand. Elements can be:  Inserted  Accessed  DeletedAt any position Lists can be:  Concatenated together.  Split into sublists. Mostly used in Applications like:  Information Retrieval  Programming language translation  Simulation

14 List A List is a sequence of zero or more elements of a given type (say elementtype) Represented by a comma-separated sequence of elements: a 1, a 2,…a n Where, n >= 0 and each a i is of type elementtype. if n>= 1, a 1 is the first element a n is the last element if n = 0, we have an empty list

15 List The elements of a list can be linearly ordered.  a i precedes a i+1 for I = 1,2,3…n-1 a i follows a i-1 for I = 2,3,4…n The element a i is at position i. END(L) will return the position following position n in an n-element list L. Position END(L) has a varying distance as the list grows and shrinks, all other positions have a fixed distance from the beginning of the list.

16 Common Operations on List ADT INSERT(x,p,L): Insert x at position p in list L. If list L has no position p, the result is undefined. LOCATE(x,L): Return the position of x on list L. RETRIEVE(p,L): Return the element at position p on list L. DELETE(p,L): Delete the element at position p on list L. NEXT(p,L): Return the position following p on list L.

17 Common Operations on List ADT PREVIOUS(p,L): Return the position preceding position p on list L. MAKENULL(L): Causes L to become an empty list and returns position END(L). FIRST(L): Returns the first position on the list L. PRINTLIST(L): Print the elements of L in order of occurrence.

18 Linked List Pointer Based Implementation of Linked List ADT Dynamically allocated data structures can be linked together to form a chain. A linked list is a series of connected nodes (or links) where each node is a data structure. A linked list can grow or shrink in size as the program runs. This is possible because the nodes in a linked list are dynamically allocated.

19 List Operations If new information needs to be added to the list, the program –  a) Allocates another node  b) Inserts it into the series. If a piece of information is to be deleted from the list, the program –  a) Deletes the node containing the information

20 Array List Vs Linked List (The programmer doesn’t need to know how many nodes will be in the list. They are created in memory as needed). a) Speed of insertion or deletion from the list. e.g. with an array, to insert an element, requires all elements beyond the insertion point to be moved forward one position to make room for the new element. Similarly, to delete an element, requires all elements after the insertion point to be moved back one position to close the gap. When a node is inserted, or deleted from a linked list, none of the other nodes have to be moved!!!!

21 Composition of Linked List Each node in the linked list contains –  a) One or more members that represent data (e.g. inventory records, customer names, addresses, telephone numbers, etc).  b) A pointer, that can point to another node. Data Members Pointer

22 Composition of linked List A linked list is called “linked” because each node in the series (i.e. the chain) has a pointer to the next node in the list, e.g. List Head NULL a) The list head is a pointer to the first node in the list. b) Each node in the list points to the next node in the list. c) The last node points to NULL (the usual way to signify the end). Note, the nodes in a linked list can be spread out over the memory.

23 Linked List  Actual picture in memory: head current26871 head current 1065

24 List Declarations How to declare a linked list in C or C++? Step 1) Declare a data structure for the nodes. e.g. the following struct could be used to create a list where each node holds a float - struct ListNode { float value; ListNode *next; };

25 List Declarations a) The first member of the ListNode struct is a float called value. It is to hold the node’s data. b) The second member is a pointer called next. It is to hold the address of any object that is a ListNode struct. Hence each ListNode struct can point to the next one in the list. The ListNode struct contains a pointer to an object of the same type as that being declared. It is called a self-referential data structure. This makes it possible to create nodes that point to other nodes of the same type.

26 List Declarations Step 2) Declare a pointer to serve as the list head, e.g ListNode *head; Before you use the head pointer, make sure it is initialized to NULL, so that it marks the end of the list. Once you have done these 2 steps (i.e. declared a node data structure, and created a NULL head pointer, you have an empty linked list. struct ListNode { float value; struct ListNode *next; }; ListNode *head;// List head pointer The next thing is to implement operations with the list.

27 Link List Operations There are 5 basic linked list operations Appending a node Traversing a list Inserting a node Deleting a node Destroying the list

28 Appending a Node To append a node to a linked list, means adding it to the end of the list. The appendNode function accepts a float argument, num. The function will - a) allocate a new ListNode structure b) store the value in num in the node’s value member c) append the node to the end of the list This can be represented in pseudo code as follows-

29 Appending a Node (Pseudocode) a) Create a new node. b) Store data in the new node. c) If there are no nodes in the list Make the new node the first node. Else Traverse the List to Find the last node. Add the new node to the end of the list. End If.

30 Appending a Node (Code) void appendNode(float num) { ListNode *newNode, *nodePtr; // Allocate a new node & store num newNode = new ListNode; newNode->value = num; newNode->next = NULL; // If there are no nodes in the list make newNode the first node if ( head != NULL) head = newNode; else// Otherwise, insert newNode at end { // Initialize nodePtr to head of list nodePtr = head; // Find the last node in the list while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node nodePtr->next = newNode; } }

31 Code Description - 1 We examine this important piece of code in detail. The function declares the following local variables – ListNode *newNode, *nodePtr; a) a)The newNode pointer will be used to allocate and point to the new node. b) b)The nodePtr pointer will be used to travel down the linked list, looking for the last node. The next few statements – i) create a new node ii) store num in its value member. newNode = new ListNode; newNode->value = num; newNode->next = NULL; The last statement above is important. This node will become the last node in the list, so its next pointer must point to NULL

32 Code Description - 2 Now test the head pointer to see if there are any nodes already in the list. If head points to NULL, we make the new node the first in the list. Do this by making head point to the new node, i.e. If (head != NULL) head = newNode; But, if head does not point to NULL, then there must already be nodes in the list. The else part must then contain code to - a) Find the end of the list b) Insert the new node.

33 Code Description - 3 else{// Otherwise, insert newNode at end // Initialize nodePtr to head of list nodePtr = head; // Find the last node in the list while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node nodePtr->next = newNode; } The code uses nodePtr to travel down the list. It does this by assigning nodePtr to head. nodePtr = head; A while loop is then used to traverse (i.e. travel through) the list, looking for the last node (that will have its next member pointing to NULL). while(nodePtr->next) nodePtr = nodePtr->next; Now the nodePtr is pointing to the last node in the list, so make its next member point to newNode.nodePtr->next = newNode; Remember, newNode->next already points to NULL.

34 Main Program // This program demonstrates a simple append // operation on a linked list. #include #include "FloatList.h” void main(void) { ListNode *head; *head = NULL; appendNode(2.5); appendNode(7.9); appendNode(12.6); } (This program displays no output.)

35 Program Step Through We step thru the above program, observing how the appendNode function builds a linked list to store the 3 argument values. The head pointer is automatically initialized to 0 (NULL), indicating the list is empty. The first call to appendNode passes 2.5 as the argument. A new node is allocated in memory. 2.5 is copied into its value member, and NULL is assigned to its next pointer.

36 Program Step Through - 1 newNode = new ListNode; newNode->value = num; newNode->next = NULL; The next statement to execute is the following if statement. if (head != NULL) head = newNode; Since head points to NULL, then the condition !head is true, so the statement, head = newNode is executed, making newNode the first node in the list. There are no more statements to execute, so control returns to function main.

37 Program Step Through - 2 There are no more statements to execute, so control returns to the function main. In the second call to appendNode, 7.9 is passed as the argument. Again, the first 3 statements create a new node, which stores the argument in the node’s value member, and assigns its next pointer to NULL. Visually this is -

38 Program Step Through - 3 Since head no longer points to NULL, the else part of the if statement is executed. else// Otherwise, insert newNode at end { // Initialize nodePtr to head of list nodePtr = head; // Find the last node in the list while (nodePtr->next) nodePtr = nodePtr->next; // Insert newNode as the last node nodePtr->next = newNode; } The first statement in the else block assigns the value in head to nodePtr. So, nodePtr and head point to the same node.

39 Program Step Through - 4 Look now at the next member of the node that nodePtr points at. Its value is NULL, so nodePtr->next also points to NULL. So, nodePtr is already at the end of the list, so the while loop terminates. The last statement, nodePtr->next = newNode, causes nodePtr->next to point to the new node. This appends newNode to the end of the list, as shown -

40 Program Step Through - 5 The third time appendNode is called, 12.6 is passed as argument. Again, the first 3 statements create a node with the argument stored in the value member. Now, the else part of the if statement executes. Again nodePtr is made to point to the same node as head.

41 Program Step Through - 6 Since nodePtr->next is not NULL, the while loop will execute. After its first iteration, nodePtr will point to the second node in the list. The while loop’s conditional test will fail after the first iteration because nodePtr->next now points to NULL. The last statement nodePtr->next = newNode causes nodePtr->next to point to the new node. This appends newNode to the end of the list, as shown -

42 Program Step Through - 7 The above is the final state of the linked list.

43 Traversing a Linked List The previous function appendNode, used a while loop that traverses, or travels through the linked list. We now demonstrate the displayList member function, that traverses the list, displaying the value member of each node. void displayList(void) { ListNode *nodePtr; nodePtr = head; while(nodePtr) { cout value << endl; nodePtr = nodePtr->next; }

44 Traverse List Pseudocode Assign list head to node pointer While node pointer is not NULL Display the value member of the node pointed to by node pointer. Assign node pointer to its own next member. End While.

45 // This program calls the displayList member function. // The funcion traverses the linked list displaying // the value stored in each node. #include #include "FloatList.h” void main(void) { ListNode *head; *head = NULL; appendNode(2.5); appendNode(7.9); appendNode(12.6); displaylist(); } Traverse List Program

46 Summary Data structure Operations  Traverse, Search, Insert, Delete, Sort, Merge Options for Implementing ADT List Array-based and Pointer based Linked List Linked List Operations  Append  Traverse