List data structure This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping.

Slides:



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

Linked Lists Geletaw S..
Lists CS 3358.
COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
CHP-5 LinkedList.
Review Pseudo Code Basic elements of Pseudo code
Review Learn about linked lists
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Data Structures Using C++ 2E
CHP-4 QUEUE.
Lecture No.01 Data Structures Dr. Sohail Aslam
Dr. Engr. Sami ur Rahman Assistant Professor Department of Computer Science University of Malakand Data Structure: List.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
D ESIGN & A NALYSIS OF A LGORITHM 08 – P RIORITY Q UEUE Informatics Department Parahyangan Catholic University.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Data Structures Using Java1 Chapter 4 Linked Lists.
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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Linked Lists © John Urrutia 2013, All Rights Reserved1.
Linked List by Chapter 5 Linked List by
Data Structures Using C++1 Chapter 5 Linked Lists.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
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.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
CSCS-200 Data Structure and Algorithms Lecture
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Unit – I Lists.
Queues Chapter 4.
Design & Analysis of Algorithm Priority Queue
Data Structure Interview Question and Answers
CE 221 Data Structures and Algorithms
Lists CS 3358.
Lecture - 6 On Data Structures
Linked Lists.
UNIT-3 LINKED LIST.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Data Structures Interview / VIVA Questions and Answers
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Linked Lists.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Presentation transcript:

List data structure

This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping list, groceries list, list of people to invite to a dinner, list of presents to give etc. In this course, we will see how we use lists in programming. A list is the collection of items of the same type. The items, or elements of the list, are stored in some particular order. It is possible to insert new elements at various positions in the list and remove any element of the list.

List data structure List is a set of elements in a linear order. Suppose we have four names a1, a2, a3, a4 and their order is as (a3, a1, a2, a4) i.e. a3, is the first element, a1 is the second element, and so on. The order is important here; this order is either due to sorting, the order may be due to the importance of the data items.

Operations a programmer performs with a list data structure. Operation NameDescription createList()Create a new list (presumably empty) copy()Set one list to be a copy of another clear();Clear a list (remove all elements) insert(X, ?)Insert element X at a particular position in the list remove(?)Remove element at some position in the list get(?)Get element at a given position update(X, ?)Replace the element at a given position with X find(X)Determine if the element X is in the list length()Returns the length of the list.

List Data Structure We need to know what is meant by “particular position”. There are two possibilities: Use the actual index of element: i.e. insert it after element 3, get element number 6. This approach is used with arrays Use a “current” marker or pointer to refer to a particular position in the list. The first option is used in the data structures like arrays. When we have to manipulate the arrays, we use index like x[3], x[6]. In the second option we do not use first, second etc for position but say wherever is the current pointer. Just think of a pointer in the list that we can move forward or backward. When we say get, insert or update while using the current pointer, it means that wherever is the current pointer, get data from that position, insert data after that position or update the data at that position. In this case, we need not to use numbers. But it is our responsibility that current pointer is used in a proper way.

List Data Structure If we use the “current” marker, the following four methods would be useful: FunctionsDescription start() Moves the “current” pointer to the very first element tail() Moves the “current” pointer to the very last element next() Move the current position forward one element back() Move the current position backward one element

List Implementation Now we will see what the implementation of the list is and how one can create a list. Suppose we want to create a list of integers. For this purpose, the methods of the list can be implemented with the use of an array inside. For example, the list of integers A= (2, 6, 8, 7, 1) can be represented in the following manner where the current position is 3. A currentsize Index

add Method The add method is used for adding an element to the list. Suppose we want to add a new element in the list i.e. add(9). To add(9) to the list at the current position, at first, we have to make space for this element. For this purpose, we shift every element on the right of 8 (the current position) to one place on the right. Thus after creating the space for new element at position 4, the array can be represented as A currentsize Index Now in the second step, we put the element 9 at the empty space i.e. position 4. Thus the array will attain the following shape. A currentsize Index

next Method The next method moves the current position one position forward. In this method, we do not add a new element to the list but simply move the pointer one element ahead. There is an array to store the list in it. We also have two variables- current and size to store the position of current pointer and the number of elements in the list.

remove Method The remove method removes the element residing at the current position. Suppose there are 6 elements (2, 6, 8, 9, 7, 1) in the list. The current pointer is pointing to the position 5 that has the value 7. We remove the element, making the current position empty. The size of the list will become 5. This is represented in the following figure. We fill in the blank position left by the removal of 7 by shifting the values on the right of position 5 to the left by one space. This means that we shift the remaining elements on the right hand side of the current position one place to the left A26891 currentsize Index A26891 currentsize Index

find Method find (x) function is used to find a specific element in the array. We pass the element, which is to be found, as an argument to the find function. This function then traverses the array until the specific element is found. If the element is found, this function sets the current position to it and returns 1 i.e. true. On the other hand, if the element is not found, the function returns 0 i.e. false. Following is the code of this find(x) function in C++. int find (int x) { int j ; for (j = 1; j < size + 1; j++ ) if (A[j] == x ) break ; if ( j < size + 1) // x is found { current = j ;//current points to the position where x found return 1 ;// return true } return 0 ; //return false, x is not found }

Other Methods There is a get() method, used to get the element from the current position in the array. return A[current] ; Another function is update(x). This method is used to change (set) the value at the current position. A [current] = x ; Then there is a method length( ).This method returns the size of the list. return size ; The back() method decreases the value of variable current by 1. In other words, it moves the current position one element backward. current -- ; The start() method sets the current position to the first element of the list. We know that the index of the array starts from 0 but we use the index 1 for the starting position. We do not use the index zero. So we set the current position to the first element by writing. current = 1 ; Similarly, the end() method sets the current position to the last element of the list i.e. size. current = size ;

Analysis of Array List Add Worst Case: To add an element at the beginning of the list is the worst case of add method. Best Case: We have to shift no element if we add at the end. Remove Worst Case: To remove an element at the beginning of the list is the worst case of remove method. Average Case: In average cases of the remove method we expect to shift half of the elements. Best Case: We have to shift no element if we remove at the end. Find Worst Case: The worst case of the find method is that it has to search the entire list from beginning to end. Average Case: On average the find method searches at most half the list.

List using Linked Memory

When we have declared the size of the array, it is not possible to increase or decrease it during the execution of the program. If we need more elements to store in the array, there is need of changing its size in the declaration. We have to compile the program again before executing it. To avoid such problems, usually faced by the programmers while using an array, there is need of using linked memory in which the various cells of memory, are not located continuously. In this process, each cell of the memory not only contains the value of the element but also the information where the next element of the list is residing in the memory. It is not necessary that the next element is at the next location in the memory. It may be anywhere in the memory. We have to keep a track of it.

Linked List For the utilization of the concept of linked memory, we usually define a structure, called linked list.

Linked Lists A linked list, or one-way list is a linear collection of data elements called nodes, where linear order is given by means of pointer. A linked list is a series of connected nodes Each node contains at least A piece of data (any type) Pointer to the next node in the list Head: pointer to the first node The last node points to NULL A  Head BCA datapointer node

Linked List Now let’s consider our previous list, used with an array i.e. 2, 6, 8, 7, 1. Following is the figure which represents the list stored as a linked list. We also have a pointer current to point to the current node of the list. We need this pointer to add or remove current node from the list. In the linked list, the current is a pointer and not an index as we used while using an array. 2 Head 6178  Current

Representation of Link List in Computer Memory

Inserting a new node Possible cases of Insertion 1. Insert into an empty list 2. Insert in front 3. Insert at back 4. Insert in middle But, in fact, only need to handle two cases Insert as the first node (Case 1 and Case 2) Insert in the middle or at the end of the list (Case 3 and Case 4)

Inserting to the Front 1. Allocate a new node 2. Insert new element 3. Have new node point to old head 4. Update head to point to new node head head 93

Inserting to the End 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node head // 93 //

Inserting to the Middle 1. Allocate a new node 2. Insert new element 3. Go to the node that should follow the one to add 4. Have that node point to the new node 5. Have new node point to node next node to the found node head // 93 // 142

Searching a Linked List Searching refers to finding a particular ITEM in the data. Binary search algorithm can not be applied to sorted link list since there is no way indexing the middle element in the list. This property is one of the main drawbacks in using a linked list as a data structure

Deleting a node Possible cases of Deletion 1. Delete in front 2. Delete at back 3. Delete in middle There are two special cases Delete first node Delete the node in middle or at the end of the list

The Scenario 417 head head head head 42

Traversing a Link List Traversal means “visiting” or examining each node. Simple linked list Start at the beginning Go one node at a time until the end

Link List in C# A linked list is a collection of class objects called nodes. Each node is linked to its successor node in the list using a reference to the successor node. A node is made up of a field for storing data and the field for the node reference. The reference to another node is called a link.

AN OBJECT-ORIENTED LINKED LIST DESIGN A linked list will involve at least two classes. We’ll create a Node class and instantiate a Node object each time we add a node to the list. The nodes in the list are connected via references to other nodes. These references are set using methods created in a separate LinkedList class.

The Node Class A node is made up of two data members: Element, which stores the node’s data; and Link, which stores a reference to the next node in the list. Here’s the code for the Node class: public class Node { public Object Element; public Node Link; public Node() { Element = null; Link = null; } public Node(Object theElement) { Element = theElement; Link = null; }

The LinkedList Class The LinkedList class is used to create the linkage for the nodes of our linked list. The class includes several methods for adding nodes to the list, removing nodes from the list, traversing the list, and finding a node in the list. Here’s the code for the LinkedList class: public class LinkedList { protected Node header; public LinkedList() { header = new Node(); }... }

Insert Method private Node Find(Object item) { Node current = new Node(); current = header; while(current.header != item) current = current.Link; return current; } public void Insert(Object newItem, Object after) { Node current = new Node(); Node newNode = new Node(newItem); current = Find(after); newNode.Link = current.Link; current.Link = newNode; }

Remove Method private Node FindPrevious(Object n) { Node current = header; while(!(current.Link == null) && (current.Link.Element != n)) current = current.Link; return current; } public void Remove(Object n) { Node p = FindPrevious(n); if (!(p.Link == null)) p.Link = p.Link.Link; }

PrintList Method public void PrintList() { Node current = new Node(); current = header; while (!(current.Link == null)) { Console.WriteLine(current.Link.Element); current = current.Link; }

Analysis of Link List add and remove In case of an array, if we have to add an element in the centre of the array, the space for it is created at first. For this, all the elements that are after the current pointer in the array, should be shifted one place to the right. In Link List we insert a new node after the current node in the chain. For this, we have to change two or three pointers while changing the values of some pointer variables. Its cost, in terms of CPU time or computing time, is not much as compared to the one with the use of arrays.

Analysis of Link List find The worst-case in find is that we may have to search the entire list. In find, we have to search some particular element say x. If found, the currentNode pointer is moved at that node. As there is no order in the list, we have to start search from the beginning of the list. We have to check the value of each node and compare it with x (value to be searched). If found, it returns true and points the currentNode pointer at that node otherwise return false. Suppose that x is not in the list, in this case, we have to search the list from start to end and return false. If we compare this with array, it will be the same. We don’t know whether x is in the array or not. So we have to search the complete array.

Variations of Linked Lists Doubly linked lists Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A Head B  C 

Variations of Linked Lists Circular linked lists The last node points to the first node of the list How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A Head BC

Josephus Problem Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains N=10, M= Eliminated

Josephus Problem Suppose if the value of N is 300 or 400 and the value of M is 5 or 10. Now who will be the leader? This is a mathematical problem where we can change the values of N and M. There is a formula where the values of N, M are allotted. You can calculate who should become the leader. Here we will solve this with the circular link list. We arrange these numbers in a circularly-linked list, point the head pointer at the starting number and after calling the next method for three times, we will reach the node which is to be removed. We will use the remove method to remove the node. Then the next method is called thrice from there and the node is removed. We will continue this till we have only one node.

Array versus Linked Lists Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. Dynamic: a linked list can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. In contrast, the size of a C++ array is fixed at compilation time. Easy and fast insertions and deletions To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. With a linked list, no need to move other nodes. Only need to reset some pointers.

What is a Priority Queues? Stores prioritized elements No notion of storing at particular position. Returns elements in priority order Order determined by key.

What’s so different? Stacks and Queues Removal order determined by order of inserting Priority Queue Order determined by key Key may be part of element data or separate Order of returned elements is not FIFO or LIFO (as in queue or stack)

Priority Queue In Priority Queue data elements are arranged according to priority values and allows the insertion of any order but allows to remove data elements according to their priority values.

Continue….  The highest priority element is always at the front of the queue and is removed first of all  The highest priority can be either the minimum value of all the items, or the maximum value  We will assume the highest priority is the maximum value  Note that in a priority queue "first in first out" does not apply in general.

Why Priority Queue ? There are many practical situations which employ priority queue.  Scheduler for an Operating System  Printer Queue  Sorting Heap Sort

The Concept of Key Suppose that you have a few assignments from different courses. Which assignment will you want to work on first? CoursePriorityDue Date Database Management System326 th -December-2009 Data Structure And Algorithm126 th -November-2009 Computer Architecture And Organization26 th -December-2009

Representation of Priority Queue  Queue can be represented as Array Linked List Heap

One-Way List Representation of a Priority Queue Each node in the list will contain three items of information: an information field INFO, a priority number PRN and a link number LINK. A node X precedes a node Y in the list: 1. When X has higher priority than Y or 2. When both have the same priority but X was added to the list before Y.

Array Representation of Priority Queue Another way to maintain a priority queue in memory is to use a separate queue for each priority number. Each such queue will appear in its own circular array and must have its own pair of pointers, FRONT and REAR If each queue is allocated the same amount of space, a two –dimensional array QUEUE can be used instead of the linear array.

Linked List based Implementation of Priority Queue

Continue….

Implementation of Operation

Continue….