Computer Science 2 Outline/Learning Objectives for the day:

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linear Lists – Linked List Representation
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks, Queues & Deques CSC212.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
CSC 202 Analysis and Design of Algorithms Lecture 06: CSC 202 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List, Stack and.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Information and Computer Sciences University of Hawaii, Manoa
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Stack Any Other Data Structure Array Linked List
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
CS342 Data Structures End-of-semester Review S2002.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Linear Data Structures
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 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.
Stacks Chapter 3 Objectives Upon completion you will be able to
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
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.
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.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Data Structures Using C++ 2E
Introduction Of Stack.
MEMORY REPRESENTATION OF STACKS
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Objectives In this lesson, you will learn to: Define stacks
STACKS.
Algorithms and Data Structures
Recursion.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Computer Science Dynamics.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Programming Linked Lists.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Computer Science Dynamics.
Stacks and Queues 1.
Computer Science Sorting.
Computer Science 2: Trees
Stacks Data structure Elements added, removed from one end only
Deleting from a binary tree
CSC 143 Java Applications of Trees.
Computer Science 2 More Trees.
Deleting from a binary tree
More Trees 5/9/2-017 Be able to dry run a program that uses trees
Computer Science 2 Queue.
Time to finish first stack program
Computer Science 2 Queue Day 2.
Computer Science
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

Computer Science 2 Outline/Learning Objectives for the day: Review Pre-Post and Infix notation by completing some conversions. Review dynamics and functions by completing a Dry Run. Be able to develop an algorithm and code to make a linked list, that is able to put information in order as it is being entered.

Warm up Evaluate the following (Assume single digit numbers) 1) 5 4 + 8 – 3 2 * + 2) 6 8 5 2 9 + + - * 9 2 – 6 * 8 – 5 3 + + Translate the following into postfix and prefix notation 1) ( 5 – x ) + 3(4 + 6y) 2) (2a – 4)/(2b)

program DryRunFunOne; type ptrtype=^rectype; rectype=record num:integer; next:ptrtype; end; var top, temp:ptrtype; procedure one(var top:ptrtype); temp:ptrtype; count:integer; begin for count:=-1 to 2 do new(temp); temp^.num:= count*2; temp^.next:=top; top:=temp; function two(top:ptrtype):integer; begin if top <> nil then writeln(top^.num); two:=top^.num + two(top^.next); end else two:=2; end; Begin top:=nil; one(top); writeln(two(top)); readln; end.

Warm-up Complete the following procedure that should add val to a stack pointed to by top. Type pointerType = record data:real; next:pointerType; End; Procedure add2front(val:real;var top:pointerType);

Ordered Insertion into Linked Lists For example, consider a procedure insert_in_order(val,top) that will insert a new value val into linked list top so that sorted order is maintained. val 9 top 12 nil 7 3 11

insert_in_order(val,top) Algorithm Step 1. Create pointer p, moving p to point to the node after which val should be added. val 9 top 12 nil 7 3 11 p

insert_in_order(val,top) Algorithm Step 2. Create the new node we want, with the proper data and next values, i.e. New(temp); temp^.data := val; temp^.next := p^.next; val 9 top 12 nil 7 p 3 11 temp 9

insert_in_order(val,top) Algorithm Step 3. Splice our new node in after the node p points to, i.e. P^.next = Temp; val 9 top 12 nil 7 p 3 11 T 9

insert_in_order(val,top) Algorithm And we’re done! top 12 nil 7 3 11 9

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p:=top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; val 9 top 12 nil 7 3 11 p

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; val 9 top 12 nil 7 3 11 p

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val)do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; val 9 top 12 nil 7 3 11 p

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; val 9 top 12 nil 7 3 11 p

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; val 9 top 12 nil New node added to front of sublist whose head is p->next 7 3 11 p

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; val 9 top 12 nil New node added to front of sublist whose head is p->next 7 3 11 p 9

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; What if value to be added is less than value of first node in list? val 2 top 12 nil 7 3 11

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val)do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; What if value to be added is less than value of first node in list? val 2 top 12 nil 7 3 11 p

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val)do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; What if value to be added is less than value of first node in list? val 2 top 12 nil 7 3 11 p

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; Not exactly where we wanted- How can we fix this? val 2 top 12 nil 7 3 11 p 2

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if (top^.data < val) begin p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); This will handle the case where new node is to be inserted before first node in list. Same as add to a stack. val 2 top 12 nil 7 3 11

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if (top^.data < val) then begin p := top; while(p^.next^.data < val) do p := p^.next; new(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); What if value to be added is larger than value of last node in list? val 15 top 12 nil 7 3 11

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if (top^.data < val)then begin p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); Example: p points to last node in list; p^.next is NIL; evaluation of p^.next^.data will cause program crash val 15 top 12 nil 7 p 3 11

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if (top^.data < val) then begin p := top; while(p^.next^.data < val) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); How can we solve this problem? val 15 top 12 nil 7 p 3 11

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if (top^.data < val) then begin p := top; while((p^.next <> nil) AND (p^.next^.data < val)) do p := p^.next; new(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); Solution: also stop moving p if it's the last node in the list - i.e. if p^.next is nil. val 15 top 12 nil 7 p 3 11

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if (top^.data < val)then begin p := top; while((p^.next <> nil) AND (p^.next^.data < val)) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); What if value is to be added to an empty list, i.e. it is the first item to be added to the list? val 8 top nil

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if (top^.data < val) then begin p := top; while((p^.next <> nil) AND (p^.next^.data < val)) do p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); attempt at evaluation of top^.data will cause program crash val 8 top nil

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if (top^.data < val) then begin p := top; while((p^.next <> nil) AND (p^.next^.data < val)) p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); How can we fix this? val 8 top nil

Implementing insert_in_order(val,L) Step 1. Create pointer p, moving p to point to the node after which val should be added. if ((top <> nil) AND (top^.data < val)) then begin p := top; while((p->next != NULL) && (p->next->data < val)) do p = p->next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); Solution: Also check first to make sure list isn’t empty. val 8 top nil

Putting it all together ... procedure insert_in_order(val:integer; var top:ptrtype) { if ((top <> nil) and (top^.data < val)) then begin p := top; while((p^.next <> nil) and (p^.next^.data < val)) p := p^.next; New(temp); temp^.data := val; temp^.next := p^.next; p^.next = temp; end else add2front(val,top); } Check to make sure list isn’t empty. Will handle case where new node is to be inserted before first node in list. Will handle case where new node is to be inserted at end of the list.

First Insertion Linked List Program Menu Add Show Quit Pushes Remove Save to file Load from file Do it recursively!!!

Recursive insert_in_order(val,L) Let’s first look at the base case(s) and see what we do. Case 1: Inserting value into an empty list. val 8 top What do we do? Add new node containing value to front of list, i.e. add2front(val, top);

Recursive insert_in_order(val,L) Case 2: Value to be added is smaller than value of first node in list (i.e. top^.data^.val). val 2 top 12 / 7 3 11 What do we do? Add new node containing value to front of list, i.e. add2front(val, top);

Recursive insert_in_order(val,L) Otherwise, will insert value somewhere in sublist pointed to by top^.next val 8 top 12 / 7 3 11 i.e., insert_in_order(val, top^.next);

Recursive insert_in_order(val,L) Recursive definition for insert_in_order(val,top): add2front(val,top) if top = nil or top^.data<val (Base case- empty list or value to be insert_in_order(val, top) = inserted smaller than first item in list) insert_in_order(val,top^.next) (Recursive case- otherwise)

Recursive insert_in_order(val,top) As a recursive function definition: procedure insert_in_order(val:real; var top:pointerType); begin if (top <> nil and top^.data < val) insert_in_order(val,top^.next) else add2front(val,top); End;

val L procedure insert_in_order(val:real;var top:ptrtype) begin if (top <> nil and top^.data < val) insert_in_order(val,top^.next); else add2front(val,top); End; val 9 L 12 / 7 3 11

val L Procedure insert_in_order(val:real;var top:ptrtype) begin if (top <> nil and top^.data < val) insert_in_order(val,top^.next); else add2front(val,top); end; void insert_in_order(double val, Node* &L) { if (L != NULL && L->data < val) insert_in_order(val,L->next); else add2front(val,L); } procedure insert_in_order(val:real;var top:ptrtype) begin if (top <> nil and top^.data < val) insert_in_order(val,top^.next); else add2front(val,top); end; val 9 L 12 / 7 11 3

val L Procedure insert_in_order(val:real, var top:pointerType) begin if (top <> nil and top^.data < val) insert_in_order(val,>next); else add2front(val,L); end; void insert_in_order(double val, Node* &L) { if (L != NULL && L->data < val) insert_in_order(val,L->next); else add2front(val,L); } procedure insert_in_order(val:real; var top:pointerType) begin if (top<> nil and top^.data < val) insert_in_order(val, top^.next); else add2front(val,top); End; Procedure insert_in_order(val:real; var top:pointerType) begin if (top <> nil and top^.data < val) insert_in_order(val,top^.next); else add2front(val,top); End; void insert_in_order(double val, Node* &L) { if (L != NULL && L->data < val) insert_in_order(val,L->next); else add2front(val,L); } val 9 L 12 / 7 11 3

val L procedure insert_in_order(val:real; var top:pointerType) begin if (top <> nil and top^.data < val) insert_in_order(val,top^.next); else add2front(val,top); end; void insert_in_order(double val, Node* &L) { if (L != NULL && L->data < val) insert_in_order(val,L->next); else add2front(val,L); } procedure insert_in_order(val:real, var top:pointerType) begin if (top <> nill and top^.data < val) insert_in_order(val,top^.next); else add2front(val,top); End; void insert_in_order(double val, Node* &L) { if (L != NULL && L->data < val) insert_in_order(val,L->next); else add2front(val,L); } Procedure insert_in_order(val:real; var top:pointerType) begin if (top <> nil and top^.data < val) insert_in_order(val, top^.next); else add2front(val,top); End; val 9 L 12 / 7 11 3 9

val L void insert_in_order(double val, Node* &L) { Procedure insert_in_order(val:real; var top:pointerType) begin if (top <> nil and top^.data < val) insert_in_order(val, top^.next); else add2front(val,top); End; void insert_in_order(double val, Node* &L) { if (L != NULL && L->data < val) insert_in_order(val,L->next); else add2front(val,L); } procedure insert_in_order(val:real; var top:pointerType) begin if (top <> nil and top^.data < val) insert_in_order(val, top^.next); else add2front(val,top); End; val 9 L 12 / 7 11 3 9

val L procedure insert_in_order( val:real, var top:pointerType) { if (top <> nil AND top^.data < val) insert_in_order(val, top^.next); else add2front(val,top); } val 9 L 12 / 7 3 11 9

We're Done! val 9 L 12 / 7 3 11 9