Paper 1 - Data Structures Using C': Chapter - 1 - Introduction
Paper 1 - Data Structures Using C': Chapter - 1 - Introduction
Paper 1 - Data Structures Using C': Chapter - 1 - Introduction
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;
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
PUSH()
S
an array of stack
TOP
stack pointer
an array of stack
TOP
stack pointer
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
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
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
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
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
DELETION (QUEUE)
F front pointer of the queue
10
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
I)
II)
ex: D B E A F C G
III)
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
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
NULL
DATA
A
NULL
RPTR
NULL
NULL
Using this method adding, deleting and traversing tree is very easy and comfort.
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
#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
#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.
2.
3.
4.
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.
9.
10.
Trace the quick sort algorithm for the following list of numbers:90,77,60,99,55,88,62
11.
12.
Write and explain quick sort algorithm. What is its time complexity.
13.
14.
UNIT-II
1.
2.
3.
4.
5.
6.
7.
8.
UNIT-III
1.
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.
5.
6.
7.
give the advantages and disadvantages of circular and double linked list.
21