Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
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.
CSE Lecture 12 – Linked Lists …
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A4 – Basic Data Structures – Continued (Queues)
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Stacks.
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 A10 – Elementary Algorithms (Revision)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Lists CSE1303 Part A Data Structures and Algorithms.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Object Oriented Data Structures
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Introduction to Data Structures Systems Programming Concepts.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Data Structure & Algorithm
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Data Structures & Algorithms
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Click to edit Master text styles Stacks Data Structure.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Queues Manolis Koubarakis Data Structures and Programming Techniques 1.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
CSE 1342 Programming Concepts
CS505 Data Structures and Algorithms
Stacks and Queues.
Basic Data Structures – Continued (Queues)
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Presentation transcript:

Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)

2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out (FIFO) initialize, append, serve, status List

3 Overview Programming Styles. Abstract Data Types (ADT). Lists. Operations. Implementations of Lists.

4 Programming Styles Procedural Programming Decide which procedures you want: use the best algorithms you can find. Modular Programming Decide which modules you want: partition the program so that data is hidden in modules. Data Abstraction Decide which types you want: provide a full set of operations for each type. Object-Oriented Programming Decide which classes you want: provide a full set of operations for each class; make commonality explicit by using inheritance.

5 Abstract Data Type (ADT) A Data Structure together with operations defined on it. Useful to consider what operations are required before starting implementation. Led to the development of object oriented programming.

6 A Stack ADT Initialize the stack. Determine whether the stack is empty. Determine whether the stack is full. Push an item onto the top of the stack. Pop an item off the top of the stack. A sequence of elements together with these operations:

7 A Queue ADT Initialize the queue. Determine whether the queue is empty. Determine whether the queue is full. Find the size of the queue. Append an item to the rear of the queue. Serve an item at the front of the queue. A sequence of elements together with these operations:

8 A List ADT Initialize the list. Determine whether the list is empty. Determine whether the list is full. Find the size of the list. Insert an item anywhere in the list. Delete an item anywhere in a list. Go to a particular position in a list. A sequence of elements together with these operations:

9 Comparison Stacks –Insert at the top of the stack (push) –Delete at the top of the stack (pop) Queues –Insert at the rear of the queue (append) –Delete at the front of the queue (serve) Lists –Insert at any position in the list. –Delete at any position in the list.

10 A Rational Number ADT Initialize the rational number. Get the numerator. Get the denominator. Simplify a rational number. Add two rational numbers. Determine whether two rational numbers are equal. etc. Two integers together with these operations:

11 A String ADT Initialize the string. Copy a string. Read in a line of input. Concatenate two strings. Compare two strings. Find a length of a string. etc. A array of characters together with these operations:

12 Lists SEALFOXDEERAPEEMU Kept in alphabetical or numerical order No spaces between elements Simple lists are implemented in arrays

13 List Operations Initialize the list. Determine whether the list is empty. Determine whether the list is full. Find the size of the list. Insert an item anywhere in the list. Delete an item anywhere in a list. Go to a particular position in a list.

14 Simple List Implementation Insertion APEDEERFOXSEAL APEDEERFOXSEAL EMU Insert EMU

APEDEERFOXSEAL Simple List Implementation Deletion EMU Delete FOX APEDEERSEALEMU

16 #ifndef LISTH #define LISTH #include #define MAXLIST 10 struct ListRec { char list[MAXLIST]; int count; }; typedef struct ListRec List; void Initialise(List *lptr); void Insert(List *lptr, char item); void Delete(List *lptr, int pos); bool IsFull(List *lptr); bool IsEmtpy(List *lptr); int Find(List *lptr, char item); /* and any more functions you can think of) */ #endif

17 #include #include "list.h" void Initialise(List *lptr) { lptr->count = 0; } bool isFull(List * lptr) { if(lptr->count == MAXLIST) return true; else return false; } bool isEmpty(List *lptr) { if(lptr->count == 0) return true; else return false; }

18 List Insert -If the list isn't full: -step to position to insert item -move elements after this position along one position starting from the end of the list, working back to the position. -copy item into the position -increase count

19 void Insert(List *lptr, char item){ int pos, current; if(IsFull(lptr)) { fprintf(stderr, "List is full."); exit(1); } for(pos = 0; pos count; pos++) { if(lptr->list[pos] > item) break; } current = lptr->count - 1; while (current >= pos) { /* Copy the element into the next position along */ lptr->list[current+1] = lptr->list[current]; current--; } lptr->list[pos] = item; lptr->count++; }

20 Linked List Simple Implementation DEERFOXAPESEAL start Each element contains a ‘ link ’ to the next element’s position Kept in alphabetical or numerical order No spaces between elements

21 Linked List Implementation: Using Array Index DEERFOXAPESEAL data marks last item start link to next item 21

22 Linked List Implementation: Using Array Index DEERFOXAPESEAL start insert: EMU EMU 11

23 Linked List Implementation: Using Array Index DEERFOXAPESEAL start insert: EMU EMU 1

24 Linked List Implementation: Using Pointers DEERFOXAPESEAL 0x20000x20080x20100x20180x20200x2018 0x20200x20180x2000NULL start EMU 0x2008 special pointer constant

25 #ifndef LLISTH #define LLISTH #include #define MAXLIST 10 typedef struct EntryRec { char ch; int next; } Entry; typedef struct LinkedListRec { Entry list[MAXLIST]; int count; int start; } LList; void Initialise(LList *lptr); void Insert(LList *lptr, char item); void Delete(LList *lptr, int pos); bool IsFull(LList *lptr); bool IsEmtpy(LList *lptr); int Find(LList *lptr, char item); /* and any more functions you can think of) */ #endif

26 #include #include "llist.h" void Initialise(LList *lptr) { lptr->count = 0; lptr->start = -1; } bool isFull(LList *lptr) { if(lptr->count == MAXLIST) return true; else return false; } bool isEmpty(LList *lptr) { if(lptr->count == 0) return true; else return false; }

27 Linked List Insert -If the list isn't full: -insert item at next free position. -step through list to find the element that comes before item and the one after. -change the new item's next value to be the position of the next element. -if the new item is not the first element -change the previous element's next value to be the position of the new item. -if the new item is the first element, change the start of the list to equal the new item's position. -increase count.

28 Linked List Implementations -There are many different types of implementations for linked lists in arrays. -Free positions always at the end (count), when deleting need to shuffle items to close the gap -Using –2 to indicate a 'free' position – still slow to find a free spot when inserting -Using a second linked list – a 'free list' inside the same array – requires a second 'start' indicator. This is the most common implementation.

29 Revision List –initialize, insert, delete, position, status –implementations Difference between Stacks, Queues, and Lists. ADT. Revision: Reading Kruse 4.5, 4.6, 4.8 Deitel & Deitel (2e) 12.1, 12.2, 12.4 Standish 8.1 – 8.4 Langsam 4.2 – 4.5 King 17.5 Preparation Next lecture: Dynamic Memory Read 4.5 in Kruse et al. Read 12.3 in Deitel and Deitel.