mc 0068-02 data structures using c

14
MC 0068 – 02 DATA STRUCTURES USING ‘C’ 1) Illustrate to implement dequeue using circular linked list. #include <stdio.h> #include <alloc.h> #include <process.h> struct node { int info; struct node *link; }; typedef struct node*NODE; /*function to get a node from the availability list */ /*function to return node to availability list */ /*function to insert an item at the front end */ /*function to insert an item at the rear end */ /*function to delete an item from the front end */ /*function to delete an item from the rear end */ /*function to display the contents of the list */ void main ( ) { NODE last; int choice, item; 1

Upload: shrikrush

Post on 24-Dec-2015

216 views

Category:

Documents


0 download

DESCRIPTION

SMUDE MCA MC0068 -2

TRANSCRIPT

Page 1: MC 0068-02 Data Structures Using c

MC 0068 – 02

DATA STRUCTURES USING ‘C’

1) Illustrate to implement dequeue using circular linked list.

#include <stdio.h>

#include <alloc.h>

#include <process.h>

struct node

{

int info;

struct node *link;

};

typedef struct node*NODE;

/*function to get a node from the availability list */

/*function to return node to availability list */

/*function to insert an item at the front end */

/*function to insert an item at the rear end */

/*function to delete an item from the front end */

/*function to delete an item from the rear end */

/*function to display the contents of the list */

void main ( )

{

NODE last;

int choice, item;

last = NULL;

for (;;)

{

printf (“1 : Insert _Front 2: Insert_Rear\n”);

1

Page 2: MC 0068-02 Data Structures Using c

printf (“3 : Delete _ Front 4: Delete _ Rear\n”);

printf (“5 : Display 6: Exit\n”);

printf (“enter the choice\n”);

scanf (“%d”, &choice);

switch (choice)

{

case 1:

printf(“Enter the item to be inserted\n”);

scanf (“%d”, &item);

last = insert _ front (item, last);

break;

case 2 :

printf (“Enter the item to be inserted\n”);

scanf (“%d”, &item);

last = insert _ rear (item, last);

break;

case 3 :

last = delete _ front (last);

break;

case 4 :

last = delete _ rear (last);

break;

case 5 :

display (last);

break;

default:

exit ( 0);

}

}

2

Page 3: MC 0068-02 Data Structures Using c

2) What do you mean by searching? Define searching problem and how do you

evaluate the performance of any searching algorithm?

In computers information retrieval is one of the most important applications. It usually

involves giving a piece of information called the key, and ask to find a record that

contains other associated information. This is achieved by first going through the list to

find if the given key exists or not. This process is called Searching. Computer systems

are often used to store large amounts of data from which individual records must be

retrieved according to some search criterion. The process of searching an item in a data

structure can be quite straightforward or very complex. Searching can be done on

internal data structures or an external data structures.

Searching methods are designed to take advantage of the file organization and

optimize the search for a particular record or to establish its absence. The file

organization and searching method chosen can make a substantial difference to an

application’s performance.

3) Compare and contrast DFS and BFS and DFS + ID approaches

Once we have identified a problem as a search problem, it’s important to choose the

right type pf search. The below information may be useful while taking the decision.

In a Nutshell

3

Page 4: MC 0068-02 Data Structures Using c

Search Time Space When to use

DFS O(c^k) O(k) Must search tree anyway, know the

level the answers are on, or you

aren’t looking for the shallowest

number

BFS O(c^d) O(c^d) Know answers are very near top of

tree, or want shallowest answer.

DFS+ID O(c^d) O(d) Want to do BFS, don’t have enough

space, and can spare the time

d is the depth of the answer

k is the depth searched

d < = k

We have to remember the ordering properties of each search. If the program needs

to produce a list sorted shortest solution first ( in terms of distance from the root

node), use breadth first search or iterative deepening. For other orders, depth firs

search is the right strategy.

If there isn’t enough time to search the entire tree, use the algorithm

that is more likely to find the answer. If the answer is expected to be in one of the

rows of nodes closest to the root, use breadth first search or iterative deepening.

Conversely, if the answer is expected to be in the leaves, use the simpler depth first

search. We have to be sure to keep space constraints in mind. If memory is

insufficient to maintain the queue for breadth first search but time is available, we

have to use iterative deepening.

4

Page 5: MC 0068-02 Data Structures Using c

5) Write note on

i) Threaded lists ii) Dynamic hashing

Threaded lists:-

A modification of the implementation of a list structure which makes it resemble

a set of ring structures is to make the right hand pointer of the last element of a sublist

point back to the head of the sublist. Each sublist has become effectively a ring

structure. That will now has what is commonly called a Threaded List. the major

advantage associated with a threaded list is that it can be traversed without the aid of a

stack. Normally when traversing a conventional list structure the return addresses are

stacked, whereas in the threaded list they have been incorporated in the data structure.

One disadvantage associated with the use of list and ring structures for

representing classifications is that they can only be entered at the ‘top’. An additional

index giving entry to the structure at eaxh of the data elements increases the update

speed considerably. Another modification of the simple list representation has bee

5

Page 6: MC 0068-02 Data Structures Using c

studied extensively by Stanfel and Patt. The individual elements (or cells) of the list

structure are modified to incorporate one extra field, so that instead of each element

looking like this.

P1 P2 this looks like S P1 P2

where the P ‘s are pointers and S is a symbol. Otherwise no essential change has been

made to the simple representation. This structure has become to known as Doubly

chained Tree. Its properties have mainly been investigated for storing variable length

keys, where each key is made up by selecting symbols from a finite (usually small)

alphabet. For Ex, (A,B,c) be the set of key symbols and R1, R2, R3, R4, R5 be five

records to be stored.

Let us assign keys made of the 3 symbols, to the record as follows:

AAA R1

AB R2

AC R3

BB R4

BC R5

Below is an example of a doubly chained tree containing the keys and giving access to

the records. The top most element contains no symbol, it merely functions as the start of

the structure. Given an arbitrary key its presence or absence is detected by matching it

against keys in the structure. Matching proceeds level by level, once a matching symbol

has been found at one level, the P1 pointer is followed to the set of alternative symbols

at the next level down. The matching will terminate either

a) when the key is exhausted, that is, no more key symbols are left to match; or

b) when no matching symbol is found at the current level

6

Page 7: MC 0068-02 Data Structures Using c

For case a) we have-

i. the key is present if the P1 pointer in the same cell as the last matching

symbol now points to a record;

ii. P1 points to a further symbol, that is, the key ‘fails short’ and is therefore

not in the structure.

For case b), we also have that the key is not in the structure, but now there is a

mismatch.

Stanfel and Patt have concentrated on generating search trees with minimum expected

search time, and preserving this property despite updating. For the detailed mathematics

demonstrating that this is possible the reader is referred to their cited work.

Dynamic Hashing

As the data base grows over time, we have three options:

- choose hash functions based on current file size. Get performance

degradations as file grows.

- Choose hash functions based on anticipated file size. Space is wasted

initially

7

Page 8: MC 0068-02 Data Structures Using c

- Periodically re-organize hash structure as file grows. Requires

selecting new hash function, recomputing all addresses and generating

new bucket assignments. Costly, and shuts down database.

Some hashing techniques allow the hash function to be modified dynamically to

accommodate the growth or shrinking of the database. these are called dynamic has

Function. Extendable hashing is one form of dynamic hashing. Extendable hashing

splits and coalesces buckets as database size changes. This imposes some performance

overhead, but space efficiency is maintained. As reorganization is on one bucket at a

time, overhead is acceptably low.

Working of dynamic hashing-

a) We choose a hash function that is uniform and random that generates values over

a relatively large range.

- Range is b-bit binary integers (typically b = 32).

- 232 is over 4 billion, so we don’t generate that many buckets!

- Instead we create buckets on demand, and do not use all b bits of the

hash initially.

- At any point we use i bits where 0 ≤ i≤ b.

- The i bits are used as an offset into a table of bucket addresses.

- Value of i grows and shrinks with the database.

- Note that the i appearing over the bucket address table tells how many

bits are required to determine the correct bucket.

- It may be case that several entries point to the same bucket.

- All such entries will have a common hash prefix, but the length of this

prefix may be less than i.

- So we give each bucket an integer giving the length of the common

hash prefix.

- Number of bucket entries pointing to bucket j is 2(i-ij).

8

Page 9: MC 0068-02 Data Structures Using c

b) To find the bucket containing search key value Ki.

- Compute h(Ki).

- Take the first/high order bits of h(Ki).

- Look at the corresponding table entry for this i-bit string.

- Follow the bucket pointer in the table entry.

c) We now look at insertions in an extendable hashing scheme.

- Follow the same procedure for lookup, ending up in some bucket j.

- If there si room in the bucket, insert information and insert record in

the file.

- If the bucket is full, we must split the bucket, and redistribute the

records.

- If bucket is split, we may need to increase the number of bits we use

in the hash.

d) Deletion of records is similar. Buckets may have to be coalesced, and

bucket addres table may have to be halved.

Advantages:-

- Extendable hashing provides performance that does not degrade as the

file grows.

- Minimal space overhead – no buckets need be reserved for future use.

- Bucket address table only contains one pointer for each value of

current prefix length.

Disadvantages:-

- Extra level of indirection in thew bucket address table.

9

Page 10: MC 0068-02 Data Structures Using c

- Added complexity.

10