Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Paper 1 - Data Structures Using C': Chapter - 1 - Introduction

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 21

PAPER 1 - DATA STRUCTURES USING C

CHAPTER -1 INTRODUCTION
1. DATA STRUCTURE: a data structure is collection of data elements whose arrangement is
characterized by accessing functions that are used to store and retrieve individual data elements.
Data structures are classified into
1. primitive type
2. . non primitive type
Primitive data types are basic data types that directly operated upon the machine instructions.
Non primitive data types are derived from primitive data types. These are group of homogeneous
and non homogeneous data items.
Homogeneous data structures contains single data type of data. Non homogeneous structures
contains different types data elements.

STRUCTURE: structure is group of logically related data items, which are stored in contiguous
memory locations with common structure name of different data types
Structure may contains different data type elements. These are called members of a structure
Syntax:
Struct structure-name
{
Data-type1

member1.;

Data-type2

member2;

Data-type3

member3;

..
};
Structure definition. Suppose x,y are students and no. number, age are structure elements then we can
declare structure as follows
Struct student
1

{
Int no;
Char name[20];
Int age;
} x,y;

Here student is structure name. and x,y are two students .


We can access the members in structure using dot(.) operator
Ex: x.no, x.name, y.age

CHAPTER -2-STACK
1. STACK: stack is a data structure in which data elements are inserted and deleted at
one end called TOP of the stack.
The TOP is called as stack pointer.
The two operations performed using stack are
a. PUSH(): it is used to insert new elements into the stack
b. POP(); this function is used to retrieve data elements from the stack
push

top

pop

40
30
20
10

2. ALGORITHM TO PERFORM STACK OPERATIONS.


2

PUSH()
S

an array of stack

TOP

stack pointer

item to insert into stack

1. //Check for stack overflow//


If TOP >= max then
Print (stack over flow);
Return
End if
2. // increment stack pointer to provide space for new element//
TOP TOP + 1;
3. // insert x at TOP of the stack //
S[TOP] X;
Return
END PUSH()
POP ()
S

an array of stack

TOP

stack pointer

item to insert into stack

1. // check for empty stack //


If TOP == 0 then
Print (stack is empty);
Return
End if
3. // retrieve element from the stack //
3

Y S[TOP];
4. // decrement stack pointer to save the space//
TOP TOP 1;
Return(Y);
END POP
3.INFIX, PREFIX, POSTFIX:
An algebraic expression is combination of operands and operators. The operand may
be visible like a,b..x,y.. or 5, 4 , etc., operators are symbols which are used to do
mathematical or logical operation on operands.
An algebraic expression can be represented using three different notations. They are infix,
postfix, prefix notations .
a. Infix : in infix notation we place the arithmetic operator in between the two
operands.
Ex: (A-B) * (C-D)
b. Prefix : in prefix notation we place the arithmetic operator before the two operands.
Ex : (A-B) * (C-D) > infix
-AB * - CD
* -AB CD - prefix notation
c. postfix : in post fix notation we place the arithmetic operator after the two operands.
Ex : (A-B) * (C-D) > infix
AB * CD
AB CD - * postfix
Three important features of postfix are
1. the operands maintain the same order as in the expression
2. the parenthesis are not needed to designated the expression
3. while evaluating postfix expression the priority of the operators is no longer
relevant.

3. RECURSION
4

Recursion: Recursion is a function, which calls itself directly or indirectly is called


recursive function.
There are two types of recursions are there
1. direct recursion
2. indirect recursion
1. Direct recursion: The recursion in which the function calls it self is called
direct recursion.
Ex; int sum (int a)
{

If (condition)
Var = sum(a);
}
Indirect recursion : in this recursion two or more functions are involved. For
example: If first function calls second function, then second function calls first
function again.
EX; int sum(int a)
{

If(condition)
Var = tot(a);
}
Int tot(a)
{

If(condition)
Var = sum(a);
}
In this example function sum ( ) calls function tot ( ), and function tot ( )
calls sum ( ) function again, this is known as indirect recursion.
2.

Advantages of Recursion:
1. using recursion the length of the program is reduced
2. Recursion is used in stacks, queues and lists.
Disadvantages:
1. it needs extra storage space
2. when there is no exit condition, the program will execute out of memory
5

3. it is not efficient in speed and time.


Write a program to generate Fibonacci series using recursion
Fibonacci series means 1 1 2 3 5 8 13 21 34 .
Except first two numbers every number in the series is sum of previous two
numbers .
Program
#include<stdio.h>
main()
{
int n,fib,i;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
printf(Fibonacci series is);
for(i=1;i<=n;i++)
{
fib = fibo(i);
printf("%5d",fib);
}
getch();
}
fibo(n)
{
int i=1,j=0;
while(j<=n)
{
if(n==1)
return(i);
else
if(n==2)
return(i);
else
i = fibo(n-1)+fibo(n-2);
j++;
return(i);
}
}
Output
6

Enter the value of n


5
Fibonacci series is
11235
Write a program to find factorial of a given number using recursion
Ex: factorial of n is n!
We can find factorial of n as follows
n! = n* (n-1)*(n-2)*.
5! = 5*4*3*2*1
program
#include<stdio.h>
main()
{
int res,n;
clrscr();
printf(enter the value of n);
scanf("%d",&n);
res = fib(n);
printf("factorial of %d = %d",n, res);
getch();
}
fib(int n)
{
int i=1;
if(n==1)
return(i);
else
i=n*fib(n-1);
return(i);
}
Output\
enter the value of n 3
factorial of 3 = 6

4. QUEUES AND LISTS


2 MARKS QUESTIONS
7

1. Define queue?
A) queue is an ordered list of data itemssuch that new item inserts into a queue at rear end,
and deletes an item from queue at front end.
We can represent queue using two methods
1. using pointers
2. using arrays
A

Front
rear
Algorithm to implement queue operations>.
2. what is priority queue?
A) priority queue can be represented using HEAPS. A HEAP is a data structure with the priority
that the value in every node is at least as large as the value in the child nodes. Application can
be used sorting, job scheduling, etc./
3. what is circular queue?
A) circular queue is a queue in which items are stored in circular manner, in which item are
inserted at first location follows the last location by increasing the variable .
4. what are applications of queues?
A) It is used in process management
Queue is used in multiprogramming system
5. define linked list?
A) a linked list contains elements at arbitrary locations. Each node of the list must keep trace of
next node. Linked lists are two types
a. single linked list
b. double linked list
6. define single linked list?
A) it is one directional list, consists with node , it contains two fiels
DATA field it contains information(data)
PTR field It contains the address of next node
DATA

PTR

Representation of single linked list

1000

10

2000

15

3000

20

NULL

The address of the first node present in START, the address of the second node present in the
pointer field of first node, the address of third node present in the pointer field of second node,
the last node pointer field contains NULL indicating it is a last node.
7. Define double linked list?
A) Double linked list is bi-directional(two directional) consists with nodes containing three fields
8

LPTR

DATA

RPTR

DATA It contains the information


LPTR It contains the address of predecessor(previous) node
RPTR It contains the address of successive (next) node

NULL

15

2000

1000

20

NULL

The RPTR of first node contains the address of node(20) , and the LPTR of node(20)
contains the address of node(15).
6 marks questions
1. Write the algorithm to implement queue operations?
A) QUEUE()
F front pointer of the queue
R Rear pointer the queue
Q array of elements representing queue of max size
X element to insert
At the time of creation of Q
F R 0;
1. // Check for queue overflow //
If R >= MAX then
Print( queue is overflow);
Return
End if
2. // Increment Rear pointer to provide space for inserting element //
R R + 1;
3. // Insert new element at rear end of queue //
Q[R] X;
4. // if initially, the queue is empty, adjust the front pointer//
If F = 0 then
F 1;
End if
Return
End INSERT
DELETION (QUEUE)
F front pointer of the queue
R Rear pointer the queue
9

Q array of elements representing queue of max size


Y element to delete
1. // check for queue underflow //
If F = 0 then
Print ( queueu is underflow);
Return;
Endif
2. // Delete the element at front //
Y Q[F};
3. // If queue is empty after deletion, set front and rear pointers//
If F=R then
F 0;
R 0;
End if
4. // Otherwise increment front pointer//
Else
F F + 1;
Return;
END DELTION()
2. write an algorithm to implement circular queue operations
A) QUEUE()
F front pointer of the queue
R Rear pointer the queue
CQ array of elements representing circular queue of max size
X element to insert
At the time of creation of CQ
F R 0;
1. //Adjust rear index //
If R = MAX then
R 1;
Else
R R +1;
2. // check for over flow //
If R=F then
Print(queue is overflow);
Return;
3. // insert new element //
CQ[R] X;
4. // set front pointer //
If F= 0 then
F 1;
End if
Return
End INSERT

DELETION (QUEUE)
F front pointer of the queue
10

R Rear pointer the queue


CQ array of elements representing circular queue of max size
Y element to delete
1. // check for queue underflow //
If F = 0 then
Print ( circular queue is underflow);
Return;
Endif
2. // Delete the element at front //
Y CQ[F};
3. // If queue is empty after deletion, set front and rear pointers//
If F=R then
F 0;
R 0;
End if
4. // Otherwise set front pointer//
If F = MAX then
F 1;
else
F F + 1;
Return;
END DELETION

5. TREES
1. DEFINE TREE? And explain tree terminology?
A) TREE: A tree is a finite set of elements called nodes and it has unique node called root
node, and the remaining nodes are partitioned into disjoint sets, where each node acts as
separate tree.
Many terms and there menaing can be derived with respect to tree structure as below.
A

NODE: the elements in a tree are known as nodes


A,B,C,D,E,F,G,H,I,J,K,L are the nodes in above tree
ROOT NODE : The unique node which is having Child nodes is called rood node
For above tree A is the root node
11

SUB TREE: The child nodes of a node is called sub trees


B,C,D are the sub trees of node A
DEGREE: The number of sub-trees of a node is called the degree of the node
Degree of node A is 3.
Terminal node: The nodes with degree 0( with out child nodes) are called terminal nodes
J,K,F,G,L,I are the terminal nodes
Non-terminal nodes: The nodes with degree greater than zero(0) are called non-terminal
nodes
A,B,C,D,E,H are the non-terminal nodes
Depth: the height of the tree refers levels of the tree
The depth of above tree is 4.
2. What is binary tree?
A) A binary tree s a tree in which each node has at most two children, a left child and a right
child , a binary tree is either empty or consists of root nod , left and right sub trees.
Properties of binary tree:
1. If h is height of binary tree then maximum number of leaves = 2 h
2. If h is height of binary tree then maximum number of nodes = 2 h+1 -1
3. if every non leaf node in a binary tree has left and right childs, then the tree is called strictly
binary tree or full binary tree.
4. if number nodes in full binary tree is j at any level. Then next level has 2j nodes.
3. what is binary search tree?
A) If the value of each node of a tree is greater than every value in left and less than every
value in right sub tree then it is called binary search tree.
4. Explain binary tree traversals?
A) Traversing means visiting each node exactly one time , the process of visiting each node in
a tree is called traversal of tree. It depends up on root node and left and child nodes.
The traversals of binary tree are three types
I)
Preorder(root, left child, right child)
II)
Inorder(left child, root, right child)
III)
Postorder(left child, right child, root)

I)

II)

preorder: The preorder traversal contains following steps


visit the root node
visit the left child
visit the right child
ex: A B D E C F G
Inorder: The inorder traversal contains following steps
12

visit the left child


visit the root node
visit the right child

ex: D B E A F C G
III)

Postorder: The preorder traversal contains following steps


visit the left child
visit the right child
visit the root node
ex : D E B F G C A

5. Representation of binary tree?


A) We can represent binary tree in two ways
I)
using arrays
II)
using linked lists
I)
using arrays:
A

This is the simplest way to represent a binary tree in memory, it uses a one dimensional array.
In this method
The root node is stored in first location of array
If j is the location of array , then left child stored at location 2j, and right child stored at 2j+1.
1
A

2
B

3
C

4
D

5
E

6
F

7
G

This method is not efficient when binary tree is incomplete


A

E
13

1
A

2
B

II)

4
C

8
D

9
E

.15

Using linked list: In this method each node of binary tree is represented as below
LPTR Address Of Left Child
DATA Value In The Node
RPTR Address Of The Right Child
A

The above binary tree is represented using linked list as follows


LPTR

NULL

DATA
A

NULL

RPTR

NULL

NULL

Using this method adding, deleting and traversing tree is very easy and comfort.

6) Write algorithm to traverse a binary tree?


A) Variables Used
ROOT to store root node of tree
LPTR to store address of left child
RPTR- to store address of right child
PREORDER(ROOT)
1. // check for empty tree //
If(ROOT == NULL ) then
Print(tree is empty);
Return;
End if
2. // process root node //
Print(DATA(ROOT));
14

3. // process the left sub tree //


If(LPTR(ROOT)!=NULL) then
Call PREORDER(LPTR(ROOT))
End if
3. // process the right sub tree //
If(RPTR(ROOT)!=NULL) then
Call PREORDER(RPTR(ROOT))
End if
END PREORDER
INORDER(ROOT)
1. // check for empty tree //
If(ROOT == NULL ) then
Print(tree is empty);
Return;
End if
2. // process the left sub tree //
If(LPTR(ROOT)!=NULL) then
Call INORDER(LPTR(ROOT))
End if
3. // process root node //
Print(DATA(ROOT));
4. // process the right sub tree //
If(RPTR(ROOT)!=NULL) then
Call INORDER(RPTR(ROOT))
End if
END INORDER
POSTORDER(ROOT)
5. // check for empty tree //
If(ROOT == NULL ) then
Print(tree is empty);
Return;
End if
6. // process the left sub tree //
If(LPTR(ROOT)!=NULL) then
Call POSTORDER(LPTR(ROOT))
End if
7. // process the right sub tree //
If(RPTR(ROOT)!=NULL) then
Call POSTORDER(RPTR(ROOT))
End if
8. // process root node //
Print(DATA(ROOT));
END POSTORDER

15

6.SEARCHING
1. Define searching? And write the names of 2 searching methods?
A) Searching is the process of finding a given element in an array of elements, the following are
the two methods to search
I) Linear search
II) Binary search

2. LINEAR SEARCH:
Procedure for linear search:
This method is inefficient when large numbers are present in array.
This is the simplest of all the search techniques.
We began comparing the first element of an array with key element
If it matches search ends,
Other wise the next element compare with key element ,
If no key element found search is unsuccessful search.
In worst case the number of iterations will be n.
Ex:
5

15

32

A[1]
a[2]
a[3]
a[4]
Key element = 15
If key == a[1]
False
If key== a[2]
True search successful
Algorithm for linear search
Linearsearch()
Variables user
A[]- array of n elements
Key element to find
I,f- initialize, Boolean variables
1. // initialize values//
I=1;
F=0;
2. // Searching process //
For I = 1 to n do increment by 1 do
If ( key == a[i])
F=1;
Break;
End if
Repeat
3. // successful search//
If(f==1)
Print(element found);
Else
Print(element not found);
Return
END LINEARSEARCH()
16

PROGRAM FOR LINEAR SEARCH

#include<stdio.h>
main()
{
int i,n,key,f=0,a[5];
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter the elements in array");
for(i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
printf("enter the element to found");
scanf("%d",&key);
for(i=1;i<=n; i++)
{
if (key == a[i])
{
f=1;
break;
}
}
if(f == 1)
printf("element found at location %d",i);
else
printf("element not found ");
getch();
}

3. BINARY SEARCH
Procedure for binary search:
This method is efficient when the number of elements are more
The elements in an array must always in sorted order otherwise it fails
This method starts with finding middle element of the array
If middle element matches with key element then the search is completed
Otherwise the key element may be in upper half or lower half
The search progress with the upper half if the key element is less than middle element
The search progress with the lower half if the key element is greater than middle
element
Algorithm for binary search
Variables used
A[ ] array of n elements
Key element to find
Low variable for lower limit
High variable for higher limit
Mid middle value
17

f=0 Boolean value


1. // initialization of values //
Low 1;
HIGH N;
FLAG 0;
2. // perform search//
While(LOW<=HIGH)
MID (LOW + HIGH)/2;
If (KEY== a[MID])
{
F=1;
Break;
}
Else
If(key<a[MID];
HIGH = MID -1;
Else
If (key>a[MID])
LOW = MID +1;
3. // successful search//
If (f==1)
Print(element found);
Else
Print(element not found);
End if
Return
END BINARY SEARCH();
PROGRAM FOR BINARY SEARCH:

#include<stdio.h>
main()
{
int i,j,n,a[50],f=0,temp,key,low,high,mid;
printf("enter the value of n");
scanf("%d",&n);
printf("enter the elements in array");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1; i<=n-1; i++)
{
for(j=i+1; j<=n; j++)
{
if(a[i]>a[j])
{
temp = a[i];
18

a[i] = a[j];
a[j] = temp;
}
}
}
printf("\n enter the element to search");
scanf("%d", &key);
low =1;
high=n;
while(low<=high)
{
mid = (low+high)/2;
if(a[mid]==key)
{
f=1;
break;
}
else
if(key<a[mid])
high=mid-1;
else
if(key>a[mid])
low=mid+1;
}
if(f==1)
printf("element found at location ", );
else
printf("element not found");
getch();
}
4. What is sorting write any 4 sorting techniques?
A) The process of arranging elements in ascending or descending order is called
sorting.
Bubble sort
Quick sort
Heap sort
Merge sort

19

Important Questions
UNIT-I
1.

Define recursion? Explain design methodology and implementation of recursive


algorithms.

2.

Write a Recursive program to print Fibonacci series

3.

write a Recursive program to Solve Towers of Hanoi and explain.

4.

Explain about Fibonacci search in detail

5.

Explain about binary search and write algorithm for IT.What are the prerequisite for
binary search?

6.

Develop an algorithm for binary search. Validate the algorithm with suitable example.

7.

Explain about linear search and with an example trace the algorithm.

8.

Explain insertion algorithm.

9.

State and explain algorithm to perform heap sort.

10.

Trace the quick sort algorithm for the following list of numbers:90,77,60,99,55,88,62

11.

Analyze the complexity of quick sort algorithm.

12.

Write and explain quick sort algorithm. What is its time complexity.

13.

What is sorting? explain about radix sort with an example.

14.

Explain any one external sorting with an example.

UNIT-II
1.

Explain the ADT operations for array implementation stack.

2.

Write a program for reversing the list using stack.

3.

Explain infix to post fix conversion using stack with an example.

4.

Write the implementation of queue.

5.

Write about representation queue using array.

6.

State and explain queue operations using arrays.

7.

List the applications of stack and queues.

8.

Explain the priority queue implementation. Write the necessary algorithms.

UNIT-III
1.

State and explain different operations on single linked list.

2.

Design an algorithm to reverse the linked list. Trace your algorithm with an example.

20

3.

Using linked list, formulate separate routines to create an empty stack and to push an
element on to a stack.

4.

Write an algorithm to concatenate two singly linked list.

5.

Write applications of single linked list to represent polynomial expressions.

6.

Write advantage and disadvantages of linked list.

7.

give the advantages and disadvantages of circular and double linked list.

21

You might also like