CS112_23_Slides-Queues-1(1)

CS112_23_Slides-Queues-1(1) - Queues-one of the linear...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CS112 Fundamentals of Programming Abstractions Queues-one of the linear structures. Yinglin Wang CS Dept., SJTU CS112 Fundamentals of Programming Abstractions Main points: Stacks can be implemented by linked lists. Stacks & queues are two important kinds of linear structures Implement queues by arrays or by linked lists.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions wrong implementation of stacks Typedef struct cellT{ stackElement element; struct cellT *link; }cellT; struct stackCDT{ cellT *start; }; Typedef Struct cellT *stackADT; Typedef Struct stackCDT *stackADT stackADT NewStack(void) { return(NULL); } See page 421-422 CS112 Fundamentals of Programming Abstractions wrong implementation of stacks StackADT mystack; mystack=NewStack(); push(myStack, e 1 ) mystack NULL
Background image of page 2
CS112 Fundamentals of Programming Abstractions wrong implementation of stacks StackADT mystack; mystack=NewStack (); push(myStack, e 1 ) mystack NULL stack NULL element e 1 push interface CS112 Fundamentals of Programming Abstractions Add new functions to stackADT Next we will consider exporting a new function from the stackADT. Assuming that we already have a function that will print an element, the new function, PrintStack, prints the entire stack.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions implementation We are writing implementation side code for PrintStack. Here is the definition of the CDT and the cellT that is used to build the linked list representation of the stack. CS112 Fundamentals of Programming Abstractions Implementation of PrintStack We will discuss two ways of writing PrintStack. Both represent important techniques for programming with linked lists. First we will use a recursive function. This is a natural thing to do, since the data structure for a cellT is itself recursive. The recursive function is a helper, since PrintStack has a stackADT as its argument. The recursive function needs a cellT * as its argument.
Background image of page 4
CS112 Fundamentals of Programming Abstractions Recursive implementation Here is the code that does the work. Notice how the code matches the data structure! It is very simple and exactly what you would expect. This code is
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

CS112_23_Slides-Queues-1(1) - Queues-one of the linear...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online