cp5_6_ps08_sol

cp5_6_ps08_sol - CP5_6 The problems in this problem set...

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

View Full Document Right Arrow Icon
CP5_6 The problems in this problem set cover lectures C5 and C6. 1. a. What are doubly linked lists? What is the record declaration for a node in a doubly linked list? 10 11 13 head Figure 1. Sample Doubly Linked List Doubly linked lists have two pointers instead of the single pointer seen in singly linked lists. The pointers point to both the previous node in the list as well as the next node in the list. type Listnode is record Element : Elementtype; Next : Listptr; Prev : Listptr; -- this is the change made to singly linked lists end record ; b. Write an algorithm to insert a node into a sorted doubly linked list. Use a diagram to show the sequence of operations that have to be performed to carry out the insertion step. Hint: Extend the approach used in class/ notes for singly linked lists. Preconditions: 1. User passes the list (called List) and the element to be inserted (called Element) to the insert procedure 2. List is already sorted Postconidtions: 1. Procedure returns the list with the element inserted in the correct position 2. List remains sorted Algorithm: Create three temporary Listptrs Current , Previous and NewNode Previous := null Current := List.Head; NewNode := new Listnode; NewNode.Element := Element
Background image of page 1

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

View Full DocumentRight Arrow Icon
Loop exit when Current = Null exit when Current.Element > Element Previous := Current; Current := Current.Next; NewNode. Next := Current; NewNode.Prev:= Previous; If Previous = null L.Head := NewNode else Previous.Next := NewNode If Current /= null Current.Prev := NewNode; Return List Consider the doubly linked list shown in Figure 1. The insertion of a node with element 12 in the list, the insert operation is shown in Figures 2 and 3. 10 11 13 head Initial State 10 11 13 head Previous Current 12 NewNode When loop is exited Figure 2. Prior to Insertion
Background image of page 2
10 11 13 12 NewNode NewNode.Next := Current Previous Current head NewNode.Prev:= Previous 10 11 13 12 NewNode Previous Current head Previous.Next := NewNode 10 11 13 12 NewNode Previous Current head
Background image of page 3

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

View Full DocumentRight Arrow Icon
Current.Prev := NewNode 10 11 13 12 NewNode Previous Current head Figure 3. After Insertion Operation c. Implement your algorithm as an Ada95 program. Package Specification
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/28/2012 for the course AERO 16.01 taught by Professor Markdrela during the Fall '05 term at MIT.

Page1 / 10

cp5_6_ps08_sol - CP5_6 The problems in this problem set...

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

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