Data Structure & Algorithms

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Lists CS 3358.
COMP171 Fall 2005 Lists.
Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked List
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
Summary of lectures (1 to 11)
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
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 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.
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.
Chapter 5 – Dynamic Data Structure Part 2: Linked List DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
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 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
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.
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.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Chapter 16: Linked Lists.
Unit – I Lists.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Lists CS 3358.
Lecture - 6 On Data Structures
Linked Lists.
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Chapter 18: Linked Lists.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Linked Lists.
Presentation transcript:

Data Structure & Algorithms Lecture 6 Linked List

Definition - List A list is a collection of items that has a particular order It can have an arbitrary length Objects / elements can be inserted or removed at arbitrary locations in the list A list can be traversed in order one item at a time

List Overview Linked lists Basic operations of linked lists Abstract data type (ADT) Basic operations of linked lists Insert, find, delete, print, etc. Variations of linked lists Singly linked lists Circular linked lists Doubly linked lists Circular doubly linked list

Linked List Terminologies Traversal of List Means to visit every element or node in the list beginning from first to last. Predecessor and Successor In the list of elements, for any location n, (n-1) is predecessor and (n+1) is successor. In other words, for any location n in the list, the left element is predecessor and the right element is successor. Also, the first element does not have predecessor and the last element does not have successor.

Linked Lists A linked list is a series of connected nodes B C  Head 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 node A data pointer

Lists – Another perspective A list is a linear collection of varying length of homogeneous components. Homogeneous: All components are of the same type. Linear: Components are ordered in a line (hence called Linear linked lists). Arrays are lists..

Arrays Vs Lists Arrays are lists that have a fixed size in memory. The programmer must keep track of the length of the array No matter how many elements of the array are used in a program, the array has the same amount of allocated space. Array elements are stored in successive memory locations. Also, order of elements stored in array is same logically and physically.

Arrays Vs Lists A linked list takes up only as much space in memory as is needed for the length of the list. The list expands or contracts as you add or delete elements. In linked list the elements are not stored in successive memory location Elements can be added to (or deleted from) either end, or added to (or deleted from)the middle of the list.

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.

An Array A Linked List

Basic Operations of Linked List IsEmpty: determine whether or not the list is empty InsertNode: insert a new node at a particular position FindNode: find a node with a given value DeleteNode: delete a node with a given value DisplayList: print all the nodes in the list

An integer linked list First Node of List Last Node of List list 10 13 5 2 data next NULL

Creating a List node struct Node { int data; // data in node Node *next; // Pointer to next node }; Node *p; p = new Node; p - > data = 10; p - > next = NULL; p 10

The NULL pointer NULL is a special pointer value that does not reference any memory cell. If a pointer is not currently in use, it should be set to NULL so that one can determine that it is not pointing to a valid address: int *p; p = NULL;

Adding a node to a list Node *p, *q; p = new Node; p - > data = 10; p - > next = NULL; q = new Node; q - > data = 6; q - > next = NULL; p - > next = q; p 10 q 6 10 6 p q

Accessing List Data Node 1 Node 2 10 6 p Expression p p - > data p - > next p - > next - > data p - > next - > next Value Pointer to first node (head) 10 Pointer to next node 6 NULL pointer

Using typedef with pointers struct Node { int data; // data in node Node *next; // Pointer to next node }; typedef Node *NodePtr; // NodePtr type is a pointer // to a Node Node *p; // p is a pointer to a Node NodePtr q; // q is a pointer to a Node

Building a list from 1 to n struct Node { int data; Node *next; }; Node *head = NULL; // pointer to the list head Node *lastNodePtr = NULL; // pointer to last node in list head lastNodePtr

Creating the first node Node *ptr; // declare a pointer to Node ptr = new Node; // create a new Node ptr - > data = 1; ptr - > next = NULL; head = ptr; // new node is first lastNodePtr = ptr; // and last node in list head 1 ptr lastNodePtr

Adding more nodes for (int i = 2; i < = n; i ++ ) { ptr = new Node; //create new node ptr - > data = i; ptr - > next = NULL; lastNodePtr - > next = ptr; // order is lastNodePtr = ptr; // important } head 1 2 ptr lastNodePtr

head 1 2 ptr lastNodePtr head 1 2 lastNodePtr ptr 3 Initially Create a new node with data field set to 3 Its next pointer should point to NULL

head 1 2 ptr lastNodePtr head 1 2 lastNodePtr ptr 3 The next pointer of the node which was previously last should now point to newly created node “lastNodePtr->next=ptr”

head 1 2 ptr lastNodePtr head 1 2 lastNodePtr ptr 3 The next pointer of the node which was previously last should now point to newly created node “lastNodePtr->next=ptr” LastNodePtr should now point to the newly created Node “lastNodePtr = ptr;”

head 1 2 ptr lastNodePtr head 1 2 lastNodePtr ptr 3 LastNodePtr should now point to the newly created Node “lastNodePtr = ptr;”

Re-arranging the view head 1 2 3 ptr lastNodePtr

Inserting a node in a list 2 5 8 head prevNode currNode 6 ? ptr Step 1: Determine where you want to insert a node. Step 2: Create a new node: Node *ptr; ptr = new Node; ptr - > data = 6;

Node *ptr, *currNode, *prevNode ; prevNode = head; ptr = new Node; ptr->data = 6; ptr->next = NULL; currNode = head->next; While (currNode->data < ptr->data) { prevNode = currNode; currNode = currNode->next; } Note: when this loop terminates prevNode and currNode are at a place where insertion will take place. Only the “LINKS” or pointers of the list remain to be adjusted

Continuing the insert Step 3: Make the new node point to the current Node pointer. ptr - > next = currNode; Step 4: Make previous node point to the new node: prevNode - > next = ptr; 2 5 8 head 6 prevNode ptr currNode Now The new link has been added in the linked list

Deleting a node from a list 2 5 8 head prevNode delNode Step 1: Redirect pointer from the Node before the one to be deleted to point to the Node after the one to be deleted. prevNode - > next = delNode - > next; 2 5 8 head prevNode delNode

Finishing the deletion Step 2: Remove the pointer from the deleted link. delNode - > next = NULL; 2 5 8 head prevNodePtr delNode Step 3: Free up the memory used for the deleted node: delete delNode;

List Operations - Summarized

Traversing a Linked List

Insertion in a Linked List

Deletion from a Linked List