Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

DATA STRUCTURES

ASSIGNMENT #3

Vinay Ratnapu

110-00-1127

1)Write an algorithm or code segment for searching a circular linked

list for a given item.

The logical and mathematical model of a particular organization of data is called Data

structure. The main characteristics of a data structure are:

Contains component data items which may be atomic or another data structure

A set of operations on one or more of the component items

Defines rules as to how components relate to each other and to the structure as a whole

The choice of a particular data structure depends on following

consideration:

It must be rich enough in structure to mirror actual relationships of data in real world for

example the hierarchical relationship of the entities is best described by the “tree” data

structure.

The structure should be simple enough that one can effectively process the data when

necessary.

Circular Linked List- A circular linked list is a linked list in which last element or node

of the list points to first node. For non-empty circular linked list, there are no NULL

pointers. The memory declarations for representing the circular linked lists are the same
as for linear linked lists. All operations performed on linear linked lists can be easily

extended to circular linked lists with following exceptions:

While inserting new node at the end of the list, its next pointer field is made to point to

the first node.

While testing for end of list, we compare the next pointer field with address of the first

node Circular linked list is usually implemented using header linked

list. Header linked list is a linked list which always contains a special node called the

header node, at the beginning of the list. This header node usually contains vital

information about the linked list such as number of nodes in lists, whether list is sorted

or not etc. Circular header lists are frequently used instead of ordinary linked lists as

many operations are much easier to state and implement using header lists

This comes from the following two properties of circular header linked lists:

The null pointer is not used, and hence all pointers contain valid addresses

Every (ordinary ) node has a predecessor, so the first node may not require a special case.

Algorithm: (Traversing a circular header linked list)

This algorithm traverses a circular header linked list with

START pointer storing the address of the header node.

Step 1: Set PTR:=LINK[START]

Step 2: Repeat while PTR≠START:

Apply PROCESS to INFO[PTR]

Set PTR:=LINK[PTR]

[End of Loop]

Step 3: Return
Searching a circular header linked list

Algorithm: SRCHHL(INFO,LINK,START,ITEM,LOC)

This algorithm searches a circular header linked list

Step 1: Set PTR:=LINK[START]

Step 2: Repeat while INFO[PTR]≠ITEM and PTR≠START:

Set PTR:=LINK[PTR]

[End of Loop]

Step 3: If INFO[PTR]=ITEM, then:

Set LOC:=PTR

Else:

Set LOC:=NULL

[End of If structure]

Step 4: Return

2) Write an algorithm or code segment for locating the nth successor of

an item in a circular linked list (the nth item that follows the given item

in the list).

Algorithm: INSRT (INFO,LINK,START,AVAIL,K,ITEM)

This algorithm inserts in a circular linked list after the kth node

Step 1: If AVAIL:=NULL, then

Write: ‘OVERFLOW’

Return
Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]

Set INFO[NEW]:=ITEM

Step 3: Set PTR:=START and N:=1

Step 4: If N=K, then

Set LINK[NEW]:=LINK[PTR]

Set LINK[PTR]:=NEW

Return

Step 5: Set PTR:=LINK[PTR] and

Step 6: Repeat while N ≠ K

Set PTR:=LINK[PTR] and N:=N+1

[End of if structure]

Step 7: Set LINK[NEW]:=LINK[PTR]

Set LINK[PTR]:=NEW

Step 8: Return

3) Give an algorithm similar to that in the text for threading a binary

tree, but to facilitate preorder traversal.

The most common form of tree maintained in computer is binary tree.

Binary Tree- A binary tree T is defined as a finite set of elements, called nodes, such

that either:

T is empty (called null tree or empty tree) or,


T contains a distinguished node, R, called root of T and remaining nodes of T form an

ordered pair of disjoint binary trees T1 and T2

Two trees T1 and T2 are called respectively left and right subtree of R (root node of T). If

T1 is nonempty, then its root is called left successor of R. Similarly, If T2 is nonempty,

then its root is called right successor of R

B C

D E G H

F J K

The nodes D,F,G,L,K are the terminal or leaf nodes

Algorithm: PREORD(INFO, LEFT, RIGHT, ROOT)

This algorithm traverses the tree in preorder

Step 1: Set TOP:=1, STACK[1]:=NULL and PTR:= ROOT

Step 2: Repeat Step 3 to 5 while PTR≠NULL

Step 3: Apply PROCESS to INFO[PTR]

Step 4: [Right Child ?]

If RIGHT[PTR] ≠ NULL, then:

Set TOP:=TOP + 1

Set STACK[TOP]:= RIGHT[PTR]

[End of If structure]

Step 5: [Left Child ?]

If LEFT[PTR] ≠ NULL, then:


Set PTR:=LEFT[PTR]

Else:

Set PTR:=STACK[TOP]

Set TOP:=TOP-1

[End of If structure]

[End of Step 2 Loop]

Step 6: Return

4)Write a recursive version of the linked-list-based linear search

algorithm.

Ans:Recursion- Recursion is a process by which a function calls itself repeatedly, until

some specified condition has been satisfied. The process is used for repetitive

computations in which each action is stated in terms of the previous result. Many

iterative (or repetitive) problems can be written in this form. In order to solve a problem

recursively, two conditions must be satisfied

Problem must be written in a recursive form

Problem statement must include a stopping condition.

#include <iostream>

#include <conio.h>

using namespace std;

int linearSearch(int array[],int counter)

--counter;
if (counter < 0)

return -1;

if (array[counter] == 10)

return (counter+1);

else

return linearSearch(array,counter);

You might also like