Linked List Introduction

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
CS 261- Winter 2009 Dynamic Array Queue and Deque.
CS Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
CS261 – Data Structures Iterator ADT Dynamic Array and Linked List.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Linked Structures, LinkedStack
CS 261 Fall 2009 Dynamic Array Introduction (aka Vector, ArrayList)
Linked List Implementation of the Deque
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CS Fall 2009 Skip Lists. Skip Lists Advantages Ordinary linked lists and arrays have fast (O(1)) addition, but slow search Sorted Arrays have fast.
CS261 Data Structures Linked Lists - Introduction.
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Elementary Data Structures
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Chapter 12 – Data Structures
Week 4 - Friday CS221.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
12 C Data Structures.
Storage Strategies: Dynamic Linking
UNIT-3 LINKED LIST.
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Stacks and Queues.
Queues Queues 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.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Abstract Data Types (ADTs)
Lists.
Linked Lists.
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Linked Lists: Implementation of Queue & Deque
Stacks, Queues, and Deques
Lesson Objectives Aims
Programming Linked Lists.
Data Structures and Algorithms
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
CS210- Lecture 6 Jun 13, 2005 Announcements
Dynamic Array: Implementation of Queue & Deque
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Dynamic Array: Implementation of Stack
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Linked List Introduction CS 261 - Winter 2011 Linked List Introduction

Problem with Array and Dynamic Array The problem with an array is that elements are kept in a single large block of memory Can often use more memory than necessary if collection grows and shrinks repeatedly Linked list is a good alternative, as the memory use is always proportional to the number of elements in the collection

Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated and released as necessary. Each link points to next link in sequence, sometimes to previous link. Lots of variations on a simple idea

A typical Link Structure struct link { EleType value; struct link * next; };

Some variations in Linked lists List have header (special value to point to start) Use null as terminator, or special value for end Use single or double links? Pointer to first element, or pointer to first and last

Simplest Example, List Stack List stack is the simplest data structure that can be implemented using linked list idea Keep pointer to first element (null if empty) Elements are added or removed from front can only access first element

Picture of list stack

Code for list stack struct ListStack { struct link * firstLink; /* initialize sets to zero */ }; void listStackPush (struct listStack *stk, double val) { stk->firstLink = _newLink(val, stk->firstLink); }

Internal utility routine struct link * _newLink (EleType v, struct link * n) { struct link * lnk = (struct link *) malloc(sizeof(struct link)); assert (lnk != 0); lnk->value = v; lnk->next = n; return lnk; }

Think about how to write Top, push, isEmpty What are the steps involved in returning top of stack? Removing top of stack? Returning true (1) if stack is empty, and false (0) if it is not empty

How fast is List Stack? Compare to dyArrayStack push - list O(1) always, dyArray(1) expected pop - list O(1) always, dyArray same top - list O(1) always, dyArray same In practice dyArray is slightly faster in real timinings.

But what about queues? An array queue is hard to do, because you can't add to the beginning without sliding things up. With lists it is easy. Just keep a pointer to both the front AND the back. Elements added to the back, removed from front

Picture of list queue

Why add to back, remove fron front? Think about the issues Why not other way around? Container where you can add to back or front, but remove from front only, is sometimes called a Scroll.

Sentinels get rid of special cases Notice in discussing pop we needed to treat removing last element as special case. Can avoid this by using a Sentinel A sentinel is just a link without a value, never removed, sits at front or back of the collection. Don’t need to check for removing last val

Picture of Sentinel Front ptr Back ptr Sentinel 3 9 7 4

Class Structure for List Queue struct listQueue { struct link * firstLink; struct link * lastLink; }; void listQueueAddBack (struct listQueue *lst, EleType val);

Init makes the sentinel void ListQueueInit (struct listQueue *q) { struct slink *lnk = (struct slink *) malloc(sizeof(struct slink)); assert(lnk != 0); /* lnk is the sentinel */ lnk->next = 0; q->firstLink = q->lastLink = lnk; }

Elements are Added to end void listQueueAddLast (struct listQueue *q, EleType val) { q->lastLink->next = _newLink(val, 0); q->lastLink = q->lastLink->next; } Are there any special cases?

Return the front element How do you return the front element? Are there any special cases? How do you remove the front element? Again, are there special cases? How do you tell if the list is empty?

What about a deque? What if we want to add and remove elements from both front and back? Need to use links going both forward and backwards Makes adding a new link harder, as must maintain both forward and backward links. Will do that in tomarrows lesson

What about a Bag? Contains is easy - just a loop Add is easy - can either add to front or to back Remove is the tricky one. How to patch up links after removing an element. Two solutions, double links or previous pointers (will do double links tomarrow)

Previous Pointers As you loop over the links looking for value to remove, keep pointer to previous element Struct link prevLink = q->firstLink; Struct link current; for (current = prev->next ; current != 0; current = current->link) { if (EQ(current->value, testValue)) { // do what needs to be done return; } prevLink = current;

When you find it When you find the element to be deleted, what does prev point to? What if the element to be deleted is at the front of the list? Does this matter?

Your turn See if you can do stack (should be trivial) See if you can do queue (only slightly harder) See if you can do bag (requires a little more thought)