Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Lists CS 3358.
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Lists: An internal look
CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
M180: Data Structures & Algorithms in Java
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structures: A Pseudocode Approach with C
1 Linked Lists Gordon College Prof. Brinton. 2 Linked List Basics Why use? 1.Efficient insertion or deletion into middle of list. (Arrays are not efficient.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 ADT Sorted List.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Data Structure Data structure uses collection of related variables that can be accessed.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 16 Stack and Queues part2
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
More Linking Up with Linked Lists Chapter 11 5/19/2015 Adopted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Brief Review.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Review Dr. Yingwu Zhu. Outline ADT concept C++ Class List with class implementation.
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.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
C++ Programming:. Program Design Including
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Lecture - 6 On Data Structures
Data Structure Dr. Mohamed Khafagy.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 4 Linked Lists.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Arrays and Linked Lists
Brief Review of ADTs and Class Implementation
Review & Lab assignments
Chapter 16 Linked Structures
Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008

Outline linked lists implementation An Array-Based Implementation of Linked Lists

List Implantation by Linked list In any structure used to store the elements of a list, it must be possible to perform at least the following operation: 1. Locate the First element 2. Given the location of any list element, find its successor 3. Locate the end of the list

Linked List Linked list nodes contain Data part – stores an element of the list Next part – stores link/pointer to next element (when no next element, null value)

Design the list class Should contain at least the following function members Constructor empty() insert() delete() display()

Construction To construct an empty list, we simply make first a null link to indicate that it does not refer to any node: first = null_value ;

Empty We can then perform the Empty operation--- determining whether a list is empty, simply by checking whether first is null: first == null_value ?

Traverse We begin by initializing an auxiliary variable ptr to point to the first node: Initialize a variable ptr to point to first node Process data where ptr points

Traverse Traverse (ctd) set ptr = ptr->next, process ptr->data Continue until ptr == null

Insertion To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part The second step is to connect this new node to existing list Two cases in this situation: (1) insertion after some element in the list and (2) insertion at the beginning of the list

Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

Insertion Note: insertion also works at end of list pointer member of new node set to null Insertion at the beginning of the list predptr must be set to first pointer member of newptr set to that value (Where first points to) first set to value of newptr In all cases, no shifting of list elements is required !

Deletion For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

Deletion Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17) Do a bypass operation : Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate the node being deleted. predptr ptr To free space

Deletion The second case is easier Just set the first points to the second node in the list and then returning the deleted node to the storage pool

Linked Lists - Advantages Access any item as long as external link to first item maintained Insert new item without shifting Delete existing item without shifting Can expand/contract as necessary

Linked Lists - Disadvantages No longer have direct access to each element of the list Many sorting algorithms need direct access Binary search needs direct access Access of n th item now less efficient must go through first element, and then second, and then third, etc.

Outline linked lists implementation An Array-Based Implementation of Linked Lists

Pointer Based Node Since each node has two different parts, a data part and a next part, it is nature to have a node class with two data members. class Node { ElementType data; Node *next; }

Pointer Based Node To declare a pointer to a node: Node *ptr To allocate a new node pointer to by ptr: ptr = new Node; ptr = new Node(data_nalue); ptr = new Node(data_value, link_value); To access the data nad next part of the node pointed to by ptr: ptr -> data ptr -> next

Array-Based Implementation of Linked Lists Given a list with names Implementation would look like this

How to do it??? First of all, define a 2D array int array[10][2];

How to do it??? Constructor( ) we make “first” variable equals to 7 to indicate we start with position 7 first ==7; How to choose a start point??

How to do it??? Put 88 into data position and set next position to NULL array[first][0]=88; array[first][1]=-1; size==1;

How to do Insertion??? Find a empty position (in this example it’s node 1) Insert(array, 1) { ptr==1; // find a empty position pre_ptr==7; // you need a function to find pre_ptr array[new_node][0]==54; array[new_node][1]==array[pre_ptr][1]; array[pre_ptr][1]==ptr; size++; }

Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

How to do Deletion???

Implementation Details For Insertion, there are also two cases to consider: insertion after some element in the list and insertion at the beginning of the list For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list