CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists.

Slides:



Advertisements
Similar presentations
EENG212 Algorithms and Data Structures
Advertisements

DATA STRUCTURES USING C++ Chapter 5
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
Data Structures: A Pseudocode Approach with C
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
1 Lectures on data structures and C 3ed CS(CSI33) By, CHANDRASHEKAR A.M.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
CSE1303 Part A Data Structures and Algorithms Lecture A6 – Dynamic Memory.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A17/18 – Revision.
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A4 – Basic Data Structures – Continued (Queues)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)
CSE1303 Part A Data Structures and Algorithms Lecture A17/18 – Revision.
CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A8 – Linked Stacks and Linked Queues.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
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.
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
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.
Algorithms and Data Structures
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
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:
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Programming Circular Linked List.
Pointers and Linked Lists
Pointers and Linked Lists
UNIT – I Linked Lists.
Binary Search Trees Chapter 7 Objectives
UNIT-3 LINKED LIST.
Linked Lists.
Basic Data Structures – Continued (Queues)
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Lists.
Chapter 4 Linked Lists
Chapter 20: Binary Trees.
Lectures on data structures and C 3ed CS(CSI33) By, CHANDRASHEKAR A.M.
Linked Lists Chris Wright Winter 2006.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Linked List Insert as a first node Insert as a last node
Linked Lists Chapter 4.
Chapter 16 Linked Structures
General List.
Presentation transcript:

CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists

2 Overview Operations for Lists. Implementation of Linked Lists. Double Linked Lists.

3 List Operations Go to a position in the list. Insert an item at a position in the list. Delete an item from a position in the list. Retrieve an item from a position. Replace an item at a position. Traverse a list.

4 Comparison Linked Storage –Unknown list size. –Flexibility is needed. Contiguous Storage –Known list size. –Few insertions and deletions are made within the list. –Random access

5 Linked List headPtr 0123

6 #ifndef LINKEDLISTH #define LINKEDLISTH #include #include "node.h" struct LinkedListRec { int count; Node* headPtr; }; typedef struct LinkedListRec List; void intializeList(List* listPtr); bool listEmpty(const List* listPtr); Node* setPosition(const List* listPtr, int position); void insertItem(List* listPtr, float item, int position); float deleteNode(List* listPtr, int position); #endif

7 void initializeList(List* listPtr) { listPtr->headPtr = NULL; listPtr->count = 0; } Initialize List count headPtr 0 NULL listPtr addr of list

8 Set Position check if position is within range start with address of head node set count to 0 while count is less than position –follow link to next node –increment count return address of current node

9 Node* setPosition(const List* listPtr, int position) { int i; Node* nodePtr = listPtr->headPtr; if (position = listPtr->count) { fprintf(stderr, “Invalid position\n”); exit(1); } else { for (i = 0; i < position; i++) { nodePtr = nodePtr->nextPtr; } return nodePtr; }

10 Set Position headPtr position 0x30a80x20300x i nodePtr

11 Set Position 2 position 0x i nodePtr 0x30a80x20300x headPtr

12 Set Position 2 position 0x30a8 2 i nodePtr 0x30a80x20300x headPtr

13 Insert 021 If we insert it at position 0. headPtr newNodePtr 103 headPtr 2

14 Instead, suppose we insert it at position headPtr newNodePtr headPtr 2 newNodePtr

15 void insertItem(List* listPtr, float item, int position) { Node* newNodePtr = makeNode(item); Node* prevPtr = NULL; if (position == 0) { newNodePtr->nextPtr = listPtr->headPtr; listPtr->headPtr = newNodePtr; } else { prevPtr = setPosition(listPtr, position-1); newNodePtr->nextPtr = prevPtr->nextPtr; prevPtr->nextPtr = newNodePtr; } listPtr->count++; }

16 Inserting – New List headPtr 0 position 0x30a8 NULL newNodePtr 0x30a8

17 Inserting – New List 0 position 0x30a8 newNodePtr 0x30a8 headPtr

18 Inserting – Start of List 0x30a8 0x2008 0x position 0x2000 newNodePtr headPtr

19 Inserting – Start of List 0x30a8 0x2008 0x position 0x2000 newNodePtr headPtr

20 Inserting – Start of List 0x30a8 0x2008 0x position 0x2000 newNodePtr headPtr

21 Inserting – Inside the List 0x position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr

22 Inserting – Inside the List 0x position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr

23 Inserting – Inside the List 0x position 0x2000 newNodePtr 0x3050 0x3080 prevPtr 0x3080 headPtr

24Delete 2310 If we delete the Node at position 0. headPtr 210

25 If we delete the Node at position 2. headPtr oldNodePtr

26 void deleteNode(List* listPtr, int position) { Node* oldNodePtr = NULL; Node* prevPtr = NULL; if (listPtr->count > 0 && position count) { if (position == 0) { oldNodePtr = listPtr->headPtr; listPtr->headPtr = oldNodePtr->nextPtr; } else { prevPtr = setPosition(listPtr, position - 1); oldNodePtr = prevPtr->nextPtr; prevPtr->nextPtr = oldNodePtr->nextPtr; } listPtr->count--; free(oldNodePtr); } else { fprintf(stderr, “List is empty or invalid position.\n”); exit(1); }

27 Deleting – 1 st Node 0 position 0x4000 oldNodePtr 0x30a80x20300x4000 headPtr

28 0 position 0x4000 oldNodePtr 0x30a80x20300x4000 Deleting – 1 st Node headPtr

29 0 position 0x4000 oldNodePtr 0x30a80x2030 Deleting – 1 st Node headPtr

30 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x30a80x20300x4000 0x2030 prevPtr headPtr

31 0x30a80x20300x4000 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x2030 prevPtr headPtr

32 0x30a80x4000 Deleting – Middle Node 1 position 0x2030 oldNodePtr 0x2030 prevPtr headPtr

33 Double Linked List Operations Go to a position in the list. Insert an item in a position in the list. Delete an item from a position in the list. Retrieve an item from a position. Replace an item at a position. in both directionsTraverse a list, in both directions.

34 Double Linked List currentPtr 01234

35 struct DoubleLinkNodeRec { float value; struct DoubleLinkNodeRec* nextPtr; struct DoubleLinkNodeRec* previousPtr; }; typedef struct DoubleLinkNodeRec Node; struct DoubleLinkListRec { int count; Node* currentPtr; int position; }; typedef struct DoubleLinkListRec DoubleLinkList;

36 Insert at end currentPtr newNodePtr 0x40000x30800x20300x2000 0x40000x3080 0x2030 NULL 0x2000 prevPtr 0x2030

37 Insert at end 0x40000x30800x20300x2000 0x40000x3080 0x2030 0x2000 NULL 0x2000 prevPtr 0x2030 currentPtr newNodePtr

38 Insert at end 0x40000x30800x20300x2000 0x40000x3080 0x2030 0x2000 NULL 0x2030 0x2000 prevPtr 0x2030 currentPtr newNodePtr

39 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 0x2000 prevPtr 0x3080 currentPtr newNodePtr

40 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL 2030 NULL 0x2000 prevPtr 0x3080 currentPtr newNodePtr

41 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x3080 0x2030 NULL x2000 prevPtr 0x3080 currentPtr newNodePtr

42 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x2000 0x30800x2030 NULL x2000 prevPtr 0x3080 currentPtr newNodePtr

43 Insert inside the list 0x40000x3080 0x2030 0x2000 0x4000 0x2000 0x30800x2000 NULL x2000 prevPtr 0x3080 currentPtr newNodePtr

44 Delete from end oldNodePtr 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x2030 currentPtr prevPtr 0x3080

45 Delete from end 0x40000x30800x2030 0x40000x3080 NULL 0x2030 currentPtr prevPtr 0x3080 oldNodePtr

46 Delete from end 0x40000x3080 0x4000 0x3080NULL 0x2030 currentPtr prevPtr 0x3080 oldNodePtr

47 Delete from inside list 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr

48 Delete from inside list 0x40000x30800x2030 0x40000x3080 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr

49 Delete from inside list 0x40000x30800x2030 0x4000 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr

50 Delete from inside list 0x40000x2030 0x4000 0x2030 NULL 0x3080 currentPtr prevPtr 0x4000 oldNodePtr

51Revision Linked List. Benefits of different implementations. Double Linked List. Revision: Reading Kruse Standish 2.4 – 2.5 Preparation Next lecture: Elementary Algorithms Read Chapter 6 in Kruse et al.