Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:

Slides:



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

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks, Queues, and Linked Lists
Data Structures ADT List
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CSE Lecture 12 – Linked Lists …
Linked Lists CENG 213 Data Structures.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Data Structures for Media Queues. Queue Abstract Data Type Queue Abstract Data Type Sequential Allocation Sequential Allocation Linked Allocation Linked.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
Data Structures: A Pseudocode Approach with C
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Chapter 4.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 7 Stacks II CS Data Structures I COSC 2006
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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 &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Introduction to Data Structures and Algorithms
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Linked Lists and Generics Written by J.J. Shepherd.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Chapter 18: Linked Lists.
Data Structures ADT List
Linked List (Part I) Data structure.
Programming Abstractions
Pointers & Dynamic Data Structures
CSI 1340 Introduction to Computer Science II
Data Structures Chapter 4: Linked Lists.
Presentation transcript:

Linked List (Part I)

Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example: Given an array which is arranged in ascending order. ○ Discuss how to insert a new element ‘1’ and how to delete the element ‘4’. Storage allocation is not flexible. 2467

Possible Improvements  The elements in an ordered list don’t need to be stored in consecutive memory space. The insertion and deletion of an element will not induce excessive data movement.  The element can be “dynamically” allocated.

Linked Representation  Data structure for a linked list: first Data Link (pointer): used to store the address of the next node. Node

Example BAT3CAT4FAT0 8 first CAT4 3 FAT BAT first

Insertion BAT3CAT4FAT0 8 first CAT4 3 FAT BAT first  Insert EAT into an ordered linked list 1) Get a new node a 2) Set the data field of a to EAT. 3) Set the link field of a to point the node after CAT, which contains FAT. Find the position where EAT is to be inserted. EAT CAT ) Set the link field of the node containing CAT to a.

Deletion BAT3CAT6EAT4 8 first 1 2 CAT6 3 FAT0 4 5 EAT4 6 7 BAT first  Remove CAT from the linked list 1) Set the link of BAT to EAT. 2) Deallocate CAT Find the address of CAT FAT0 BAT

Representation of a Linked List class ListNode { friend class LinkedList; public: ListNode(); ListNode(DataField value); ~ListNode(); private: DataField data; ListNode *link; }; class LinkedList { private: ListNode * first; }; first class LinkedList { private: ListNode * first; }; class ListNode { friend class LinkedList; public: ListNode(); ListNode(DataField value); ~ListNode(); private: DataField data; ListNode *link; }; datalink

Linked Ordered List  Suppose elements are arranged in ascending order.  ADT class LinkedOrderedList { public: LinkedOrderedList(); ~ LinkedOrderedList(); void Insert(DataField value); bool Delete(DataField value); //return false if value is not found. bool IsEmpty(); private: ListNode *first; };

Initialization  The constructor of ListNode:  The constructor of LinkedOrderedList: LinkedOrderList::LinkedOrderList() { first = NULL; } ListNode::ListNode(DataField value) { data = value; link = NULL; }

Algorithm of Insert() 01void LinkedOrderList::Insert(DataField value) 02{ 03 curr = first; 04 while (curr != NULL) 05 { 06if (curr->data >= value) 07 { 08 ListNode *a = new ListNode(value); 09 a->link = curr; 10 previous->link = a; 11 break; 12 } 13 previous = curr; 14 curr = curr->link; 15 } 16}

Insertion BAT3CAT4FAT0 8 first  Insert EAT into an ordered linked list curr EAT 4CAT6 previous 03 curr = first; 04 while (curr != NULL) 05 { 06 if (curr->data >= value) 07 { 08 ListNode *a = new ListNode(value); 09 a->link = curr; 10 previous->link = a; 11 break; 12 } 13 previous = curr; 14 curr = curr->link; 15 } a

Boundary Condition: Case 1  Consider to insert AT. There will be no previous node for AT. The update of first is required. BATCATFAT first AT

Boundary Condition: Case 2  Consider to insert GAT. BATCATFAT first GAT 03 curr = first; 04 while (curr != NULL) 05 { 06 if (curr->data >= value) 07 { 08 ListNode *a = new ListNode(value); 09 a->link = curr; 10 previous->link = a; 11 break; 12 } 13 previous = curr; 14 curr = curr->link; 15 } No statement is written to insert GAT at the end of the list.

Problem of Insert()  The function Insert() fails to deal with boundary conditions. The insertion is always performed between two existing nodes.  Improvements Add codes before- and after the while-statement for dealing with the boundary conditions. Always maintain two (dummy) nodes so that insertion can always be performed between two nodes.

Improvement Using Two Dummy Nodes  Maintain two dummy nodes at each end of the list. class LinkedList { private: ListNode * first, *last; }; LinkedOrderList::LinkedOrderList() { first = new ListNode(); last = new ListNode(); first->link = last; last->link = NULL; } firstlast  No need to update the pointer first.  Boundary conditions are eliminated (Insertion and Deletion always take place between two nodes).

New Version of Insert() 01void LinkedOrderList::Insert(DataField value) 02{ 03 previous = first; 04 curr = first->link; 05 while (curr != NULL) 06 { 07if (curr == last || curr->data >= value) 08 { 09 ListNode *a = new ListNode(value); 10 a->link = curr; 11 previous->link = a; 12 break; 13 } 14 previous = curr; 15 curr = curr->link; 16 } 17}

Algorithm of Delete() 01bool LinkedOrderList::Delete(DataField value) 02{ 03 if (IsEmpty()) 04 return false; previous = first; 07 curr = first->link; 08 while (curr != last) 09 { 10 if (curr->data == value) 11 { 12 previous->link = curr->link; 13 Deallocate curr; 14 return true; 15 } 16 previous = curr; 17 curr = curr->link; 18 } 19 return false; 20}

Deletion  Consider to remove BAT from the list. firstlast BAT 10 if (curr->data == value) 11 { 12 previous->link = curr->link; 13 Deallocate curr; 14 return true; 15 } previouscurrcurr->link

Destruction of Nodes  Remember to deallocate each node in the destructor. 01 LinkedOrderList ::~ LinkedOrderList() 02{ 03 curr = first; 04 while (curr != NULL) 05 { 06 next = curr->link; 07 Deallocate curr; 08 curr = next; 09 } 10}

Performance Analysis  Suppose there are n nodes in a linked list. Space complexity: ○ A linked list uses an exact amount of memory space to store these n nodes. ○ Space complexity to perform a insertion or deletion O(1). Time complexity to perform a insertion or deletion: ○ Consider a worst case in which nodes are always inserted and deleted at the end of the list. Therefore, the complexity is O(n). ○ Excessive request of allocation or deallocation of memory space for a node increases loading for the OS system (and may lower efficiency).

Linked Stack BACE0 datalink top Pop D Push D

Linked Queue  Deletion takes place at front; insertion at rear. BCDAE0 frontrear Pop Push E

Implementation of Linked Queue LinkedQueue:: LinkedQueue() { front = rear = NULL; }; bool LinkedQueue::IsEmpty() { if (front is NULL and rear is NULL) return true; return false; }

Implementation of Linked Queue void LinkedQueue::Push(Datafield value) { if (IsEmpty()) front = rear = new ListNode(value); else rear = rear->link = new ListNode(value); }; Datafield LinkedQueue::Pop() { if (IsEmpty()) output error; else { delNode = front; value = front->data; front = front->link; deallocate delNode; return value; } }; LinkedQueue::~ LinkedQueue() { while (!IsEmpty()) Pop(); };

Comparison  Compare stack/queue implemented using array and linked stack/queue. ArrayLinked list Memory spaceThe length of an array is fixed; Resize() is required if the stack is full. Memory space can be dynamically allocated. The storage is more compact. Execution time for Push() and Pop() The time complexity is O(1). The time complexity is also O(1). But the memory request increase overhead.

Polynomial Representation class Polynomial { private: ListNode *first; }; first class ListNode { friend class LinkedList; public: ListNode(int c, int e); ~ListNode(); private: int coef, exp; ListNode *link; }; 3210 f(x) =3x 2 +1 coefexp

Adding Polynomial  Example Consider the following two polynomials: a(x) =3x 14 +2x 8 +1 b(x) =8x 14 -3x x 6 b.first bi a.first ai

Case 1  ai->exp == bi->exp bi ai C.first Add coefficients and append to the result C. Advance ai and bi to next term

Case 2  ai->exp exp ai bi C.first Append the term indicated by bi to the result C. Advance bi to next term

Case 3  ai->exp > bi->exp bi ai C.first -310 Append the term indicated by ai to the result C. Advance ai to next term

Algorithm of Add() LinkedPolynomial LinkedPolynomial::Add(Polynomial B) { Create a new LinkedPolynomial C as result; ai = first; bi = B.first; while (ai != NULL && bi != NULL) { if (ai->exp == bi->exp) { Sum = ai->coef + bi->coef; if (Sum != 0) C.InsertBack(Sum, ai->exp); ai = ai->link; bi = bi->link; } else if (ai->exp exp) { C.InsertBack(bi->coef, bi->exp); bi = bi->link; } else if (ai->exp > bi->exp) { C.InsertBack(ai->coef, ai->exp); ai = ai->link; } for (; ai != NULL; ai = ai->link) C.InsertBack(ai->coef, ai->exp); for (; bi != NULL; bi = bi->link) C.InsertBack(bi->coef, bi->exp); return C; }