Linked List - I. Abstract Data Type (ADT) ADT is a data type whose properties (domain and operations) are specified independently of any particular implementation.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Linked Lists.
Linked Lists Chapter 4.
Linear Lists – Array Representation
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Linear Lists – Linked List Representation
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linear collections.
Lists: An internal look
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
ADTs unsorted List and Sorted List
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Unsorted Lists CS 308 – Data Structures. What is a list? A list is a homogeneous collection of elements. Linear relationship between elements: (1) Each.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
2004 A.M. Turing Award Winners Vinton G. Cerf Robert E. Kahn Citation For pioneering work on internetworking, including the design and implementation of.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
Linked List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique.
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.
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
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.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 5 Linked Lists II
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Data Structure & Algorithms
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CS505 Data Structures and Algorithms
C++ Plus Data Structures
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Data Structures ADT List
Data Structures ADT List
Doubly Linked List Implementation
Chapter 16 Linked Structures
Data Structures ADT List
Linked Lists.
Data Structures and Algorithms Memory allocation and Dynamic Array
TA: Nouf Al-Harbi Data Structures LAB 2 TA: Nouf Al-Harbi
Doubly Linked List Implementation
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Linked List - I

Abstract Data Type (ADT) ADT is a data type whose properties (domain and operations) are specified independently of any particular implementation. –Logical (or ADT) level: abstract view of the domain and operations. –Implementation level: specific representation of the structure to hold the data items, and the coding for operations.

4 Basic Kinds of ADT Operations Constructor/Destructor – creates/deletes a new instance (object) of an ADT. Transformer -- changes the state of one or more of the data values of an instance. Observer -- allows us to observe the state of one or more of the data values of an instance without changing them. Iterator -- allows us to process all the components in a data structure sequentially.

List Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor. Length The number of items in a list; the length can vary over time.

Class ShoppingList class ShoppingList { public : intLengthIs ( ) const ; void RetrieveItem (int pos) ; void InsertItem (const Item& item, int pos ) ; void RemoveItem (int pos); void DeleteAll ( ); private : int size ; Node*front ; } ;

Singly Linked List Diagram Pointer to front node ABC NULL Nodes Data carried in nodePointer to next node

Class Node A Item item;Node *link; class Node { public: Node(Item i){item = i}; private: Item item; Node *link; }; Insert an item/node into shopping list? Delete an item/node?

Front Insert Example (1 of 6) front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D ?? void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, //ignore prev ptr for the FrontInsert example *cur; Inserting D at position 0 size 3

Front Insert Example (2 of 6) front ABC itemlinkitemlinkitemlink curpos 0 anItem D ? assert(pos >= 0 && pos <= size); size++; size 4

Front Insert Example (3 of 6) front ABC itemlinkitemlinkitemlink curpos 0 anItem D if (pos == 0) { // Inserting at the front cur = front; size 4

Front Insert Example (4 of 6) front ABC itemlinkitemlinkitemlink curpos 0 anItem D front = new Node(anItem); size 4 D itemlink ?

Front Insert Example (5 of 6) front ABC itemlinkitemlinkitemlink curpos 0 anItem D front->link = cur; size 4 D itemlink

Front Insert Example (6 of 6) front ABC itemlinkitemlinkitemlink return; } size 4 D itemlink

Empty Insert Example (1 of 6) size curpos 0 anItem A ? void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, //ignore prev for EmptyInsert example *cur; Inserting A at position 0 in empty list front 0

Empty Insert Example (2 of 6) size curpos 0 anItem A ? assert(pos >= 0 && pos <= size); size++; front 1

Empty Insert Example (3 of 6) size curpos 0 anItem A if (pos == 0) { // Inserting at the front cur = front; front 1

Empty Insert Example (4 of 6) size curpos 0 anItem A front = new Node(anItem); front 1 A itemlink ?

Empty Insert Example (5 of 6) size curpos 0 anItem A front->link = cur; front 1 A itemlink

Empty Insert Example (6 of 6) size return; } front 1 A itemlink

Middle Insert Example (1 of 8) void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 2 front ABC itemlinkitemlinkitemlink prevcurpos 2 anItem D ?? size 3

Middle Insert Example (2 of 8) assert(pos >= 0 && pos <= size); size++; front ABC itemlinkitemlinkitemlink prevcurpos 2 anItem D ?? size 4

Middle Insert Example (3 of 8) prev = NULL; cur = front; front ABC itemlinkitemlinkitemlink prevcurpos 2 anItem D size 4

Middle Insert Example (4 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } First Iteration front ABC itemlinkitemlinkitemlink prevcurpos 1 anItem D size 4

Middle Insert Example (5 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Second Iteration front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4

Middle Insert Example (6 of 8) prev->link = new Node(anItem); front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4 D ? itemlink

Middle Insert Example (7 of 8) prev->link->link = cur; front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4 D itemlink

Middle Insert Example (8 of 8) } front ABC itemlinkitemlinkitemlink size 4 D itemlink

End Insert Example (1 of 8) void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; Inserting D at position 3 front ABC itemlinkitemlinkitemlink prevcurpos 3 anItem D ?? size 3

End Insert Example (2 of 8) assert(pos >= 0 && pos <= size); size++; front ABC itemlinkitemlinkitemlink prevcurpos 3 anItem D ?? size 4

End Insert Example (3 of 8) prev = 0; cur = front; front ABC itemlinkitemlinkitemlink prevcurpos 3 anItem D 0 size 4

End Insert Example (4 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } First Iteration front ABC itemlinkitemlinkitemlink prevcurpos 2 anItem D size 4

End Insert Example (5 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Second Iteration front ABC itemlinkitemlinkitemlink prevcurpos 1 anItem D size 4

End Insert Example (6 of 8) while (pos > 0) { prev = cur; cur = cur->link; pos--; } Third Iteration front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4

End Insert Example (7 of 8) prev->link = new Node(anItem); prev->link->link = cur; front ABC itemlinkitemlinkitemlink prevcurpos 0 anItem D size 4 D itemlink

End Insert Example (8 of 8) } front ABC itemlinkitemlinkitemlink size 4 D itemlink

void ShoppingList::insert(const Item & anItem, int pos) { Node *prev, *cur; assert(pos >= 0 && pos <= size); size++; if (pos == 0) { // Inserting at the front cur = front; front = new Node(anItem); front->link = cur; return; } prev = NULL; cur = front; while (pos > 0) { prev = cur; cur = cur->link; pos--; } prev->link = new Node(anItem); prev->link->link = cur; }

Remove Example (1 of 7) void ShoppingList::remove(int pos) { Node *cur, *prev; Removing at position 1 front ABC itemlinkitemlinkitemlink prevcurpos 1 ?? size 3

Remove Example (2 of 7) assert(pos >= 0 && pos < size); size--; front ABC itemlinkitemlinkitemlink prevcurpos 1 ?? size 2

Remove Example (3 of 7) prev = NULL; cur = front; front ABC itemlinkitemlinkitemlink prevcurpos 1 size 2

Remove Example (4 of 7) while (pos > 0) { prev = cur; cur = cur->link; pos--; } front ABC itemlinkitemlinkitemlink prevcurpos 0 size 2 First iteration Delete the node pointed by cur

Remove Example (5 of 7) prev->link = cur->link; front ABC itemlinkitemlinkitemlink prevcurpos 0 size 2

Remove Example (6 of 7) delete cur; front ABC itemlinkitemlinkitemlink prevcurpos 0 size 2

Remove Example (7 of 7) } front AC itemlinkitemlink size 2

Delete All Example (1 of 8) void ShoppingList::deleteAll() { Node *kil; front ABC itemlinkitemlinkitemlink kil ? size 3

Delete All Example (2 of 8) while (front != NULL) { kil = front; front = front->link; ABC itemlinkitemlinkitemlink kil frontsize 3 First iteration

Delete All Example (3 of 8) kil->link = NULL; delete kil; } ABC itemlinkitemlinkitemlink kil frontsize 3 First iteration

Delete All Example (4 of 8) while (front != NULL) { kil = front; front = front->link; BC itemlinkitemlink kil frontsize 3 Second iteration

Delete All Example (5 of 8) kil->link = NULL; delete kil; } BC itemlinkitemlink kil frontsize 3 Second iteration

Delete All Example (6 of 8) while (front != NULL) { kil = front; front = front->link; C itemlink kil frontsize 3 Third iteration

Delete All Example (7 of 8) kil->link = 0; delete kil; } C itemlink kil frontsize 3 Third iteration 0

Delete All Example (8 of 8) size = 0; } frontsize 0