Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
CSEB324 Data Structures & Algorithms
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS Data Structures Chapter 4 Lists.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
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 &
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
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.
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.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
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. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
LINKED LISTS.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Chapter 12 – Data Structures
Linked List :: Basic Concepts
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
12 C Data Structures.
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Linked lists.
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.
Linked List Sudeshna Sarkar.
Queues.
Lists.
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Review & Lab assignments
Data Structures and Algorithms
Problem Understanding
Linked List.
Linked Lists.
Linked lists.
BY PROF. IRSHAD AHMAD LONE.
Presentation transcript:

Lists 1

Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add an item at any position in the list Delete: Remove an item from the list at any position in the list Traverse: access and process elements in order of occurrence 2

Possible Implementations Array –capacity determined at compile-time –capacity determined at run-time –good choice if capacity known before list is constructed insertions/deletions mainly at the end Linked List (dynamic structure) –capacity grows and shrinks as size changes –insertions/deletions do not require shifting –access to an item by index is not efficient 3

Structural Concept Each element is a node Space is dynamically allocated (and returned) one node at a time Items are not contiguous in memory

linked list (dynamic) node contains >=1 data item and pointer to next node The last node's "next" pointer is "empty" A separate pointer accesses first node first data next

Accessing nodes no direct access to individual nodes nodes only accessed via pointers access types –a list is a sequential access data structure Because you cannot get directly to an item –an array is a direct access data structure Because a subscript gets you directly to an item

Implementation typedef Complx QueueElement; struct QueueNode {QueueNode * next; QueueElement XX; }; void enqueue (QueueNode *, Complx); QueueNode * myFront; // Declare anchor myFront= (QueueNode *) malloc (sizeof(QueueNode)); // above stmt creates list node 0, sets myFront Complx X; enqueue (myFront, X); // put X in the list 7

Efficiency How do you insert? How do you extract? What about access in the "middle"? Are some ways easier? struct Node { int X; Node * next; }; 8

Inserting at position 0 // allocate space for a node & store item in it Node * aNode = new Node (item); aNode -> next = first; // new node -> old head of list first = aNode; //anchor -> new head of list first addedNode

Inserting between the ends Locate position for insertion Save "next" Create new item Change next -> new item Store saved "next" in new item's "next" 10 first addedNode temp

Removing a node Must be careful Save the ptr to the node BEFORE the node to be removed. May have to "peek" at next item to decide if you’re there yet Save the "next" ptr of the node to remove Put the saved "next" pointer into the "next" of the previous node Free old node (malloc/free or new/delete) 11

DANGER!!! When removing a node –must save a ptr to it if re-use is possible –must delete everything the node points to free won't "chase down" additional storage 12 first temp not freed

Traversal curr_ptr = first; while (curr_ptr != NULL) {compare data for correct node curr_ptr=curr_ptr -> next; //advance } 13 first ptr to "current" node

Two-way lists struct Node { int X; Node * next; // points forward in list Node * prev; // points backward in list }; Insert & Delete functions more complex Must have (or get) pointers to both sides 14 first addedNode temp

Two-way lists-2 C=// the current node (maybe found with a search) N=C->next; // save the tail_pointer P=(Node*) malloc (Node); // get new node P->next=N; // new node points to old tail of list (1) P->prev=C; // cutoff point points to new node (2) C->next=P; // old head points to new node (3) N->prev=P; // old tail points to new node (4) 15 first addedNode C N P