Linked Lists.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

EENG212 Algorithms and Data Structures
DATA STRUCTURES USING C++ Chapter 5
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Ceng-112 Data Structures I Chapter 5 Queues.
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.
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)
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.
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
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 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.
CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists.
Introduction to Data Structures Systems Programming.
Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.
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.
Introduction to Data Structures Systems Programming Concepts.
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 © 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.
Programming Practice 3 - Dynamic Data Structure
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.
1 Objectives of these slides: to describe linked lists in C 6 – Lists.
Algorithms and Data Structures
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CPT: Lists/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Programming Circular Linked List.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Elementary Data Structures
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.
Introduction to Data Structures
Chapter 4 Linked Lists
Algorithms and Data Structures
Lectures on data structures and C 3ed CS(CSI33) By, CHANDRASHEKAR A.M.
Linked Lists Chapter 4.
Linked Lists Chris Wright Winter 2006.
Summary on linked lists
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Linked List Insert as a first node Insert as a last node
Review & Lab assignments
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Linked Lists.
Linked List Mechanics Slides Table of Contents Linear Lists
General List.
Andy Wang Object Oriented Programming in C++ COP 3330
Presentation transcript:

Linked Lists

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

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.

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

Linked List Head 1 2 3

#ifndef LINKEDLISTH #define LINKEDLISTH #include <stdbool.h> #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

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

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

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

Set Position Head 1 2 2 position

Set Position Head 0x4000 0x2030 0x30a8 2 position i 0x4000 nodePtr

Set Position Head 0x4000 0x2030 0x30a8 2 position 1 i 0x2030 nodePtr

Set Position Head 0x4000 0x2030 0x30a8 2 position 2 i 0x30a8 nodePtr

Insert Head 1 2 If we insert it at position 0. 1 2 Head

Insert Head 1 2 If we insert it at position 0. 2 1 3 Head

Instead, suppose we insert it at position 1. Head 1 2

Instead, suppose we insert it at position 1. Head 3 2 1

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

Inserting – New List Head NULL 0x30a8 position 0x30a8 newNodePtr

Inserting – New List Head 0x30a8 position 0x30a8 newNodePtr

Inserting – Start of List Head Head 0x30a8 0x2008 0x2000 newNodePtr 0x2000 position

Inserting – Start of List Head Head 0x30a8 0x2008 0x2000 newNodePtr 0x2000 position

Inserting – Start of List Head Head 0x30a8 0x2008 0x2000 newNodePtr 0x2000 position

Inserting – Inside the List Head 0x3080 0x3050 nodePtr 0x3080 0x2000 newNodePtr 0x2000 1 position

Inserting – Inside the List Head 0x3080 0x3050 nodePtr 0x3080 0x2000 newNodePtr 0x2000 1 position

Inserting – Inside the List Head 0x3080 0x3050 nodePtr 0x3080 0x2000 newNodePtr 0x2000 1 position

Delete Head 1 2 3 If we delete the Node at position 0. 2 1 Head

If we delete the Node at position 2. Head 1 2 3

If we delete the Node at position 2. Head 2 1 2

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

Deleting – 1st Node Head 0x4000 0x2030 0x30a8 position 0x4000 nodePtr

Deleting – 1st Node 0x4000 0x2030 0x30a8 position Head 0x4000 nodePtr

Deleting – 1st Node 0x2030 0x30a8 position Head 0x4000 nodePtr

Deleting – Middle Node Head 0x4000 0x2030 0x30a8 1 position 0x2030 nodePtr 0x2030 OldNodePtr

Deleting – Middle Node Head 0x4000 0x2030 0x30a8 1 position 0x2030 nodePtr 0x2030 OldNodePtr

Deleting – Middle Node Head 0x4000 0x30a8 1 position 0x2030 nodePtr OldNodePtr

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. Traverse a list, in both directions.

Double Linked List 1 2 3 4 Current

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;

Insert at end currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 0x2000 NULL NULL NULL 0x4000 0x3080 NULL 0x2030 currentPtr prevPtr newPtr 0x2000

Insert at end currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 0x2000 NULL NULL 0x4000 0x3080 NULL 0x2030 currentPtr prevPtr newPtr 0x2000

Insert at end currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 0x2000 NULL NULL 0x4000 0x3080 0x2030 0x2030 currentPtr prevPtr newPtr 0x2000

Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 NULL NULL 0x4000 0x3080 0x2000 0x3080 currentPtr prevPtr 0x2000 NULL newPtr NULL

Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 NULL NULL 0x4000 0x3080 0x2000 0x3080 currentPtr prevPtr 0x2000 2030 newPtr NULL

Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 NULL NULL 0x4000 0x3080 0x2000 0x3080 currentPtr prevPtr 0x2000 2030 newPtr 3080

Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 NULL NULL 0x4000 0x2000 0x2000 0x3080 currentPtr prevPtr 0x2000 2030 newPtr 3080

Insert inside the list currentPtr prevPtr newPtr 0x4000 0x3080 0x2030 NULL NULL 0x4000 0x2000 0x2000 0x3080 currentPtr prevPtr 0x2000 2030 newPtr 3080

Delete from end currentPtr oldPtr prevPtr 0x4000 0x3080 0x2030 0x3080 NULL NULL 0x4000 0x3080 oldPtr 0x2030 currentPtr 0x3080 prevPtr

Delete from end currentPtr oldPtr prevPtr 0x4000 0x3080 0x2030 0x3080 NULL NULL NULL 0x4000 0x3080 oldPtr 0x2030 currentPtr 0x3080 prevPtr

Delete from end currentPtr oldPtr prevPtr 0x4000 0x3080 0x3080 NULL

Delete from inside list 0x4000 0x3080 0x2030 0x3080 0x2030 NULL NULL 0x4000 0x3080 oldPtr 0x3080 currentPtr 0x4000 prevPtr

Delete from inside list 0x4000 0x3080 0x2030 0x2030 0x2030 NULL NULL 0x4000 0x3080 oldPtr 0x3080 currentPtr 0x4000 prevPtr

Delete from inside list 0x4000 0x3080 0x2030 0x2030 0x2030 NULL NULL 0x4000 0x4000 oldPtr 0x3080 currentPtr 0x4000 prevPtr

Delete from inside list 0x4000 0x2030 0x2030 NULL NULL 0x4000 oldPtr 0x3080 currentPtr Head 0x4000 prevPtr

Elementary Algorithms Next Elementary Algorithms