List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3



Similar documents
DATA STRUCTURE - STACK

Linked Lists Linked Lists, Queues, and Stacks

DATA STRUCTURE - QUEUE

St S a t ck a ck nd Qu Q eue 1

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Stacks. Linear data structures

Chapter 3: Restricted Structures Page 1

Common Data Structures

CmpSci 187: Programming with Data Structures Spring 2015

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Abstract Data Types. Chapter 2

Data Structures and Algorithms C++ Implementation

Data Structures and Algorithms Stacks and Queues

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Basic Data Structures and Algorithms

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

STACKS,QUEUES, AND LINKED LISTS

Data Structures Using C++ 2E. Chapter 5 Linked Lists

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

CS 2412 Data Structures. Chapter 2 Stacks and recursion

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

DATA STRUCTURES USING C

Module 2 Stacks and Queues: Abstract Data Types

Analysis of a Search Algorithm

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

7.1 Our Current Model

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

10CS35: Data Structures Using C

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structures and Data Manipulation

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Stack Allocation. Run-Time Data Structures. Static Structures

Programming with Data Structures

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Application of Stacks: Postfix Expressions Calculator (cont d.)

Universidad Carlos III de Madrid

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Data Structures Using C++

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

C++FA 5.1 PRACTICE MID-TERM EXAM

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

ADTs,, Arrays, Linked Lists

Lecture 11 Array of Linked Lists

D06 PROGRAMMING with JAVA

CS 2412 Data Structures. Chapter 3 Queues

CSE 211: Data Structures Lecture Notes VII

Stacks. Data Structures and Data Types. Collections

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Cpt S 223. School of EECS, WSU

Lecture Notes on Stacks & Queues

Algorithms and Data Structures

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Big O and Limits Abstract Data Types Data Structure Grand Tour.

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

STACK Data Structure:

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

Data Structures. Level 6 C Module Descriptor

Sequential Data Structures

Lecture 12 Doubly Linked Lists (with Recursion)

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

1 Abstract Data Types Information Hiding

Data Structures and Algorithms

PES Institute of Technology-BSC QUESTION BANK

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Linked List as an ADT (cont d.)

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Data Structure with C

14 Stacks, Queues, And Linked Lists

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Linked List Problems

COMPUTER SCIENCE. Paper 1 (THEORY)

Introduction to Data Structures and Algorithms

Introduction to Data Structures

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Transcription:

List, Stack and Queue Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Outline Structure Linked List Overview Implementation Stack Overview Implementation Queue Overview Implementation

Structure A collection of values (members) Like a class in java or c++, but without methods and access controls (private, protected, public.) struct time int hh; int mm; int ss; ; struct time t1; t1.hh = 20; t1.mm=12; t1.ss=30;

Structure We can also use pointer to structure struct time int hh; int mm; int ss; ; struct time* t1; (*t1).hh=20; Pointer to structure is very common. The above is equivalent to: struct time* t1; t1->hh = 20; // same as (*t1).hh=20;

Some words about typedef Allow us to define alias for a data type: typedef int My_integer_type; My_interger_type x =3; Tpedef can be used for structures: typedef struct time int hh; int mm; int ss; Time_type; Time_type t1; t1.hh = 12;

Dynamic Memory Allocations We can allocate memory at run time using malloc malloc can be used to allocate a piece of memory of the specified size, and returns a pointer to it. Example: Time_type *t1; t1 = (Time_type*)malloc(sizeof(Time_type)); Allocate enough memory for storing a Time_type variable (which is a structure). Return a pointer to it. Cast it to a pointer to Time_type, and assign it to t1.

Dynamic Memory Allocations Use free to de-allocate the memory when it is no longer needed. This is important because there is no garbage collection in C. So you will run out of memory if you keep allocating without deallocating. ( Memory Leak ) Time_type *t1; t1 = (Time_type*)malloc(sizeof(Time_type)); t1->hh = 12; free(t1); //de-allocate when we no longer need it.

Linked List Overview A list of structures (nodes) Each structure contains Element (to store data) Pointer to next structure Insert() Delete() Print()

Linked List Implementation struct node_s int data; struct node_s *next; ; typedef struct node_s node; //To create a node variable node anode; //static, allocate in compile time or node *anode = (node*)malloc(sizeof(node)); //dynamic allocation

Linked List Implementation Link two nodes together node a,b; a.next = &b; b.next = NULL; //use pointer node* a, *b; a = (node*)malloc(sizeof(node)); b = (node*)malloc(sizeof(node)); b->next = NULL; a->next = b;

Linked List Implementation Insert a node to a list node a,b,c; c.next = NULL; //originally, only a and c a.next = &c; //insert b between a and c b.next = &c; a.next = &b;

Linked List Implementation Delete a node from a list node a,b,c; c.next = NULL; //orginial a.next = &b; b.next = &c; //remove b from the list a.next = &c;

Linked List Implementation

Stack books on your table?

Stack Overview Last In First Out (LIFO) Push() Pop() Top() is_empty()

Stack Implementation Can be implemented by linked list or array Create an empty stack node *create_stack() node* emptystack; emptystack = (node*)malloc(sizeof(node)); emptystack->next = NULL; return emptystack;

Stack Implementation Push an entry into the stack void Push(int inputdata, node *stack) node* newnode = (node*)malloc(sizeof(node)); newnode->data = inputdata; newnode->next = stack->next; //should be first stack->next = newnode; //how about change the above //2 lines?

Stack Implementation Pop an entry from the stack int Pop(node* stack) int temp; node* tobepopped; if(stack->next!=null) temp = stack->next->data; tobepopped = stack->next; stack->next = stack->next->next; free(tobepopped); return temp; else return 0; //error code, you can define according to the demand

Stack Implementation Return the top element in the stack int top(node* stack) if(stack->next!=null) return stack->next->data; else return 0; Determine if the stack is empty int is_empty(node *stack) return (stack->next==null);

If you are implementing a large project, usually we can separate the declaration and implementation to.h and.c files.

Stack Implementation #include <stdio.h> #include stack.h int main() node* mystack = create_stack(); Push(1,mystack); Push(2,mystack); Push(3,mystack); while(!is_empty(mystack)) printf( %d\n,pop(mystack)); return 0;

Stack Implementation using Array Implement a stack using array typedef struct int *data; //data is an array of integer int top; //position of top element int size; //maximum number of data in the stack Stack;

Stack Implementation using Array createstack, makeempty //return 1 for success, 0 for fail int createstack(stack* astack,int size) astack->data = (int*)malloc(sizeof(int)*size); if(astack->data==null) //malloc failed return 0; astack->size = size; astack->top = -1; return 1; void makeempty(stack *astack) astack->top = -1;

Stack Implementation using Array isempty, isfull int isempty(stack* astack) if(astack->top<0) return 1; else return 0; int isfull(stack* astack) if(astack->top >= astack->size-1) //we put the 1 st element at the 0 th position return 1; else return 0;

int top(stack* astack) if(!isempty()) return astack->data[astack->top]; else return 0; //mean error code int pop(stack* astack) if(!isempty()) int adata = top(astack); astack->top--; return adata; else return 0; //return 1 if we can successfully push //element, return 0 if we fail int push(stack* astack, int adata) if(!isfull()) astack->top++; astack->data[astack->top] = adata; return 1; else return 0;

Queue ticket office of the Ocean park?

Queue Overview First In First Out (FIFO) Enqueue Dequeue

Queue Implementation A queue may be implemented using linked-list or array Implement a queue using array

Queue Implementation Implementing a queue using circular array typedef struct int* data; //data is an array of int int head; //current head int tail; //current tail int num; //number of elements in queue int size; //size of queue Queue;

Queue Implementation createqueue //return 1 for success, 0 for fail int createqueue(queue* aqueue, int size) aqueue->data = (int*)malloc(sizeof(int)*size); if(aqueue->data==null) return 0; aqueue->head=0; aqueue->tail=-1; aqueue->num=0; aqueue->size=size; return 1;

Queue Implementation enqueue //return 1 is successfully enqueue, return 0 if the queue is full int enqueue(queue *aqueue,int adata) if(aqueue->num<aqueue->size) aqueue->tail = (aqueue->tail+1)%aqueue->size; //mode operation aqueue->data[queue->tail]=adata; aqueue->num++; return 1; else return 0;

Queue Implementation dequeue //return the data if successfully dequeue, return 0 if fail int dequeue(queue* aqueue) if(aqueue->num>0) int adata = aqueue->data[aqueue->head]; aqueue->head = (aqueue->head+1)%aqueue->size; //mode operation aqueue->num--; return adata; else return 0;

Queue Implementation isempty, isfull int isempty(queue *aqueue) return (aqueue->num==0); int isfull(queue *aqueue) return (aqueue->num==aqueue->size);

Queue Implementation front, makeempty //see the front element, similar to dequeue but do not remove the data int front(queue* aqueue) return aqueue->data[aqueue->head]; void makeempty(queue* aqueue) aqueue -> head = 0; aqueue -> tail = -1; aqueue -> num = 0;