Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 2 Outline/Learning Objectives for the day:

Similar presentations


Presentation on theme: "Computer Science 2 Outline/Learning Objectives for the day:"— Presentation transcript:

1 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.

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

3 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.

4 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);

5 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

6 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

7 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

8 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

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

10 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

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; val 9 top 12 nil 7 3 11 p

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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.

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

31 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);

32 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);

33 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);

34 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)

35 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;

36 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

37 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

38 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

39 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

40 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

41 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

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


Download ppt "Computer Science 2 Outline/Learning Objectives for the day:"

Similar presentations


Ads by Google