0% found this document useful (0 votes)
7 views

DS Chapter 1 and 2 Notes

Uploaded by

mayekarsai332
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DS Chapter 1 and 2 Notes

Uploaded by

mayekarsai332
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Q.1] Explain the concept of Data Structure with example?

→ A data structure is an arrangement of data, either in the computer’s memory


or on the disk storage. Some common examples of data structure include array,
linked list, queues, sack, binary tree and hash table.

Q.2] Explain different type of data structure.


→ The classification of data structure is given below:

Q.3] Differentiate between linear and non-linear data structure.



Linear Data Structure Non-linear Data structure
The data items are arranged in The items are arranged in non-
sequential order, one after the other. sequential order (hierarchical
manner).
All items are present on the single The data items are present at
layer. different layer.
It is easy to implement. It is complex to implement.
The linear relationship is present Data element have a hierarchical
between data element. relationship.
The time complexity increases with Time complexity remine the same.
the data size.
Example: Array, stack, Queue. Example: Tree, Graph, Map

Q.4] Define ADT (Abstract Data Type) with example.


→ i) ADT :- ADT means Abstract data type used for defining the data structure
along with set of operation that may or may not be performed on that data
structure
ii) The aim of ADT is to help the programmer understand the structure for which
ADT is being written.
iii) Depending on the magnitude and requirements of the system to be design,
we can add or reduce the number of operations to be include in the operator
definition. ADT’s are not unique for the different data structure.
iv) Example – Represents a rational number as an ADT.

Q.5] Explain the concept of recursion with example.


→ When a function call itself is called as recursive function. Whenever we use
recursive function, we have to define the stopping condition so that unlimited
function call inside function is avoided.
Example:
#include<stdio.h>
#include<conio.h>
Int factorial(int x);
Void main()
{
int n,fact;
clrscr();printf(“Enter any number”);scanf(“%d”,&n);
fact=factorial(n);
printf(“Factorial = &d”,fact);getch();
}
int factorial(int x)
{
if(x==0)
{return(1);}
else
{return(x*factorial(x-1));}
}

Q.6] Explain concept of stack, what are the various operation performed on the
data structure.

― Stack : the stack is an organisation that uses processing Last-In-
First-Out (LIFO). The data which is added at the end is always
removed first for processing.
1) Push() : Insertion/addition a value at the stack top.
2) Pop() : Deleting a value from the stack top.
3) Peep() : It returns the value from the stack top but does not
delete the value.
4) isEmpty() : It returns output as there if the stack is empty, else
returns false.
5) isFull() : It returns output as true if the stack is full, else it returns
false.
― There are two ways for memory allocation for the stack : Static
and Dynamic
1) Static memory allocation is done with the use of array.
2) Dynamic memory allocation is done with the use of linked
list.

Q.7] List and explain various operation performed on data structure.


→ The data which is stored in our data structure are processed by
some set of operation.
1) Insertion ― Add new data in data structure.
2) Deleting ― Remove the data from the data structure.
3) Sorting ― Arrange the data in increasing or decreasing order.
4) Searching ― Find the location of detain data structure.
5) Merging ― Combining the data of two different sorted files into
single stored file.
6) Traversing ― Accessing each data exactly one in the data
structure so that each data item is traversed or visited.

Q.8] Write down the ADT for the stack. Write down program to implement
following operation on a stack using an array. 1)push 2)pop 3)peep 4)display
→ A]
Abstract typedef < eltype > stack(s)
Abstract empty (s)
Stack < eltype > s
Postcondition empty == true
If (length(s)==0)
Abstract full (s)
Stack < eltype > s
Precondition empty == false
Postcondition = true, If (length (s) == max capacity of the storage)
Abstract eltype pop (s)
Stack (eltype) s
Precondition empty (s) = false
Postcondition pop = first (s)
Abstract eltype push (x,s)
Stack (eltype) s
eltype x
postcondition: s = s + < x >
B] Program:
#include<stdio.h>
#include<conio.h>
#define MAX 100
int TOP=-1;
int STACK[MAX];
void push(int val);
void pop();
void peep();
void display();
void main()
{
int choice,a;
clrscr();
do
{
printf("Enter your choice \n");printf("1.push \t 2.pop \t 3.peep \t 4.display \n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter new element");scanf("%d",&a);push(a);break;
case 2: pop();break;
case 3: peep();break;
case 4: display();break;
default : printf("It is wrong choice");
}
}while(choice!=5);
getch();
}
void push(int val)
{
if(TOP==MAX-1)
{printf("Overflow Condition \n");}
else
{TOP++;STACK[TOP]=val;printf("Elemented is inserted \n");}
}
void pop()
{
int val;
if(TOP==-1)
{printf("Underflow Condition \n");}
else
{val=STACK[TOP];TOP--;printf("Poped Element is %d \n",val);}
}
void peep()
{
int val;
if(TOP==-1)
{printf("Underflow Condition \n");}
else
{val=STACK[TOP];printf("Peeped element is %d \n",val);}
}
void display()
{
int val,i;
if(TOP==-1)
{printf("It is empty stack \n");}
else
{
for(i=TOP;i>=0;i--)
{printf("%d \n",STACK[i]);}
}
}

Q.9] Define types of expression with an example.



1. Infix Expression
An expression where the operators are placed in the middle of
the operands is called as an infix expression.
Example: 2+3*5
2. Post Expression
An expression where the operators are placed after the operand
is called as a postfix expression.
Example: 235*+
3. Prefix Expression
An expression where the operators are placed before the
operand is called as a prefix expression.
Example: +2*35

Q.10] Explain the concept of queue with its ADT.


→ Abstract typedef <eltype> queue(s)
Abstract empty (s)
queue <eltype> s
post condition empty =true
if (Length(s)=0)
abstract eltype remove (s)
queue (eltype) s
precondition empty (s) = False
postcondition remove = Front(s)
abstract eltype insert ( x, s )
queue (eltype) s
eltype x

Q.11] Write down program to perform operation of linear queue with array.
→ Program:
#include<stdio.h>
#include<conio.h>
#define MAX 100
int FRONT=-1; int REAR=-1;
int Queue[MAX];
void insert(int val);
void remove1();
void display();
void main()
{
int choice,a;
clrscr();
do
{
printf("Enter your choice \n");
printf("1.insert \t2.remove \t3.display \n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter new element \n");
scanf("%d",&a);
insert(a);
break;
case 2: remove1();
break;
case 3: display();
break;
default : printf("It is wrong choice \n");
}
}while(choice!=4);
getch();
}
void insert(int val)
{
if(REAR==MAX-1)
{printf("Queue is full \n");}
else if(FRONT==-1&&REAR==-1)
{FRONT=REAR=0;Queue[REAR]=val;}
else
{REAR++;Queue[REAR]=val;}
}
void remove1()
{
int val;
if(REAR==-1&&FRONT==-1)
{printf("Queue is empty \n");}
else if(REAR==FRONT)
{val=Queue[FRONT];FRONT=REAR=-1;}
else
{val=Queue[FRONT];FRONT++;}
printf("Deleted element is %d \n",val);
}
void display()
{
int i;
if(REAR==-1&&FRONT==-1)
{printf("Queue is empty \n");}
else
{
for(i=FRONT;i<=REAR;i++)
{printf("%d\n",Queue[i]);}
}
}

Q.12] Write down program to perform operation of circular queue.


Q.13] Explain the concept of Circular Queue.


→ Queue: In the case of linear queue, it is possible that the queue might be
reported as full even when a large chunk of its memory is still free.
A linear queue does not provide an option of reusing the memory
Which was used earlier.
i) Insert operation on Queue
Step 1: If COUNT=SIZE
PRINT”QUEUE OVERFLOW ”
Go to the step 11
Step 2: If COUNT!=SIZE&&REAR==SIZE-1
Step 3: SET REAR=0
Step 4: SET QUEUE[REAR]=VALUE
Step 5: SET COUNT=COUNT+1
Go to the step 11
Step 6: SET REAR=REAR+1
Step 7: SET QUEUE[REAR]=VALUE
Step 8: SET COUNT=COUNT+1
Step 9: If FRONT=-1
Step 10: SET FRONT=FRONT+1
ii) Delete operation on queue
Step 1: If COUNT=0
PRINT”QUEUE UNDERFLOW”
Go to step 7
Step 2: If COUNT=1
Step 3: SET FRONT=-1,REAR=-1,COUNT=0,
Go to step 7
Step 4: If FRONT=SIZE=-1
Step 5: SET FRONT=0, COUNT=-1
Go to the step 7
Step 6: SET FRONT=FRONT+1,COUNT=COUNT-1
Step 7: END
iii) Delete operation on queue
Step 1: If COUNT=0
PRINT”QUEUE OVERFLOW”
Step 2: If FORNT<=REAR
Step 3: FOR(i=FRONT TILL i<=REAR)
PRINT QUEUE[i]
Step 4: FOR(i=FORNT TILL I<=REAR)
PRINT QUEUE[i]
Step 5: FOR(i=FRONT TILL i<=REAR)
PRINT QUEUE[i]
Step 6: END

Q.14] Differentiate the linear and circular queue.


Q.15] Explain the concept of priority queue with example.



1) A queue in which element are organize into groups according to priority
and processed such that highest priority element are output first are
called priority queue.
 An element with higher priority is processed before an element with a
lower priority.
 Two elements with the same priority are processed on a first-come-first-
served (FCFS) basis.
2) A priority queue can be through of as a modified queue in which when
an element has to be taken off the queue, the one with higher-priority
is removed first. The priority of element can be based on various
factors. Priority queue are widely used in operating systems to execute
the highest priority order process first. The priority of the process may
be set based on the CPU time it requires to get executed completely.
For example, if there are three processes, where the first process needs
5 nanoseconds, the second process needs 4 nanoseconds and the third
process needs 7 nanoseconds, then the second process will have the
highest priority and will thus be the first to be executed. Another factor
is the importance of one process over another. In case we have to run
two processes, first one is online order booking and second is printing
stock details, then online booking must be executed first.
3) Example –
a) size() : Return the number of elements in P.
b) isEmpty() :Return true if P has no entry.

You might also like