CS 261 - Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
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.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS 261- Winter 2009 Dynamic Array Queue and Deque.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS Fall 2009 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
Stacks, Queues, and Deques
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. 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.
Stacks, Queues, and Deques
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
CS 261 – Fall 2009 Binary Search Trees Again, but in detail.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
“Planning is bringing the future into the present so that you can do something about it now.” – Alan Lakein Thought for the Day.
Today’s Agenda  Linked Lists  Double ended Linked Lists  Doubly Linked Lists CS2336: Computer Science II.
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.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
CS 261 Fall 2009 Dynamic Array Introduction (aka Vector, ArrayList)
Linked List Implementation of the Deque
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
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.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
CS261 Data Structures Linked Lists - Introduction.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Linked List Introduction
Chapter 12 – Data Structures
Lectures linked lists Chapter 6 of textbook
Data Structures and Algorithms
Hash Tables Part II: Using Buckets
Abstract Data Types (ADTs)
Stacks, Queues, and Deques
Linked Lists: Implementation of Queue & Deque
Stacks, Queues, and Deques
Data Structures and Algorithms
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Dynamic Array: Implementation of Queue & Deque
Stacks, Queues, and Deques
Chapter 9 Linked Lists.
Abstract Data Types (ADTs)
Presentation transcript:

CS Fall 2009 Linked List Introduction

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 routine sets to zero */ }; struct link * _newLink (EleType v, link * n) { /* used internally */ struct link * newLink = (struct link *) malloc(sizeof(struct link)); assert(newLink != 0); newLink->next = n; newLink->value = v; return newLink; } void listStackPush (struct listStack *stk, EleType val) { stk->firstLink = _newLink(val, stk->firstLink); }

Other Operations How do you return the top element in stack? How do you remove the top element? How do you tell if the stack is empty? You will do all of these in the worksheet

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

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

Elements are Added to end void listQueueAddLast (struct listQueue *lst, EleType val) { struct link * lnk = _newLink(val, 0); if (lst->lastLink != 0) lst->lastLink->next = lnk; lst->lastLink = lnk; if (firstLink == 0) lst->firstLink = lnk; }

Empty test - just see if link is null Int listQueueIsEmpty (struct listQueue *lst) { return lst->firstLink == 0; }

Elements Removed from Front void listQueueRemoveFirst (struct listQueue *lst) { struct link * lnk = lst->firstLink; assert (! listQueueIsEmpty(lst)); lst->firstLink = lnk->next; free(lnk); if (lst->firstLink == 0) /* ie, removing last*/ lst->lastLink = 0; }

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 = null; struct link * current = lst->firstLink; for ( ; current != null; current = current->link) { if (EQ(current->value, testValue)) { // do what needs to be done return; } prevLink = current; }

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