Lists.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Lists: An internal look
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
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
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© 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.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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.
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.
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 Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
CSE 1342 Programming Concepts
Lecture 6 of Computer Science II
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Linked List :: Basic Concepts
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Data structures and algorithms
Review Deleting an Element from a Linked List Deletion involves:
12 C Data Structures.
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
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.
Linked Lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Introduction to Linked Lists
Queues.
Lists.
Circular Buffers, Linked Lists
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked List Intro CSCE 121 J. Michael Moore.
Lists, Strings & Multi-dimensional arrays
Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
Review & Lab assignments
Data Structures and Algorithms
Problem Understanding
Linked List.
Linked List and Selection Sort
Chapter 17: Linked Lists.
Data Structures & Algorithms
General List.
Linked Lists.
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Linked List Intro CSCE 121.
Linked lists.
Linked Lists.
BY PROF. IRSHAD AHMAD LONE.
Module 13 Dynamic Memory.
Problem Understanding
Presentation transcript:

Lists

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

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

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 data next anchor

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

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; };

Inserting at position 0 Node * anchor ; anchor=NULL; // no list exists yet // allocate space for a node & store item in it Node * aNode = malloc (sizeof(Node)); aNode -> next = anchor; // new node -> old head of list anchor = aNode; //anchor -> new head of list aNode anchor

Inserting at the end Node * last ; // allocate space for a node & store item in it Node * aNode = malloc (sizeof(Node)); aNode -> next = anchor; // new node -> old head of list last=aNode; // point to new last element anchor aNode last

Inserting between any 2 nodes Locate position for insertion (after “last”) Node * aNode=malloc(sizeof(aNode)); last->next = aNode; // point to new node aNode->next" =tail; aNode tail anchor last

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)

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 first temp not freed

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

Two-way lists Insert & Delete functions more complex 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 addedNode temp first

Two-way lists-2 addedNode 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) addedNode P 1 4 2 first 3 C N