Doubly Linked Lists-Computer Programming-Assignment Solution, Exercises of Aeronautical Engineering

This is solution to assignment for Aeronautical Engineering and Computer Programming course. It was submitted to Prof. Chitraksh Gavde at Biju Patnaik University of Technology, Rourkela. It includes: List, Doubly, Linked, Problem, Pointer, Programming, Procdure,Postcondition, Precondition, Loop, Else, Return

Typology: Exercises

2011/2012

Uploaded on 07/20/2012

savitri_122
savitri_122 🇮🇳

4.6

(14)

184 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Doubly Linked Lists-Computer Programming-Assignment Solution and more Exercises Aeronautical Engineering in PDF only on Docsity!

CP5_

The problems in this problem set cover lectures C5 and C6.

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

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

When loop is exited^ NewNode

Figure 2. Prior to Insertion

Current.Prev := NewNode

10 11 13

12

NewNode

head Previous Current

Figure 3. After Insertion Operation

c. Implement your algorithm as an Ada95 program.

Package Specification

GNAT 3.15p (20020523) Copyright 1992-2002 Free Software Foundation, Inc.

Checking: c:/docume~1/jayaka~1/mydocu~1/16070/code/doubly_linked_list.ads (source file time stamp: 2004-03-31 20:46:40)

  1. -- Specification for doubly-linked lists

  2. -- Specified: Jayakanth Srinivasan

  3. -- Last Modified: February 11, 2004


  4. package Doubly_Linked_List is

  5. subtype Elementtype is Integer;

  6. type Listnode;

  7. type Listptr is access Listnode;

  8. type Listnode is

  9. record

  10. Element : Elementtype;

  11. Next : Listptr;

  12. Prev : Listptr; -- this is the change made to singly linked lists

  13. end record;

  14. type List is

  15. record

  16. Head : Listptr;

  17. end record;

  18. procedure Makeempty (

  19. L : in out List );

  20. -- Pre: L is defined

  21. -- Post: L is empty

  22. function Isempty (

  23. L : in List )

  24. return Boolean;

  25. -- Pre: L is defined

  26. -- Post: returns True if L is empty, False otherwise

  27. procedure Display (

  28. L : in List );

  29. -- Pre: L may be empty

  30. -- Post: displays the contents of L's Element fields, in the

  31. -- order in which they appear in L

  32. procedure Initialize (

  33. L : in out List );

  34. -- Pre: L may be empty

  35. -- Post: Elements inserted into the list at correct position

  36. procedure Insert_In_Order (

  37. L : in out List;

  38. Element : in Elementtype );

  39. end Doubly_Linked_List;

54 lines: No errors

Package Implementation

GNAT 3.15p (20020523) Copyright 1992-2002 Free Software Foundation, Inc.

Compiling: c:/docume~1/jayaka~1/mydocu~1/16070/code/doubly_linked_list.adb (source file time stamp: 2004-03-31 20:48:14)


  1. -- Implementation for doubly-linked lists
  2. -- Programmer: Jayakanth Srinivasan
  3. -- Last Modified: Feb 11, 2004

  4. with Ada.Text_Io;
  5. with Ada.Integer_Text_Io;
  6. with Ada.Unchecked_Deallocation;
  7. use Ada.Text_Io;
  8. use Ada.Integer_Text_Io;
  9. package body Doubly_Linked_List is
  10. -- create an instance of the free procedure
  11. procedure Free is
  12. new Ada.Unchecked_Deallocation(Listnode, Listptr);
  13. -- check if list is empty. List.Head will be null
  14. function Isempty (
  15. L : in List )
  16. return Boolean is
  1. -- create a node and set the data to element
  2. Newnode := new Listnode;
  3. Newnode.Element:= Element;
  4. -- check if the list is empty.
  5. if Isempty(L)= False then
  6. loop
  7. -- need two separate exits, otherwise there will be
  8. -- an execption at runtime
  9. exit when Current = null;
  10. exit when Current.Element >Element;
  11. Previous := Current;
  12. Current := Current.Next;
  13. end loop;
  14. end if;
  15. -- do insertion
  16. Newnode.Prev:= Previous;
  17. Newnode.Next := Current;
  18. if Previous = null then
  19. -- list is empty
  20. L.Head := Newnode;
  21. else
  22. Previous.Next := Newnode;
  23. if Current /= null then
  24. Current.Prev := Newnode;
  25. end if;
  26. end if;
  27. end Insert_In_Order;
  28. end Doubly_Linked_List;

108 lines: No errors

Test Program

GNAT 3.15p (20020523) Copyright 1992-2002 Free Software Foundation, Inc.

Compiling: c:/docume~1/jayaka~1/mydocu~1/16070/code/doubly_list_test.adb (source file time stamp: 2004-03-31 20:49:02)


  1. -- Program to test the doubly linked list package
  2. -- Programmer: Jayakanth Srinivasan
  3. -- Date Last Modified : Feb 11, 2004

  4. with Doubly_Linked_List;
  5. use Doubly_Linked_List;
  6. procedure Doubly_List_Test is
  7. My_List : List;
  8. begin
  9. -- initialize the list
  10. Initialize(My_List);
  11. -- insert 1,3,2,4 into the list and display at each stage
  12. Insert_In_Order(My_List, 1);
  1. Display(My_List);
  2. Insert_In_Order(My_List, 3);
  3. Display(My_List);
  4. Insert_In_Order(My_List, 2);
  5. Display(My_List);
  6. Insert_In_Order(My_List, 4);
  7. Display(My_List);
  8. end Doubly_List_Test;

27 lines: No errors

2. What is the Shortest Path through the graph shown below using Dijkstra’s algorithm.

Assume node A is the start node.

Show all the steps in the computation of the shortest path.

Initialize

V = {a, b, c, d, e}

E = {(a,b), (a,c), (b,e), (b,c), (c,b), (c,d), (c,e), (d,a), (d,e), (e.d)}

S = {→}

Q = {a, b, c, d, e}

D = [0, ∞, ∞,∞,∞]

Previous = [0, 0, 0, 0, 0]

Start at A

Relax (a,b,10)

Relax (a,c,5)

S = {a}

Q = {b, c, d, e}

b e

a

c d

For a graph G = (V, E). where V is a finite nonempty set of vertices and E is the set

of edges,

a. Walk

A walk is a sequence of vertices (v1, v2, …, vk) in which each adjacent vertex

pair is an edge

b. Path

A path is a walk with no repeated nodes

c. Eulerian Path

An Eulerian path in a graph is a path that uses each edge precisely once.

d. Cycle

A cycle is a path that begins and ends with the same vertex

e. Degree of a vertex

The degree of a vertex in an undirected graph is the number of edges incident

with it, except that a loop at a vertex contributes twice to the degree of that vertex

In a directed graph, the degree of the vertex is partitioned into indegree (number

of edges entering a vertex) and outdegree (number of edges leaving a vertex).

Note that a loop contributes to both the indegree and the outdegree.