SlideShare a Scribd company logo
1 of 50
Download to read offline
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 1111////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Section – A 2 Marks Questions
[QUESTIONS 1 TO 18] [PAGE 1 TO 4]
Q1. What is data structure?
Ans. The logical or mathematical model of a particular organization of data is called a data
structure.
Q2. What are two main types of data structure?
Ans. 1.Linear Data Structure:
A data structure is said be linear if its elements are stored sequentially in the
form of a linear list e.g. arrays, stacks, linked lists.
2. Non-linear Data structure:
A data structure is said to be non-linear if its elements are not stored
sequentially in the form of linear list e.g. trees & graphs.
Q3. Write various operations performed on data structure.
Ans. The various operations are: -
1. Traversing
2. Inserting
3. Deleting
4. Merging
5. Searching
6. Sorting
7. Updating
8. Creating
Q4. What is an array? How array is stored in memory.
Ans. Array is collection of homogeneous data elements. They are stored in continuous
memory locations. They are accessed by using index value.
int a[5];
12 4 45 67 68
1000 1002 1004 1006 1008
1000 is the location of the first element of the array. As the int data type takes two
bytes of the memory so the next element is stored at 1002 and so on.
Q5. What are pointer Arrays?
Ans. A pointer array is memory variable that use to store address of a variable. Pointer
arrays are use to store address of more then one variable.
e.g.
main()
{
int *a[3];
int x,y,z;
x=10;
y=20;
z=30;
a[0]=&x;
a[1]=&y;
a[2]=&z;
printf(“address of x=%u”,a[0]);
}
Q6. What is a linked list?
Ans. A linked list or one-way list is a linear collection of data elements, called nodes,
where the linear order is given by means of pointers.
Each node is divided into 2 parts. The first part contains the information of the
element & the second part called the link field, contains the address of the next node
in the list.
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 2222////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q7. Write syntax to declare a node of linked list.
Ans. Singly linked list
struct node
{
int info;
node *link;
};
Doubly linked list
Struct node
{
int info;
node *next,*prev;
};
Q8. What are different types of linked list?
Ans. The various types of linked list are:-
1. Singly
2. Doubly
3. Circular
4. Generalized
Q9. What are Reversible linked list?
Ans. A reversible linked list enable you to visit a node, Which is previously visited by the
pointer, we can visit any node, which is previously visited, By providing address of
the node.
Q10. What are operations performed on linked list?
Ans. 1. Insertion
2. Deletion
3. Searching
4. Traversing
Q11. Write an algorithm to delete a node from linked list from beginning.
Ans. DELFIRST (LINK, START).
1. [Underflow?] If START = NULL
then: Write underflow and Exit
2. Set START=LINK [START]
3. Exit.
Q12. Write an algorithm to Delete a node from linked list from END.
Ans. DELEND(LINK, START,TEMP).
1 [Underflow ] If START = NULL
then: Write UNDERFLOW and Exit
2 Set TEMP = START
3 WHLE LINK[TEMP]!=NULL
TEMP=LINK[TEMP] END LOOP
4 Set TEMP=NULL
5 Exit.
Q13. Write a function to insert in a linked list.
Ans. void insert(struct node **s)
{
struct node *p,*l;
int cc;
p=(struct node*)malloc(sizeof(struct node));
if(*s==NULL)
{
printf(“enter element”);
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 3333////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
scanf(“%d”,&p->nfo);
p->link=NULL;
*s=p;
}
else
{
printf(“nEnter 1 for insert at beg”);
printf(“nEnter 2 for insert at end”);
scanf(“%d”,&cc);
if(cc==1)
{
printf(“Enter element”);
scanf(“%d”,&p->info);
p->link=*s;
*s=p;
}
if(cc==2)
{
printf(“Enter element”);
scanf(“%d”,&p->info);
p->link=NULL;
l=*s;
while(l->link!=NULL)
{
l=l->link;
}
l->link=p;
}
}
}
Q14 Write a function to delete from a linked list.
Ans. void del(node **s)
{
node *l;
l=*s;
if(l==NULL)
printf("no element");
else
{
printf(“dele ele=%d”,l->info);
*s=l->link;
}
}
Q15 Write a function to print elements from a linked list.
Ans. void print(node *s)
{
printf(“nElement are:->”);
while(s!=NULL)
{
printf(“%d”,s->info);
s=s->link;
}
}
CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.
# 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala
CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 4444////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q16 Define Header linked list.
Ans. A header linked list is a linked list which always contains a special node called the
header node, at the beginning of the list.
TYPES
1.Gounded header list:-In this last node contain the null pointer.
2.Circular header list:- In this last node points back to the header ndode.
Q17. Differentiate between local & global variables.
Ans. Local variables: are those variables, which are accessible to a program within a
particulars scope. These are accessible to the code within the limited scope. They
are generally used when recursion is required in programming.
Global variables: a certain variable are those variables, which are declared outside
the scope. These variables are available throughout the program. Global variables
are used when you want to reverse.
Q18. Differentiate between structured & modular programming.
Ans. Structured programming considers the problem as a single structure & applies the
various conventional methods to solve that problem. On the other hand, modular
programming divides the main module into a number of different sub modules. Then
the main module refers to a number of subordinate modules
Section – A 5 Marks Questions
[QUESTIONS 1 TO 18] [PAGE 4 TO 15]
Q1. How to declare an array?
Ans. Typeofvariable variablename [size]
(1) An array must be declared since it is a type of variable.
(2) An array containing five elements all of which are integers can be declared as
follows:
int x[5];
(3) The name of the array can not be same as that of any other variable declared
within the function.
(4) The size of the array (the no of elements ) specified using the subscript notation.
(5) The subscript 5 indicates how many elements are to be allocated to array x
(6) The subscript used to declare an array are called dimension.
The elements of five element array in c are numbered starting with 0 not with 1.
E.g. Suppose we want to store nos. 56,57,58,90,3 then the assignment statement for
this should be as under
x[0]=56 // first no
x[1]=57 // second no
x[2]=58 // third no
x[3]=90 // fourth no
x[4]=3 //fifth no
Q2. Write a program to input and print numbers using array.
Ans. #include<stdio.h>
void main()
{
int i,s[10];
for(i=0;i<10;i++)
{
scanf("%d",&s[i]);
}
for(i=0;i<10;i++)
{
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 5555////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
printf("n %d",s[i]);
}
}
Q3. Write a program to input and print characters using array.
Ans. #include<stdio.h>
void main()
{
int I;
char s[10];
for(i=0;i<10;i++)
{
scanf("%c”,&s[i]);
}
for(i=0;i<10;i++)
{
printf("%c",s[i]);
}
}
OR
void main()
{
int I;
char s[10];
scanf("%s”,&s);
printf("%s",s);
}
Q4. Write a program to input marks in 4 subjects of 10 students and print.
Ans. #include<stdio.h>
main( )
{
int data [10][4];
int r,c;
clrscr( );
//to enter data
for(r=0;r<10;r++)
{
printf("enter the detail %d student",r+1);
for(c=0;c<4;c++)
{ printf("enter the marks in sub %d",c+1);
scanf("%d",&data[r][c]);
}
}
// to display data
for(r=0;r<10;r++)
{
printf("n detail of %d student",r+1);
for (c=0;c<4;c++)
{
printf("n the marks in sub %d Is Id",c+1,data[r][c]);
}
}
getch( );
}
CJ SOFTECH Walia Enclave Opp Punjabi UnivCJ SOFTECH Walia Enclave Opp Punjabi UnivCJ SOFTECH Walia Enclave Opp Punjabi UnivCJ SOFTECH Walia Enclave Opp Punjabi Univ
Patiala.Patiala.Patiala.Patiala.
# 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala
CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 6666////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q5. Create a program to print the transpose of a matrix.
Ans. void main()
{
int a[2][2],I,j;
for(I=0;I<2;I++)
{ printf(“enter the values for %d row of matrix”,I+1);
for(j=0;j<2;j++)
{
scanf(“%d”,&a[I][j]);
}
}
/* transposing the matrix */
for(I=0;I<2;I++)
{
for(j=0;j<2;j++)
{
printf(“t%d”,a[j][I]);
}
printf(“n”);
}
getch();
}
Q6. Write a program for multiplication if two 2-D array.
Ans.
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],I,j,k,r1,r1,r2,c1,c2;
printf( “Enter Rows of first matrix “);
scanf(“%d”,&r1);
printf( “Enter columns of first matrix”);
scanf(“%d”,&c1);
printf( “Enter columns of first matrix”);
scanf(“%d”,&r2);
printf( “Enter columns of first matrix”);
scanf(“%d”,&c2);
if (c2!=r2)
{
printf(“ Multiplication not possible “);
exit(0);
}
printf (“Enter first matrix”n);
for(I=0;I<r1;I++)
for(j=0;j<c2;j++)
{
Printf(“Enter array element “);
Scanf(“%d”,&a[I][j]);
}
printf(Enter second matrix “n)
for(I=0;I<r2;I++)
for(j=0;j<c2;j++)
{
Printf(“Enter array element “);
Scanf(“%d”,&a[I][j]);
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 7777////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
}
for (I=0;I<r2,I++)
for(j=0;j<c2;j++)
{
c[I][j]=0;
for(k=0;k<r2;k++)
c[I][j]+=a[I][k]*b[k][j];
}
}
printf (“Result of Multiplication cationn”);
for(I=0;I<r1;I++)
{
printf(“n”);
for(j=0;j<c2;j++)
printf(“t%d”,c[I][j]);
}}
Q7. Write a program for addition of two 2-D array.
Ans. #include<stdio.h>
void main()
{
int a[2][2],b[2][2],c[2][2];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
scanf("%d",&a[i][j]);
for( i=0;i<2;i++)
for( j=0;j<2;j++)
scanf("%d",&b[i][j]);
for( i=0;i<2;i++)
for( j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
for( i=0;i<2;i++)
{
for( j=0;j<2;j++)
printf("%d ",c[i][j]);
printf("n ");
}}
Q8. What do you mean by call by reference?
Ans. In this approach the addresses of the actual arguments are passed to the called
function. In this way processing is done on the actual variables.
#include<stdio.h>
int add(int*,int*);
void main()
{int a,b,c;
a=10;
b=20;
c=add(&a,&b);
printf("%d",c);
}
int add(int *x, int *y)
{
int z;
z=*x+*y;
return(z);
}
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 8888////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q9. Create a program to print the transpose of a matrix using function.
Ans.
void trans(int[ ]);
void main()
{
int a[2][2],I,j;
for(I=0;I<2;I++)
{ printf(“enter the values for %d row of matrix”,I+1);
for(j=0;j<2;j++)
{
scanf(“%d”,&a[I][j]);
}
}
trans(a);
}
void trans(int a[ ])
{
int I,,j;
for(I=0;I<2;I++)
{
for(j=0;j<2;j++)
{
printf(“t%d”,a[j][I]);
}
printf(“n”);
}
getch();
}
Q10. What is a doubly linked list?
Ans. A singly linked list can move only in one direction in certain applications, it is
sometimes required that the list be traversed in either a forward or reverse direction.
This property of a linked list implies that each mode must contain two link fields
instead of one. The links are used to denote the predecessor & successor of a mode.
The link devoting the predecessor & of a mode is called the left link & that devoting
its successor is called the right link. A list containing this type of mode is called a
doubly linked list.
Q11. Discuss the advantages & disadvantages of circular linked list.
Ans. Circular lists have certain advantages over singly linked list. The first advantage is
that in a circular list every mode is accessible from a given mode.
A second advantage is concerned with the deletion operation. As we know that in
order to delete a mode in a singly linked list, it is necessary to give the address of the
first mode of the list. This is necessary because we need to find the predecessor of
the mode to be deleted To find the predecessor of a search is required starting from
the first mode of the list. In the case of circular list, this requirement does not exist
since the search for the processor of the mode to be deleted can be initiated from
that mode itself.
However, there is a disadvantage in using circular lists. It is possible to get into an
infinite loop. In processing a circular list, it is important that we are able to detect the
end of the list.
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 9999////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q12. Write an algorithm to create a linked list & insert an item into a linked list in
beginning.
Ans. INFIRST (INFO, LINK, START, AVAIL, ITEM). This algorithm inserts ITEM as the
first node in the list.
1. [Overflow?] If AVAIL = NULL
then: Write overflow and Exit
2. Set NEW: = AVAIL
3. Set INFO [NEW]: = ITEM [Copies new data into new node.
4. Set LINK [NEW]: =START [New mode points to original first node].
5. Set START: = NEW [Changes START so it points to the new node].
6. Exit.
Q13. Write an algorithm to insert an item into a linked list in end.
Ans. INEND (INFO,TEMP, LINK, START, AVAIL, ITEM). This algorithm inserts ITEM as
the END node in the list.
1. [Overflow?] If AVAIL = NULL
then: Write overflow and Exit
2. Set NEW: = AVAIL
3. Set INFO [NEW]: = ITEM [Copies new data into new node.
4. Set LINK [NEW]: =NULL
5. TEMP=START
6. WHILE TEMP!=NULL
TEMP=LINK[TEMP]
END LOOP
7. Set TEMP = NEW .
8. Exit.
Q14. Write a program to pass structure in function using pointer.
Ans. #include<stdio.h>
#include<conio.h>
struct emp
{
char empname[34];
int empno;
int bs;
int bonus;
float total;
};
void print(emp*);
void main()
{
int i;
clrscr();
emp k;
printf("enter");
scanf("%s%d%d",&k.empname,&k.empno,&k.bs);
print(&k);
}
void print(emp *k)
{
k->bonus=k->bs/10 ;
k->total=(float)k->bs+k->bonus;
printf("nemployee name is t %s sal=t%f",k->empname,k->total);
}
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 10101010////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q15. Write a program to pass array in function using pointer.
Ans. #include<stdio.h>
#include<conio.h>
void print(int*);
void main()
{
int a[5],i;
clrscr();
printf("enter numbers");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
print(&a[0]);
getch();
}
void print(int *p)
{
int i,g;
g=*p;
for(i=0;i<5;i++)
{
if(g<*p)
{
g=*p;
}
p++;
}
printf("%d ",g);
}
Q16. Write a program to implement linked list.
Ans. //linked list
#include<conio.h>
#include<stdio.h>
struct node
{
int info;
struct node *link;
};
void main()
{
struct node *start,*p,*l;
int i,j,a=0,c,cc;
clrscr();
start=NULL;
printf("link list is created");
while(c!=4)
{
printf("nenter 1 for insert");
printf("n Enter 2 for delete");
printf("n Enter 3 for print");
printf("nEnter 4 for exit");
scanf("%d",&c);
if(c==4)
CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.
# 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala
CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 11111111////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
{
break;
}
if(c==1)
{
p=(struct node*)malloc(sizeof(struct node));
if(start==NULL)
{
printf("enter element");
scanf("%d",&p->info);
p->link=NULL;
start=p;
}
else
{
printf("nEnter 1 for insert at beg");
printf("nEnter 2 for insert at end");
scanf("%d",&cc);
if(cc==1)
{
printf("Enter element");
scanf("%d",&p->info);
p->link=start;
start=p;
}
if(cc==2)
{
printf("Enter element");
scanf("%d",&p->info);
p->link=NULL;
l->link=start;
while(l->link!=NULL)
{
l=l->link;
}
l->link=p;
}
}
}
if(c==2)
{
if(start==NULL)
printf("no element");
else
{
printf("dele ele=%d",start->info);
start=start->link;
}
}
if(c==3)
{
printf("nElement are:->");
l=start;
while(l!=NULL)
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 12121212////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
{
printf("%d",l->info);
l=l->link;
}
}
}
getch();
}
Q17. Write a program to implement linked list using functions.
Ans. //linked list using functions
#include<conio.h>
#include<stdio.h>
struct node
{
int info;
struct node *link;
};
void insert(struct node**);
void del(struct node**);
void print(struct node*);
void main()
{
struct node *start,*p,*l;
int i,j,a=0,c,cc;
clrscr();
start=NULL;
printf("link list is created");
while(c!=4)
{
printf("nenter 1 for insert");
printf("n Enter 2 for delete");
printf("n Enter 3 for print");
printf("nEnter 4 for exit");
scanf("%d",&c);
if(c==4)
{
break;
}
if(c==1)
{
insert(&start);
}
if(c==2)
{
del(&start);
}
if(c==3)
{
print(start);
}
}
getch();
}
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 13131313////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
void insert(struct node **s)
{
struct node *p,*l;
int cc;
p=(struct node*)malloc(sizeof(struct node));
if(*s==NULL)
{
printf("enter element");
scanf("%d",&p->info);
p->link=NULL;
*s=p;
}
else
{
printf("nEnter 1 for insert at beg");
printf("nEnter 2 for insert at end");
scanf("%d",&cc);
if(cc==1)
{
printf("Enter element");
scanf("%d",&p->info);
p->link=*s;
*s=p;
}
if(cc==2)
{
printf("Enter element");
scanf("%d",&p->info);
p->link=NULL;
l=*s;
while(l->link!=NULL)
{
l=l->link;
}
l->link=p;
}
}
}
void del(struct node **s)
{
struct node *l;
l=*s;
if(l==NULL)
printf("no element");
else
{
printf("dele ele=%d",l->info);
*s=l->link;
}
}
void print(struct node *s)
{
printf("nElement are:->");
while(s!=NULL)
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 14141414////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
{
printf("%d",s->info);
s=s->link;
}
}
Q18. Write a program to implement doubly (two–way) linked list.
Ans. //doubly linked list
#include<conio.h>
#include<stdio.h>
struct node
{
int info;
struct node *link;
struct node *prev;
};
void main()
{
struct node *start,*p,*l,*t;
int i,j,a=0,c,cc;
clrscr();
start=NULL;
printf("link list is created");
while(c!=4)
{
printf("nenter 1 for insert");
printf("n Enter 2 for delete");
printf("n Enter 3 for print");
printf("nEnter 4 for exit");
scanf("%d",&c);
if(c==4)
{
break;
}
if(c==1)
{
p=(struct node*)malloc(sizeof(struct node));
if(start==NULL)
{
printf("enter element");
scanf("%d",&p->info);
p->link=NULL;
p->prev=NULL;
start=p;
}
else
{
printf("Enter element");
scanf("%d",&p->info);
p->link=start;
p->prev=NULL;
start->prev=p;
start=p;
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 15151515////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
}
}
if(c==2)
{
if(start==NULL)
printf("no element");
else
{
printf("dele ele=%d",start->info);
start=start->link;
start->prev=NULL;
}
}
if(c==3)
{
printf("nElement are:->");
l=start;
while(l!=NULL)
{
t=l;
printf("%d",l->info);
l=l->link;
}
printf("nElementin rev order are:->");
while(t!=NULL)
{
printf(" %d",t->info);
t=t->prev;
}
}
}
getch();
}
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 16161616////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Section – B 2 Marks Questions
[QUESTIONS 1 TO 18] [PAGE 16 TO 18]
Q1. Define Stack.
Ans. It is also called Last-In-First-Out system. Is a linear list in which insertions and
deletions can take place only at one end, called the top.
Q2. Define PUSH , POP.
Ans. Push:- It is the operation to insert a element in the stack.
Pop :- It is the operation to delete a element from the stack.
Q3. Name Operations Performed On Stacks.
Ans.
• Initialization of Stack.
• Push Operation.
• Pop Operation.
• Underflow Operation.
• Overflow Operation.
Q4. Write a Algorithm of Underflow Operation.
Ans. Stack Empty (S)
1. if top[S] = -1
2. then return true
3. else return false
Q5 Write a Algorithm of Overflow Operation.
Ans. StackFull(S)
1. if top[s] is equal to last available location
2. then return true
3. else return false
Q6. Write a Algorithm of Push operation.
Ans. Push (S[N],TOP)
1. If TOP=N Then
Write “over flow” and exit
End if
2. TOP=TOP+1
3. Read S[TOP]
4. Exit
Q7. Write a Algorithm of POP Operation
Ans. POP(S[N])
1. If TOP=-1 Then
Write “ Underflow” and Exit
End if
2. TOP=TOP -1
3. Exit
Q8. Write Applications Of Stacks.
Ans.
• Recursion Call
• Interrupt Handling
• Traversing in Graph (Depth First Search)
• Infix to postfix
• Postfix evaluation
Q9. What is time & space complexity?
Ans. Time complexity is related to the performance requirements of an algorithm.
Space complexity is the amount of memory it needs to run to completion.
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 17171717////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q10. Write difference between stack and queue.
Ans. Queues
Queues are FIFO linear structures Queues are used for a variety of purposes and
implemented in multiple ways.
Stacks
Stacks are LIFO structures. Stacks cannot be implemented in a variety of ways.
Q11. What is Queue?
Ans. Queue is also called First-In First Out system, it is a linear list in which insertions take
place at one end known as rear and deletions can take place from the other end
known as front.
Q12. Name different types of Queue.
Ans.
• Simple Queue
• Circular Queue
• Priority Queue
Q13. Write Various operations performed on Queue.
Ans. Operations Performed On Queues:
• Initialization of Queue
• Insertion of element
• Deletion of element
• Overflow operation
• Underflow operation
Q14. Write algorithm to Insert in a queue.
Ans. This operation increments the rear by one and stores the element at new position of
rear.
Insert(Q[n],x)
1. If rear=n
Write “overflow and Exit.
2. If rear=-1
Rear=0
Front=0
Q [rear] = x and Exit
3. rear = rear+ 1
4. Q [rear] = x
5. Exit
Q15. Write algorithm to delete a element from Queue.
Ans. This operation will remove the first element from the queue Q.
delete(Q[n],x)
1. If front=0
Write “under flow” and Exit.
2. If rear=front
Rear=-1
Front=-1
Exit
3. front=front + 1
4. Exit
Q16. Define Priority Queue.
Ans. A priority queue is a collection of elements such that each elements has been
assigned a priority and such that the order in which elements are deleted and
processed comes from the following rules:
1. An element of higher priority is processed before any element of lower
priority.
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 18181818////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
2. two elements with the same priority are processed according to the order in
which they were added to the queue.
Q17. Write Applications of Queues.
Ans. One of the important areas to which queues can be applied is that of simulation. It is
the process of forming an abstract model from a real situation in order to understand
the impact of modifications & the effect of introducing various strategies on the
situation. The major advantage of simulation is that it allows experimentation without
modifying the real situation. Areas such as military operations are safer to simulate.
Q18. What is dequeue?
Ans. A dequeue also known as double ended queue. It is a link of queues in which
elements can be added or removed from at either end but not in the middle.
Section – B 5 Marks Questions
[QUESTIONS 1 TO 16] [PAGE 18 TO 31]
Q1. Explain Prefix Expression With Examples.
Ans. Infix expression can be written in prefix form as + A B. Prefix form is also known as
Polish Notation.
To convert infix to prefix 3 levels of precedence are used
Highest : Exponentiation (^)
Next Highest : Multiplication (*) & Division (/)
Lowest : Addition (+) & Subtraction ( - )
Eg 1
(A + B) * C
(+ A B) * C
* + A B C
Eg2.
(A + B) / (C - D)
(+ AB) / (-C D)
/ + A B – C D
Q2. Explain of Postfix Expression with Examples.
Ans. Infix expression can be written in postfix form as A B +.Postfix expression also known
as Reverse polish notation.
To convert infix to postfix same level of precedence are used.
Eg1.
(A + B) * C
( A + B +)* C
A B + C*
Eg2.
(A + B) / (C – D)
( A B +) / ( C D -)
A B + C D - /
Q3. Write an algorithm for transforming infix expression into postfix expression
with example.
Ans. POLISH (Q, P)
Suppose Q is an arithmetic expression written in infix mutation. This algorithm finds
the equivalent postfix expression P.
1. Push “(“ onto STACK & add) “ to the end of Q
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 19191919////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
2. Scan Q from left to right & repeat steps 3 to 6 for each element of Q until the
stack is empty.
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator Q is encountered, then
a) Repeatedly pop from STACK & add to P each operator, which has the
same precedence as or higher precedence than Q.
b) Add Q to STACK
[End of If structure]
6. If a right parenthesis is encountered
a) Repeatedly pop from STACK & add to P each operator until a left
parenthesis is encountered.
b) Remove the left parenthesis [Do not add the left parenthesis to P]
[End of If structure]
[End of step 2 Loop]
7. Exit
Q4. Write a program to implement a stack.
Ans. //stack
#include<stdio.h>
#include<conio.h>
void main()
{
int s[5],t=-1,c,i;
clrscr();
while(c!=4)
{
printf(“enter 1 for push n 2 for
pop”);
printf(“enter 3 for print 4 for exit”);
scanf(“%d”,&c);
if(c==1)
{
if(t==4)
{
printf(“overflow”);
}
else
{
t=t+1;
printf(“enter ele”);
scanf(“%d”,&s[t]);
}
}
if(c==2)
{
if(t==-1)
{
printf(“underflow”);
}
else
{
printf(“element deleted”);
t--;
}
}
if(c==3)
{
for(i=0;i<=t;i++)
{
printf(“ %d”,s[I]);
}
}
}
getch();
}
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 20202020////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q5. Write a program to implement a stack using functions.
Ans.
#include<stdio.h>
#include<conio.h>
struct stack
{
int s[5];
int t;
};
void push(stack*);
void pop(stack*);
void print(stack*);
void main()
{
stack i;
int c;
clrscr();
i.t=-1;
do
{
printf(“enter 1 for push n 2 for
pop”);
printf(“enter 3 for print 4 for exit”);
scanf(“%d”,&c);
if(c==1)
{
if(i.t==4)
{
printf(“overflow ”);
}
else
{
push(&i);
}
}
if(c==2)
{
if(i.t==-1)
{
printf(“ underflow”);
}
else
{
pop(&i);
}
}
if(c==3)
{
print(&i);
}
} while(c<4);
getch();
}
void push(stack *p)
{
p->t++;
printf(“enter element ”);
scanf(“%d”,&p->s[p->t]);
}
void pop(stack *p)
{
printf(“deleted element=%d ”
,p->s[p->t]);
p->t--;
}
void print(stack *p)
{
int i;
for (i=0;i<=p->t;i++)
{
printf(“ %d ”,p->s[I]);
}
}
Q6. Write a program to implement linked list as stack.
Ans. #include<conio.h>
#include<stdio.h>
struct node
{
int info;
struct node *link;
};
void main()
{
struct node *start,*p,*l;
int i,j,a=0,c,cc;
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 21212121////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
clrscr();
start=NULL;
printf("link list is created");
while(c!=4)
{
printf("nEnter 1 for push");
printf("nEnter 2 for pop");
printf("nEnter 3 for print");
printf("nEnter 4 for exit");
scanf("%d",&c);
if(c==4)
{
break;
}
if(c==1)
{
p=(struct node*)malloc(sizeof(struct node));
if(start==NULL)
{
printf("enter element");
scanf("%d",&p->info);
p->link=NULL;
start=p;
}
else
{
printf("enter elements");
scanf("%d",&p->info);
p->link=start;
start=p;
}
}
if(c==2)
{
if(start==NULL)
printf("underflow");
else
{
printf("element deleted");
start=start->link;
}}
if(c==3)
{
printf("elements are");
l=start;
while(l!=NULL)
{
printf("%d",l->info);
l=l->link;
}
}
}
getch();
}
CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.
# 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala
CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 22222222////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q7. Explain Circular Queue.
Ans. This type of queue removes the total shifting of elements in the queue. In Circular
Queue there is neither a starting point nor an ending point i.e. first available location
is considered as next to last available location. Working of Circular Queue is given as
following:
Q8. Define Underflow and Overflow operations.
Ans. Underflow Operation
This algorithm is called before deleting an element and return true if queue is
empty else return false
Queue_Empty(Q)
1.if rear [Q] = -1
2.then return true
3.else retun false
Overflow Operation
This algorithm is called before inserting an element to check whether a queue
is full or not. This algorithm will return true if queue is full else return false.
1.if front points to next available location of rear
2.then return true
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 23232323////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
3.else return false
Q8. Write a program to implement Queue.
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int q[5],f=-1,r=-1,i,c;
clrscr();
do
{
printf("n1 for ins n 2 for deln 3
for print n 4 for exn");
scanf("%d",&c);
if(c==1)
{
if(r==4)
{
printf("nover flow");
}
else if(f==-1)
{
f=r=0;
printf("enter ele ");
scanf("%d",&q[r]);
}
else
{
r=r+1;
printf("enter ele ");
scanf("%d",&q[r]);
}
}
if(c==2)
{
if(f==-1)
{
printf("under flow");
}
else if(f==r)
{
f=r=-1;
}
else
{
f=f+1;
}
}
if(c==3)
{
if(f>-1)
{
for(i=f;i<=r;i++)
{
printf(" %d",q[i]);
}
}
}
}
while(c<4);
getch();
}
SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 24242424////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q9. Write a program to implement Queue using structure and functions.
Ans.
#include<stdio.h>
#include<conio.h>
struct queue
{
int s[5];
int r,f;
};
void ins(queue*);
void del(queue*);
void print(queue*);
void main()
{
queue i;
int c;
clrscr();
i.r=-1;
i.f=-1;
do
{
scanf(“%d”,&c);
if(c==1)
{
if(i.r==4&&i.f==0||i.f==i.r+1)
{
printf(“overflow”);
}
else
{
ins(&i);
}
}
if(c==2)
{
if(i.f==-1)
{
printf(" u f");
}
else
{
del(&i);
}
}
if(c==3)
{
print(&i);
}
} while(c<4);
getch();
}
void ins(queue *p)
{
if(p->f==-1)
{
p->f=0;
p->r=0;
scanf(“%d”,&p->s[p->r]);
}
else if(p->r==4)
{
p->r=0;
scanf(“%d”,&p->s[p->r]) }
else
{
p->r++;
scanf(“%d”,&p->s[p->r]) }
}
void del(queue *p)
{
if(p->f==p->r)
{
p->f=-1;
p->r=-1;
}
else if(p->f==4)
{
p->f=0;
}
else
{
p->f=p->f+1;
}
}
void print(queue *p)
{
int i;
if(p->f>-1)
{
if(p->f<=p->r)
{
for(i=p->f;i<=p->r;i++)
printf(“%d”,p->s[i]);
}
else
{
for(i=p->f;i<=4;i++)
printf(“%d”,p->s[i])
for(i=0;i<=p->r;i++)
printf(“%d”,p->s[i])
}
}
}
SUBJECT: DATA STRUCTURE 25/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q10. Write a program to implement Queue using linked list.
Ans. #include<conio.h>
#include<stdio.h>
struct node
{
int info;
struct node *link;
};
void main()
{
struct node *start,*p,*l;
int i,j,a=0,c,cc;
clrscr();
start=NULL;
printf("link list is created");
while(c!=4)
{
printf("nEnter 1 for insert");
printf("nEnter 2 for delete");
printf("nEnter 3 for print");
printf("nEnter 4 for exit");
scanf("%d",&c);
if(c==4)
{
break;
}
if(c==1)
{
p= (struct node*)malloc(sizeof(struct node));
if(start==NULL)
{
printf("enter element");
scanf("%d",&p->info);
p->link=NULL;
start=p;
}
else
{
printf("enter element");
scanf("%d",&p->info);
p->link=NULL;
l=start;
while(l->link!=NULL)
{
l=l->link;
}
l->link=p;
}
}
if(c==2)
{
printf("element deleted ");
start=start->link;
}
SUBJECT: DATA STRUCTURE 26/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
if(c==3)
{
l=start;
while(l!=NULL)
{
printf(" %d",l->info);
l=l->link;
}
}
}
getch();
}
Q11. Write a program to implement Circular Queue.
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int q[5],f=-1,r=-1,i,c;
clrscr();
do
{
printf("n1 for ins n 2 for deln 3 for print n 4 for exn");
scanf("%d",&c);
if(c==1)
{
if(r==4&&f==0||f==r+1)
{
printf("nover flow");
}
else if(f==-1)
{
f=r=0;
printf("enter ele ");
scanf("%d",&q[r]);
}
else
{
if(r<4)
{
r=r+1;
}
else
{
r=0;
}
printf("enter ele ");
scanf("%d",&q[r]);
}
}
if(c==2)
{
if(f==-1)
{
printf("under flow");
SUBJECT: DATA STRUCTURE 27/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
}
else if(f==r)
{
f=r=-1;
}
else
{
if(f<4)
{
f=f+1;
}
else
{
f=0;
}
}
}
if(c==3)
{
if(f>-1)
{
if(r>=f)
for(i=f;i<=r;i++)
{
printf(" %d",q[i]);
}
else
{
for(i=f;i<=4;i++)
{
printf(" %d",q[i]);
}
for(i=0;i<=r;i++)
{
printf(" %d",q[i]);
}
}
}
}
}
while(c<4);
getch();
}
Q12. Write an algorithm for transforming infix expression into postfix expression .
Ans. POLISH (Q, P)
Suppose Q is an arithmetic expression written in infix mutation. This algorithm finds
the equivalent postfix expression P.
1. Push “(“ onto STACK & add) “ to the end of Q
2. Scan Q from left to right & repeat steps 3 to 6 for each element of Q until the
stack is empty.
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator Ø is encountered, then
SUBJECT: DATA STRUCTURE 28/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
a. Repeatedly pop from STACK & add to P each operator, which has the
same precedence as or higher precedence than Ø.
b. Add Ø to STACK
[End of If structure]
6. If a right parenthesis is encountered
a. Repeatedly pop from STACK & add to P each operator until a left
parenthesis is encountered.
b. Remove the left parenthesis [Do not add the left parenthesis to P]
[End of If structure]
[End of step 2 Loop]
7. Exit
Q13. Write a program to convert infix to postfix.
Ans.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char ie[20];
char pe[20];
char s[20];
int i,t=0,k=0,j=0;
clrscr();
scanf("%s",&ie);
printf("noriginal exp=%s",ie);
i=strlen(ie);
ie[i]=NULL;
i=0;
while(ie[i]!=NULL)
{
if(ie[i]=='+'||ie[i]=='-'||ie[i]=='*'||ie[i]=='/')
{
if(t==0)
{
s[t]=ie[i];
t++;
}
else
{
if(ie[i]=='*'||ie[i]=='/')
{
s[t]=ie[i];
t++;
}
else
{
while(s[t-1]=='*'||s[t-1]=='/'||s[t-1]=='+'||s[t-1]=='-')
{
pe[k]=s[t-1];
k++;
t--;
}
s[t]=ie[i];
SUBJECT: DATA STRUCTURE 29/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
t++;
}
}
}
else
{
pe[k]=ie[i];
k++;
}
i++;
}
t--;
while(t!=-1)
{
pe[k++]=s[t--];
}
pe[k]='0';
printf("n post exp=%s",pe);
getch();
}
Q14. Write algorithm for Postfix Evaluation.
Ans. Following algorithm uses a stack to calculate postfix expression. Suppose P is an
arithmetic expression written in postfix notation. The following algorithm, which uses
a STACK to hold operands, evaluates P. This algorithm finds the VALUE of an
arithmetic expression P written in postfix notation.
1. Add a right parenthesis ‘)’ at the end of P.
2. Scan P from left to right and repeat steps 3 & 4 for each element P until the
‘)’ is encountered.
3. If an operand is encountered, then:
Push it into STACK
4. If an operator * is encountered, then:
a. Remove the two top elements of STACK, where A is the top element
and B is next-to top element.
b. Evaluate B * A
c. Place the result of (b) back on STACK
5. Set VALUE equal top element on STACK
6. return.
SUBJECT: DATA STRUCTURE 30/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q15. Write a program to find result of an infix expression.
Ans.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char ie[20];
char pe[20];
char s[20];
char *p,c;
int p1,p2;
int i,t=0,k=0,j=0,si[20];
clrscr();
scanf("%s",&ie);
printf("infix=%s",ie);
i=strlen(ie);
ie[i]=NULL;
i=0;
while(ie[i]!=NULL)
{
if(ie[i]=='+'||ie[i]=='-'||ie[i]=='*'||ie[i]=='/')
{
if(t==0)
{
s[t]=ie[i];
t++;
}
else
{ if(ie[i]=='*'||ie[i]=='/')
{
s[t]=ie[i];
t++;
}
else
{
while(s[t-1]=='*'||s[t-1]=='/'||s[t-1]=='-'||s[t-1]=='+')
{
pe[k]=s[t-1];
k++;
t--;
}
s[t]=ie[i];
t++;
}}}
else
{
pe[k]=ie[i];
k++;
}
i++;
}
t--;
while(t!=-1)
{
pe[k++]=s[t--];
}
pe[k]='0';
printf("npostfix=%s",pe);
i=strlen(pe);
j=0;
t=-1;
while(j<i)
{
if(pe[j]=='+'||pe[j]=='-
'||pe[j]=='/'||pe[j]=='*')
{
p1=si[t--];
p2=si[t];
switch(pe[j])
{
case '+':
si[t]=p2+p1;
break;
case '-':
si[t]=p2-p1;
break;
case '*':
si[t]=p2*p1;
break;
case '/':
si[t]=p2/p1;
break;
}
}
else
{
t++;
c=pe[j];
p=&c;
si[t]=atoi(p);
}
j++;
}
printf("nans=%d",si[0]);
getch();
}
SUBJECT:SUBJECT:SUBJECT:SUBJECT: DATA STRUCTUREDATA STRUCTUREDATA STRUCTUREDATA STRUCTURE 31313131////50505050
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q16. Write a program of priority queue.
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int pq[10],n=0,i,c,e;
clrscr();
do
{
printf("nenter 1 to insert 2 to delete 3 for print
4 for exit");
scanf("%d",&c);
if(c==1)
{
if(n==10)
printf("overflow");
else
{
if(n==0)
{
printf("nenter ele");
scanf("%d",&pq[n]);
n=n+1;
}
else
{
i=n;
printf("nenter ele");
scanf("%d",&e);
while(pq[i-1]>e&&i>0)
{
pq[i]=pq[i-1];
i--;
}
pq[i]=e;
n=n+1;
}
}
}
if(c==2)
{
if(n==0)
printf("nunderflow");
else
{
printf("ndeleted ele=%d",pq[0]);
i=0;
while(i<n-1)
{
pq[i]=pq[i+1];
i++;
}
n--;
}
}
if(c==3)
{
for(i=0;i<n;i++)
printf("%d ",pq[i]);
}
}while(c!=4);
}
CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.
# 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala
CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
SUBJECT: DATA STRUCTURE 32/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Section – C 2 Marks Questions
[QUESTIONS 1 TO 27] [PAGE 32 TO 35]
Q1. What is big O notation?
Ans. The big O notation provides a theoretical measure of the time or memory required by
an algorithm. It also enables the user to compare the expected run times.
Q2. What are the various data qualifiers?
Ans. Qualifiers are used in association with data types. Qualifiers allow you to add
meaning to the data type. Data type qualifiers affect the allocation or access of data
storage. Various programming languages support various qualifiers. Some of the
qualifiers are discussed below.
Long, signed, unsigned, const, volatile, restrict.
Q3. What are various tuples in a grammar?
Ans. A grammar is defined as a specification, which uses the syntactic rules of a
language. It consists of a finite set of productions. Grammars are used as
replacement specifiers and also to generate string from language based on a set of
production rules.
Formal definition: a grammar is defined by using four tuples, namely Vn, Vt, S and P.
thus grammar G={Vn, Vt, S, P}
Tuples Use
Vn A set of mathematical symbol
Vt A set of terminal symbol
S The starting symbol
P Finite set of production rules
Q4. What does VSAM stands for.
Ans. VSAM is abbreviation for Virtual Storage Access Method, a file management system
used on IBM mainframe.
Q5. What is a tree?
Ans. A graph with no cycles is called a tree. “Tree” is one of the most important nonlinear
data structures in computer algorithms. A tree structure depicts a hierarchical
relationship among the nodes of the tree “Parent-child” relationship is a typical
hierarchical relationship.
Prakas
Raju
Mala
SoumeAnita
Gouta arth MouAmit
Pankaj Sujat Ujjwa
SUBJECT: DATA STRUCTURE 33/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q6. Give two applications of binary tree.
Ans. User interface: The best example for this are file system organization and windows
graphical interface.
Database System: Balanced search trees are good in situation that require both
sequential efficiency and random access while performing insertion and deletion.
Q7. Define AVL tree.
Ans. A binary search tree in which the difference of heights of the right and left sub-trees
of any node is less than or equal to one is known as AVL tree. The name is derived
from the names of inventors who are Adelson-Velskii and Land.
Q8. What is a binary tree?
Ans. Binary tree is an important class of tree data structure in
which a node can have at most two children (which are
sub-trees). Moreover, children (or the sub-trees) of the
node of a binary tree are ordered. One child is called the
“left” child and the other is called the “right” child
Q9. What is binary search tree?
Ans. A binary tree in which all elements in the left subtree of a node are less than the
contents of root & all elements in the right subtree are greater than or equal to the
contents of the root. Such a tree is called a binary search tree.
Q10. What are the applications of binary trees?
Ans. A binary tree is a useful data structure when two- way decisions must be made at
each point in a process. eg. Suppose that we want to find all duplicates in a list of
numbers. The no. of comparisons may be reduced by placing the first number in the
root. Each successive node is compared to the root, if it matches, we have a
duplicate of a number is lesser, we examine the left subtree, else if it is larger, we
examine the right subtree.
Q11. What is the level & height of a binary tree?
Ans. Level:
The level of any mode is equal to length of its path from root to the mode: The root of
any tree has level equal to zero.
Height:
It is equal to one + maximum level in a tree.
Q12. How a tree is represented in memory using linked organization?
Ans. A tree is implemented using nodes, a node consists of information part and two
address fields, one to store the address of left node and right to store the address of
right node. If a node does not have left or right child then a sentinel value known as
NULL value is inserted.
Q13. How a tree is represented in memory using sequential organization?
Ans. Under this technique an array is used. Root of the tree is stored at 1st
index of the
array. Left child is stored at double the index of parent node and right child is stored
at double the index of parent +1.
Q14. What is a heap tree?
SUBJECT: DATA STRUCTURE 34/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Ans. In a heap tree root node is larger than its left and right node. Therefore in a heap tree
root node is the largest. It is immaterial in a heap tree whether left child is smaller
than right child or not. A heap tree has utility to perform heap sort which has
complexity as O(log2n).
Q15. What is a spanning tree?
Ans. A graph with no cycles is called a tree. A cycle is formed in a graph id both the start
vertex and the end vertex is the same. In other words, the path has the same first
and the last nodes. A spanning tree of a graph is a sub graph that contains all the
nodes without any cycles. It contains enough edges to form a tree. Various kinds of
spanning tree are available. The most important is the minimum spanning tree.
Q16. What does hashing mean?
Ans. Hashing involves computing the address of a data item by calculating a function on
the search key value. Hashing provides another method of retrieving data. Spell
checkers available in modern day word processors use it.
Q17. What is a strictly Binary tree?
Ans. Strictly binary tree are the binary tree whose each node contains zero or two child
nodes. A strictly binary tree with N leaf nodes contains 2N-1 child nodes.
Q18. What is complete binary tree?
Ans. Complete binary tree are strictly
binary tree in which all leaf
nodes are at the same depth
except last level. A complete
binary tree with M nodes at level
L can contain 2M nodes at level
L+1.Therefore a complete
binary tree of depth D contains
2D nodes.
Q19. Design Node of a Binary Search tree.
Ans. struct node
{
int info;
node *left, *right;
};
Q20. Write different types of traversing in binary tree.
Ans. Traversing (visiting all the nodes) a tree starting at node is often done in one of three
orders
• Preorder - node, left subtree, right subtree.
• Inorder - left subtree, node, right subtree. This could be used to print a binary
search tree in sorted order.
• Postorder - left subtree, right subtree, node. This could be used to print an
expression tree in reverse polish notation (postfix).
Q21. Define Expression Tree.
Ans. Normal infix notation: (7-5) * (3+(1-2))
Tree equivalent:
*
/ 
/
SUBJECT: DATA STRUCTURE 35/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
- +
/  / 
# # # -
7 5 3 / 
# #
1 2
Q22. Define searching.
Ans. Searching is the process of finding data from a set of given data items.
Types:-
Linear search
Binary search
Q23. Define Sorting.
Ans. Sorting is the process of arranging both numeric and alpha numeric data in a specific
order such as ascending or descending. Types:-
Selection sort
Bubble sort
Insertion sort
Quick sort
Merge sort
Radix sort
Heap sort
Q24. What are the various operations performed on binary search tree .
Ans. Basic operations are:-
Insertion
Searching
Deletion
Traversal
Q25. Design node of binary search tree.
Ans. struct node
{
char info;
struct node *left;
struct node *right;
};
Q26. Write function in “C” language to create a tree.
Ans. void create(char n)
{
btree=(struc node*)malloc(sizeof(struct node));
btree->info=n;
btree->left=NULL;
btree->right=NULL;
}
Q27. Write function in “C” language to traverse a tree in Inorder.
Ans. void Inorder (bNode *T)
{
if (T !=NULL)
SUBJECT: DATA STRUCTURE 36/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
{
Inorder (T->left);
printf (“%d” , Toy->data);
Inorder (T->right):
}
return 0;
}
SUBJECT: DATA STRUCTURE 37/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Section – C 5 Marks Questions
[QUESTIONS 1 TO 29] [PAGE 36 TO 48]
Q1. Explain Binary search tree.
Ans. Binary search trees form an important
sub-class of binary trees. In an ordinary
tree, the elements are not ordered in any
way. A binary search tree is a binary
tree which is either empty or in which the
following criteria are satisfied.
1. All keys of the left sub-tree of the
root are “less than” the root.
2. All keys of the right sub-tree of the
root are “greater than” the root.
3. The left and right sub-trees of a
binary search tree are binary
search trees once again. As
Shown in fig:-
Q2. What is a Extended binary tree?
Ans. In an extended binary tree, the special nodes are added to a binary tree to
make it complete binary tree . In extended binary tree each node must contain
two child.
For example, let T be the following tree.
The extended binary tree of T is
Q3. Write an algorithm for linear search.
Ans. Algorithm LINEAR (DATA, N, ITEM, LDC)
Hire DATA is a linear array with N elements & ITEM is a given item of information.
This algorithm finds the location LOC of ITEM in DATA, or sets LOC: = 0 if the search
is unsuccessful.
1. [Insert ITEM at the end of DATA]
Set DATA [N+1]: = ITEM
SUBJECT: DATA STRUCTURE 38/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
2. [Initialize counter]. Set LOC: =1
3. Search for ITEM]
Repeat while DATA [LOC] ≠ ITEM
Set LOC: LOC +1
[End of roop]
4. [Successful?] If LOC= N+1, then Set LOC: = 0
5. Exit
Q4. Write PREORDER algorithms for Tree traversals.
Ans. Algorithm PREORDER (T): Given a binary tree whose root mode address is given by
a pointer variable PTR, this algorithm traverse the tree in preorder in a recursive
manner.
1. [Process the root mode]
If PTR ≠ NULL then write (DATA (Toy) )
Else write (‘Empty Tree”)
Return.
2. [Process the left subtree]
If LPTR≠ NULL
Then call PREORDER (LPTR (7) )
3. [Process the right subtree0
If RPTR (T) ≠ NULL
Then call PREORDER (RPTR (T) )
4. Exit
Q5. Write INORDER algorithms for Tree traversals.
Ans. Algorithm INORDER (T): Given a binary tree whose root mode address is given by a
pointer variable T, this algorithm traverse the tree in preorder in a recursive manner.
Algorithm INORDER (T):
1. [Check for empty tree]
If T= NULL
Then write (‘empty tree’)
Return.
2. [Process the left subtree]
If LPTR ≠ NULL
Then call INORDER (LPTR (T))
3. [Process the root mode]
Write (DATA (T))
4. [Process the right subtree]
If RPTR ≠ NULL
Then call INORDER (RPTR (T))
5. Exit.
Q6. Write POSTORDER algorithms for Tree traversals.
Ans. Algorithm POSTORDER (T): Given a binary tree whose root mode address is given
by a pointer variable PTR, this algorithm traverse the tree in preorder in a recursive
manner.
Algorithm POSTORDER (Toy)
1. [Check for empty tree]
If PTR=NULL then write (‘Empty tree’)
Return
SUBJECT: DATA STRUCTURE 39/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
2. [Process the left subtree]
If LPTR (PTR) ≠ NULL
Then call POSTORDER (LPTR (PTR))
3. [Process the right subtree]
If RPTR (PTR) ≠ NULL
Then call POSTORDER (RPTR (PTR))
4. [Process the root mode]
Write (DATA (PTR)
5. Exit
Q7. List some of Hashing methods.
Ans. 1. Division method: Choose a number m larger key in k. the hash function h is
defined by
H(k)=k(mod m) or H(k)=k(mod m)+1
Here k(mod m) denotes the remainder where k is divided by m. the second formula
is used where we want the hash address to range from 1 to m rather than from 0 to
m-1.
2. Mid square method: The key k is squared. Then the hash function h is defined by
H(k) =l
Where l is obtained by deleting digits from both ends of k2
. We emphasize that the
same position of k2
must be used for all of the keys.
3. Folding Method: The key k is partitioned into a number of parts k1,k2,………..,kr,
where each part except possibly the last has the same number of digits as the
required address. There the parts are added together, ignoring the last crry. That is
H(k) =k1+k2+k3+…….+kr
Where the leading digit carries, if any ignored. Some times for extra “milling” the even
numbered parts k2,k4….are each reversed before the addition.
Q8. Define String and operations performed on strings.
Ans. Character type array is called string. String is a collection of characters. There are
several operations, which typically arise in the context of string manipulation Few
important such operations are outlined below
String Operations
IsEmpty (string s)
This operation checks whether a string “s” is null or not
StringLength (string s)
This operation computes the length of a string “s” .
String StringConcatenate (string s1, string s2)
This operation concatenates two strings “s” and “s2” to produce another string.
StringCopy (string s1, string s2)
This operation copies a string “s2” to “s1”. This operation becomes useful whenever
one strings to be assigned to another string.
Int StringCompare (string s1, string s2)
This operation compares two strings “s1” and “s2” and finds out whether they are
identical.
In StringSearch (string s1, s2)
This operation tests whether a given string “s2” occurs in another given string “s1”.
Q9. Explain Binary Search method.
Ans. When the given array is sorted then a considerably better method of searching is
possible. This method, known as binary search, makes a comparison between “k”
and the middle element of the array. Since the array is sorted, this comparison
SUBJECT: DATA STRUCTURE 40/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
results either in a match between “k” and the middle element of “A”, or identifying the
left half or the right half of the array to which the desired element may belong. In the
case when the current element is not equal to the middle element of “A” , the
procedure is repeated on the half in which the desired element is likely to be present.
Proceeding in this way, either the element is detected or the final division leads to
half consisting of no element. In this case, it is ascertained that the array does not
contain “k”. This is a very efficient method of searching because each comparison
enables one to eliminate half of the elements form further consideration. Because of
this basic principle of elimination, the method is also known as dichotomous search.
Q10. Explain the time complexity of the average case for the bubble sort.
Ans. The bubble sort algorithm has two loops: inner and outer. The outer loop i.e. must
iterate as many times as the elements in the array while the inner loop iterates n
times for the first time, n-1 times second time and go on decreasing. The total
number of comparisons made are (n+1)+(n-2)+…+2+1=n(n-1)/2 or O(n2).
The average case for the bubble sort is also n2.
Q11. List different searching techniques and explain one in detail.
Ans. Searching is the process of finding data from a set of given data items.
Linear Search
Binary Search
Search trees
Hash table methods
Linear Search: The linear search is straightforward method of retrieving elements.
This is often considered the simplest technique for searching an unordered list of
elements. In linear search, the list is scanned element by element for the element to
be retrieved. When an appropriate match is found, a message is displayed. A
sequential search can be applied to any kind of list static or dynamic. Following are
some of the properties of the linear search technique.
The item to be found is called the key.
The search function will return the index number (for a static list) or a pointer (for
dynamic list) when item is found in the list.
If the key cannot be found in the list, then not found value is returned eg. –1 for a
static list or NULL for a dynamic list.
Q12. Write down the binary search algorithm.
Ans. Let A be an array of N elements and ele is the element to be searched in the array.
Step 1. Start=0, End=N-1, Loc=-1
Step 2. Repeat Steps 3, 4 while Start<=End and Loc=-1
Step 3. Mid=(Start+End)/2
Step 4. if A[Mid]=ele then
i. loc=mid
ii. exit
else
if A[Mid]<ele then
start=Mid+1
else
end=mid-1
[end of If]
[end of while]
Step 5. Exit
SUBJECT: DATA STRUCTURE 41/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q13. Write an algorithm to search an element from a binary search tree.
Ans. Variables to be used:
1.INFO is the information field of node.
2.LEFT points to left child
3.RIGHT Points to right child
4.PAR points to parent node
5.LOC points to ITEM if exists otherwise points to NULL
6.ITEM which is to search
7.ROOT is the root of tree
Algorithm:
1. If ROOT=NULL, then LOC:=NULL and PAR:=NULL
2. If ITEM=INFO[ROOT] then set LOC:=ROOT and PAR:=NULL and Return
3. If ITEM<INFO[ROOT], then
Set PTR:=LEFT[ROOT] and SAVE:=ROOT
Else:
Set PTR:=RIGHT[ROOT] and SAVE:=ROOT
4.Repeat steps 5 and 6 while PTR≠NULL:
5.If ITEM=INFO[PTR],
then: set LOC:=PTR and PAR:=SAVE and return
6.If ITEM<INFO[PTR], then:
set SAVE:=PTR and PTR:=LEFT[PTR]
else
set SAVE:=PTR and PTR:=RIGHT[PTR]
7.set LOC:=NULL and PAR:=SAVE.
8.Exit
Q14. Write an algorithm to insert an item into a heap tree.
Ans. Variables to be used:
1.PAR points to parent node
2.ITEM which is to search
3.ROOT is the root of tree
4.CHILD is the child to delete
N represents total number of nodes.
Algorithm:
1. set N:=N+1 and PTR:=N
2. Repeat step 3 to 6 while PTR<1
3. set PAR:=PTR/2
4. If ITEM<=TREE[PAR] then
Set TREE[PTR]:=ITEM, and return
5. set TREE[PTR]:=TREE[PAR]
6. set PTR:=PAR
7. set TREE[1]:=ITEM
8. Return
Q15. Discuss complexity of heap sort.
Ans. Complexity of heap sort can be calculated in following two phases:
a) If H is a heap tree. The number of comparisons to find the appropriate
place of a new element ITEM in H cannot exceed the depth of H. Since H
is a complete tree, therefore its depth is bounded by log2m where m is
total number of elements in H.
SUBJECT: DATA STRUCTURE 42/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
b) If there are m elements in heap tree then there is need to place m
elements and each element requires log2m comparisons. Therefore
complexity is O(nlog2n).
Q16. Write an algorithm for Bubble sort .
Ans. Algorithm BUBBLE (DATA, N)
Here DATA is an array with n elements. This algorithm sorts the elements in DATA.
1. Repeat steps 2 & 3 for k = I to N-I
2. Set PTR: = 1 [Initializes pass pointer]
3. Repeat while PTR ≤ N-K [Execute pass]
a) If DATA [PTR] > DATA [PTR +1], then interchange DATA [PTR] & DATA
[PTR+1]
[End of If structure]
b) Set PTR: = PTR+1
[End of inner loop]
[End of step 1 outer loop]
4. Exit
Q17. Write an algorithm for linear search.
Ans. Algorithm LINEAR (DATA, N, ITEM, LoC)
Hire DATA is a linear array with N elements & ITEM is a given item of information.
This algorithm finds the location LOC of ITEM in DATA, or sets LOC: = 0 if the search
is unsuccessful.
1. [Insert ITEM at the end of DATA]
Set DATA [N+1]: = ITEM
2. [Initialize counter]. Set LOC: =1
3. Search for ITEM]
Repeat while DATA [LOC] ≠ ITEM
Set LOC: LOC +1
[End of roop]
4. [Successful?] If LOC= N+1, then Set LOC: = 0
5. Exit.
Q18. Write an algorithm for merge sort.
Ans. Algorithm Merge (A, LBound, UBound, MID value)
1. I=UBound
2. J=MIDvalue+1
3. K=LBound
4. Repeat while (I<MIDvalue) and (J<=UBound)
If (A[I] < A [J])
Then L [K]= A [I]
K= K+1
I=I+1
Else
C [K] = A [H]
J = J+1
K = K+1
5. Repeat while (IL = MIDvalue)
C[K] = A[I]
I = I+1
K = K+1
SUBJECT: DATA STRUCTURE 43/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
6. Repeat while (J L = Ubound)
C [K] = A[J]
J = J+1
K = K+1
7. Repeat for I = LBound, LBound + -------UBound
A [I] = C[I]
8. Return
SUBJECT: DATA STRUCTURE 44/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q19. Write a program for quick sort.
Ans.
#include<stdio.h>
#include<conio.h>
#define max 10
int partition();
void quick();
void p();
int a[max],low,high;
void main()
{
int i;
low=1;
high=max;
for(i=1;i<=max;i++)
{
printf("nEnter the number: ");
scanf(“%d”,&a[i]);
}
quick();
getch();
}
void quick(int a[],int low,int high)
{
int j,i;
if(low < high)
{
j=partition();
p();
} }
int partition()
{
int key,i,j,temp;
char over='f';
key=a[low];
i=low+1;
j=high;
while(!over)
{
while(key > a[i])
{ i++; }
while(key < a[j])
{
j--;
}
if(i < j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Q20. Write a program for Merge Sort.
Ans.
#include<stdio.h>
#include<conio.h>
#define max 10
int a[max];
void simple();
void proc();
void main()
{
int low=0,high,i,mid;
high=max-1;
for(i=0;i<max;i++)
{
printf("nEnter the number: ");
Scanf(“%d”,&a[i]);
}
proc();
for(i=0;i<max;i++)
{
printf(“%d”,a[i]);
}
getch();
}
void proc(int a[],int low,int high)
{
int mid;
if(low < high)
{ mid=(low + high)/2; }
proc();
proc();
simple();
}
void simple(int a[],int i,int j,int high)
{
int k,m,n,temp[max];
while((i <= j-1) && (j <= high))
{
if(a[i] < a[j])
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
if(i <= j-1)
SUBJECT: DATA STRUCTURE 45/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
else
{
over='t';
}
}
temp=a[low];
a[low]=a[j];
a[j]=temp;
cout<<a[low];
return j;
}
void p()
{int j;
quick(a,low,j-1);
quick(a,j+1,high);
}
{
for(m=0;m<=j-1;m++)
{ temp[k]=a[m];
k++;
} }
else
{
for(m=j;m<=high;m++)
{ temp[k]=a[m];
k++;
}} }
for(m=i;m<=high;m++)
{
a[m]=temp[k];
k++;
}}
Q21. Write a program of selection sort.
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++)
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
for(i=0;i<10;i++)
printf(" %d",a[i]);
getch();}
Q22. Write a program of bubble sort.
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=9;i>0;i--)
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
for(i=0;i<10;i++)
printf(" %d",a[i]);
getch();
}
Q23. Write a program of insertion sort.
Ans. #include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=1;i<=9;i++)
{
SUBJECT: DATA STRUCTURE 46/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
j=i-1;
t=a[i];
while(t<a[j])
{
a[j+1]=a[j];
j--;
if(j==-1)
{
a[j+1]=t;
break;
}}
a[j+1]=t;
}
for(i=0;i<10;i++)
printf(" %d",a[i]);
getch();
}
Q24. Write a program of Binary search.
Ans. #include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
int a[20],i,n,beg,end,mid=0,item;
clrscr();
printf("HOW MANY VALUES YOPU WANT TO ENTER");
scanf("%d",&n);
printf("ENTER VALUES");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
beg=0;
end=n-1;
printf("ENTER ITEM");
scanf("%d",&item);
while(beg<=mid)
{
mid=(beg+end)/2;
if(a[mid]==item)
{
printf("VALUE IS FOUND AT LOCATION:%d",mid);
getch();
exit(0);
}
if(a[mid]>item)
{
end=mid;
}
else
{
SUBJECT: DATA STRUCTURE 47/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
beg=mid;
}
}
printf("VALUE NOT FOUND");
getch() ;
}
Q25. Write complexity of various sorting techniques.
Ans.
Bubble: O(n2
)
Insertion : O(n2
)
Selection: O(n2)
Merge :O(n log(n))
Radix: o(k*n)
Heap: O(n log(n))
Quick: O(n2
)
Q26. What are the various operations performed on binary search tree and design
node of binary search tree and write a c function to create a tree.
Ans. Basic operations are:-
Insertion
Searching
Deletion
Traversal
struct node
{
char info;
struct node *left;
struct node *right;
};
void create(char n)
{
btree=(struc node*)malloc(sizeof(struct node));
btree->info=n;
btree->left=NULL;
btree->right=NULL;
}
Q27. Write a function in “C” language to insert a element in a tree.
Ans. void insert(char n)
{
if(btree==NULL)
{
create(n);
return;
SUBJECT: DATA STRUCTURE 48/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
}
else
{
ptr=btree;
while(ptr!=NULL)
{
if(ptr->info==n)
{
printf(“node already exists”); return;
}
else
{
if(ptr->info<n)
{
ptr->right=(struc node*)malloc(sizeof(struct node));
ptr=ptr->right;
ptr->info=n;
ptr->right=NULL;
ptr->left=NULL;
return;
}
else
{
ptr=ptr->right;
continue;
}
}
else
{
if(ptr->info>n)
{
if(ptr->left==NULL)
{
ptr->left=(struc node*)malloc(sizeof(struct node));
ptr=ptr->left;
ptr->info=n;
ptr->right=NULL;
ptr->left=NULL;
return;
}
else
{
ptr=ptr->left;
continue;
}
}
}
}
CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.
# 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala
CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
SUBJECT: DATA STRUCTURE 49/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
}
Q28. Write a function in “C” language to search a element in a tree.
Ans.
struct node* search(char n)
{
int f=0;
ptr=NULL;
while(ptr!=NULL)
{
if(ptr->info==n)
{
f=1;
break;
}
else
if(ptr->info>n)
{
ptr=ptr->left;
continue;
}
else
if(ptr->info<n)
{
ptr=ptr->right;
continue;
}
}
if(f==0)
{
printf(“node found”);
return (ptr);
}
else
{
printf(“node not found”);
return(NULL);
}
}
SUBJECT: DATA STRUCTURE 50/50
Prepared By: CJ SofTech [Charanjiv Singh 98156-18658]
Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work
Q29. Explain Binary Tree Traversing.
Ans. Traversal of a binary tree is one of the most important operation required to be done
on a binary tree. Many other operations involving binary tree data structure often
need to traverse or walk through a given tree. Traversing is a process of visiting
every node in the tree exactly once.
Inorder Traversal
If a tree “Toy”, is traversed in an “inorder” fashion then the left sub-tree of “T” is
traversed first, then the root node of “T” is visited and then the right sub-tree of “T” is
traversed. The “C” function for inorder traversal of the tree, T, is presented below.
Void Inorder (bNode *T)
{
if (T !=NULL)
{
Inorder (Toy->left);
printf (“%d” , Toy->data);
Inorder (Toy->right):
}
return;
}
Preorder Traversal
If a tree. “T” , is traversed in “preorder” fashion then the root node of “Toy” is
visited first and then the left sub-tree of “T” is traversed and finally the right sub-tree
of “T” is traversed. The “C” function for Preorder traversal of the tree, T, is presented
below.
Void Preorder (bNode *T)
{
If (T!= NULL)
{
printf (“%d” , Toy->data);
Preorder (T->left);
Preorder (T->right);
}
return;
}
Postorder Traversal
If a tree, “T” , is traversed in “postorder” manner, then the left sub-tree of “T” is
traversed first, then the right sub-tree of “T” is traversed and finally the root node of
“T” is visited . The “C” function for Postorder traversal of the tree, T, is presented
below.
void Postorder (bNode *T)
{
Postorder (T->left);
Postorder (T->right);
printf (“%d” , T->data);
}
return;
}

More Related Content

What's hot

Verbs Followed by Gerunds or Infinitives
Verbs Followed by Gerunds or InfinitivesVerbs Followed by Gerunds or Infinitives
Verbs Followed by Gerunds or InfinitivesPeter Mangiaracina
 
Chapter 14 presentation by azar
Chapter 14 presentation by azarChapter 14 presentation by azar
Chapter 14 presentation by azarAbdel-Fattah Adel
 
Present perfect
Present perfectPresent perfect
Present perfectmluisavm
 
Modal verbs 6B: can, could, be able to
Modal verbs 6B: can, could, be able toModal verbs 6B: can, could, be able to
Modal verbs 6B: can, could, be able toMíriam
 
Presentation 8 noun clauses & embedded questions
Presentation 8 noun clauses & embedded questionsPresentation 8 noun clauses & embedded questions
Presentation 8 noun clauses & embedded questionsMarta Łopion
 
Modal verbs-must/have to- should/ought to-may/might/must/can have p.p. verb
Modal verbs-must/have to- should/ought to-may/might/must/can have p.p. verb Modal verbs-must/have to- should/ought to-may/might/must/can have p.p. verb
Modal verbs-must/have to- should/ought to-may/might/must/can have p.p. verb y T
 
Present perfect vs past simple
Present perfect vs past simplePresent perfect vs past simple
Present perfect vs past simpleevapangelova
 
Must, Have To, Should
Must, Have To, ShouldMust, Have To, Should
Must, Have To, ShouldDarplay
 

What's hot (20)

FUTURE TENSES
FUTURE TENSESFUTURE TENSES
FUTURE TENSES
 
Present Perfect 3 eso
Present Perfect 3 esoPresent Perfect 3 eso
Present Perfect 3 eso
 
Verbs Followed by Gerunds or Infinitives
Verbs Followed by Gerunds or InfinitivesVerbs Followed by Gerunds or Infinitives
Verbs Followed by Gerunds or Infinitives
 
Chapter 14 presentation by azar
Chapter 14 presentation by azarChapter 14 presentation by azar
Chapter 14 presentation by azar
 
Gerunds and infinitives
Gerunds and infinitives Gerunds and infinitives
Gerunds and infinitives
 
Used to
Used toUsed to
Used to
 
Present perfect
Present perfectPresent perfect
Present perfect
 
Modal verbs 6B: can, could, be able to
Modal verbs 6B: can, could, be able toModal verbs 6B: can, could, be able to
Modal verbs 6B: can, could, be able to
 
Tenses project
Tenses projectTenses project
Tenses project
 
Present perfect simple and continuous
Present perfect simple and continuousPresent perfect simple and continuous
Present perfect simple and continuous
 
Presentation 8 noun clauses & embedded questions
Presentation 8 noun clauses & embedded questionsPresentation 8 noun clauses & embedded questions
Presentation 8 noun clauses & embedded questions
 
future tense
future tensefuture tense
future tense
 
English Phrasal Verbs
English Phrasal VerbsEnglish Phrasal Verbs
English Phrasal Verbs
 
English Phrasal Verbs
English Phrasal VerbsEnglish Phrasal Verbs
English Phrasal Verbs
 
Modal verbs-must/have to- should/ought to-may/might/must/can have p.p. verb
Modal verbs-must/have to- should/ought to-may/might/must/can have p.p. verb Modal verbs-must/have to- should/ought to-may/might/must/can have p.p. verb
Modal verbs-must/have to- should/ought to-may/might/must/can have p.p. verb
 
Present perfect vs past simple
Present perfect vs past simplePresent perfect vs past simple
Present perfect vs past simple
 
Third Conditional
Third ConditionalThird Conditional
Third Conditional
 
Should, must, have to
Should, must, have toShould, must, have to
Should, must, have to
 
The Complete Sentence Ppt
The Complete Sentence PptThe Complete Sentence Ppt
The Complete Sentence Ppt
 
Must, Have To, Should
Must, Have To, ShouldMust, Have To, Should
Must, Have To, Should
 

Viewers also liked

الفصل الأول
الفصل الأولالفصل الأول
الفصل الأولismail789123
 
桃太郎Pdf
桃太郎Pdf桃太郎Pdf
桃太郎Pdf777nana
 
Hash set (java platform se 8 )
Hash set (java platform se 8 )Hash set (java platform se 8 )
Hash set (java platform se 8 )charan kumar
 
Entrepreneurship development for final year students
Entrepreneurship development for final year studentsEntrepreneurship development for final year students
Entrepreneurship development for final year studentsSandip Mavchi
 
Unit i data structure FYCS MUMBAI UNIVERSITY SEM II
Unit i  data structure FYCS MUMBAI UNIVERSITY SEM II Unit i  data structure FYCS MUMBAI UNIVERSITY SEM II
Unit i data structure FYCS MUMBAI UNIVERSITY SEM II ajay pashankar
 
rimap Continuous Professional Development
rimap Continuous Professional Developmentrimap Continuous Professional Development
rimap Continuous Professional DevelopmentFERMA
 
Tax: PwC Top Issues
Tax:  PwC Top Issues Tax:  PwC Top Issues
Tax: PwC Top Issues PwC
 
банк как It компания алексеев-finopolis2016_13окт
банк как It компания алексеев-finopolis2016_13октбанк как It компания алексеев-finopolis2016_13окт
банк как It компания алексеев-finopolis2016_13октfinopolis
 
Fintech Insurance Report -June 2016
Fintech Insurance Report -June 2016Fintech Insurance Report -June 2016
Fintech Insurance Report -June 2016PwC
 

Viewers also liked (12)

الفصل الأول
الفصل الأولالفصل الأول
الفصل الأول
 
pwc-tax-function-of-the-future
pwc-tax-function-of-the-futurepwc-tax-function-of-the-future
pwc-tax-function-of-the-future
 
桃太郎Pdf
桃太郎Pdf桃太郎Pdf
桃太郎Pdf
 
Hash set (java platform se 8 )
Hash set (java platform se 8 )Hash set (java platform se 8 )
Hash set (java platform se 8 )
 
Schule
SchuleSchule
Schule
 
Entrepreneurship development for final year students
Entrepreneurship development for final year studentsEntrepreneurship development for final year students
Entrepreneurship development for final year students
 
Unit i data structure FYCS MUMBAI UNIVERSITY SEM II
Unit i  data structure FYCS MUMBAI UNIVERSITY SEM II Unit i  data structure FYCS MUMBAI UNIVERSITY SEM II
Unit i data structure FYCS MUMBAI UNIVERSITY SEM II
 
rimap Continuous Professional Development
rimap Continuous Professional Developmentrimap Continuous Professional Development
rimap Continuous Professional Development
 
Social learning theory
Social learning theorySocial learning theory
Social learning theory
 
Tax: PwC Top Issues
Tax:  PwC Top Issues Tax:  PwC Top Issues
Tax: PwC Top Issues
 
банк как It компания алексеев-finopolis2016_13окт
банк как It компания алексеев-finopolis2016_13октбанк как It компания алексеев-finopolis2016_13окт
банк как It компания алексеев-finopolis2016_13окт
 
Fintech Insurance Report -June 2016
Fintech Insurance Report -June 2016Fintech Insurance Report -June 2016
Fintech Insurance Report -June 2016
 

Similar to Data structure manual (20)

Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5
 
Data structure and algorithm notes
Data structure and algorithm notesData structure and algorithm notes
Data structure and algorithm notes
 
Bt0065
Bt0065Bt0065
Bt0065
 
B T0065
B T0065B T0065
B T0065
 
Mcs 011 solved assignment 2015-16
Mcs 011 solved assignment 2015-16Mcs 011 solved assignment 2015-16
Mcs 011 solved assignment 2015-16
 
L5 array
L5 arrayL5 array
L5 array
 
Main ds manual
Main ds manualMain ds manual
Main ds manual
 
Doc 20180130-wa0005
Doc 20180130-wa0005Doc 20180130-wa0005
Doc 20180130-wa0005
 
Doc 20180130-wa0004-1
Doc 20180130-wa0004-1Doc 20180130-wa0004-1
Doc 20180130-wa0004-1
 
Doc 20180130-wa0004
Doc 20180130-wa0004Doc 20180130-wa0004
Doc 20180130-wa0004
 
Chapter 1 Basic Concepts
Chapter 1 Basic ConceptsChapter 1 Basic Concepts
Chapter 1 Basic Concepts
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Project Management System
Project Management SystemProject Management System
Project Management System
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structures
StructuresStructures
Structures
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Doc 20180130-wa0006
Doc 20180130-wa0006Doc 20180130-wa0006
Doc 20180130-wa0006
 

More from sameer farooq

Can a firewall alone effectively block port scanning activity
Can a firewall alone effectively block port scanning activityCan a firewall alone effectively block port scanning activity
Can a firewall alone effectively block port scanning activitysameer farooq
 
Virtual Circuit Switching: Frame Relay and ATM
Virtual Circuit Switching:Frame Relayand ATMVirtual Circuit Switching:Frame Relayand ATM
Virtual Circuit Switching: Frame Relay and ATMsameer farooq
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
Software Project management
Software Project managementSoftware Project management
Software Project managementsameer farooq
 
Radio Frequency Waves ,Data communication & Networks
Radio Frequency Waves ,Data communication & NetworksRadio Frequency Waves ,Data communication & Networks
Radio Frequency Waves ,Data communication & Networkssameer farooq
 

More from sameer farooq (8)

Idps book
Idps bookIdps book
Idps book
 
Can a firewall alone effectively block port scanning activity
Can a firewall alone effectively block port scanning activityCan a firewall alone effectively block port scanning activity
Can a firewall alone effectively block port scanning activity
 
Windows firewall
 Windows firewall  Windows firewall
Windows firewall
 
Virtual Circuit Switching: Frame Relay and ATM
Virtual Circuit Switching:Frame Relayand ATMVirtual Circuit Switching:Frame Relayand ATM
Virtual Circuit Switching: Frame Relay and ATM
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Software Project management
Software Project managementSoftware Project management
Software Project management
 
kerberos
kerberoskerberos
kerberos
 
Radio Frequency Waves ,Data communication & Networks
Radio Frequency Waves ,Data communication & NetworksRadio Frequency Waves ,Data communication & Networks
Radio Frequency Waves ,Data communication & Networks
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 

Data structure manual

  • 1. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 1111////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Section – A 2 Marks Questions [QUESTIONS 1 TO 18] [PAGE 1 TO 4] Q1. What is data structure? Ans. The logical or mathematical model of a particular organization of data is called a data structure. Q2. What are two main types of data structure? Ans. 1.Linear Data Structure: A data structure is said be linear if its elements are stored sequentially in the form of a linear list e.g. arrays, stacks, linked lists. 2. Non-linear Data structure: A data structure is said to be non-linear if its elements are not stored sequentially in the form of linear list e.g. trees & graphs. Q3. Write various operations performed on data structure. Ans. The various operations are: - 1. Traversing 2. Inserting 3. Deleting 4. Merging 5. Searching 6. Sorting 7. Updating 8. Creating Q4. What is an array? How array is stored in memory. Ans. Array is collection of homogeneous data elements. They are stored in continuous memory locations. They are accessed by using index value. int a[5]; 12 4 45 67 68 1000 1002 1004 1006 1008 1000 is the location of the first element of the array. As the int data type takes two bytes of the memory so the next element is stored at 1002 and so on. Q5. What are pointer Arrays? Ans. A pointer array is memory variable that use to store address of a variable. Pointer arrays are use to store address of more then one variable. e.g. main() { int *a[3]; int x,y,z; x=10; y=20; z=30; a[0]=&x; a[1]=&y; a[2]=&z; printf(“address of x=%u”,a[0]); } Q6. What is a linked list? Ans. A linked list or one-way list is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. Each node is divided into 2 parts. The first part contains the information of the element & the second part called the link field, contains the address of the next node in the list.
  • 2. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 2222////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q7. Write syntax to declare a node of linked list. Ans. Singly linked list struct node { int info; node *link; }; Doubly linked list Struct node { int info; node *next,*prev; }; Q8. What are different types of linked list? Ans. The various types of linked list are:- 1. Singly 2. Doubly 3. Circular 4. Generalized Q9. What are Reversible linked list? Ans. A reversible linked list enable you to visit a node, Which is previously visited by the pointer, we can visit any node, which is previously visited, By providing address of the node. Q10. What are operations performed on linked list? Ans. 1. Insertion 2. Deletion 3. Searching 4. Traversing Q11. Write an algorithm to delete a node from linked list from beginning. Ans. DELFIRST (LINK, START). 1. [Underflow?] If START = NULL then: Write underflow and Exit 2. Set START=LINK [START] 3. Exit. Q12. Write an algorithm to Delete a node from linked list from END. Ans. DELEND(LINK, START,TEMP). 1 [Underflow ] If START = NULL then: Write UNDERFLOW and Exit 2 Set TEMP = START 3 WHLE LINK[TEMP]!=NULL TEMP=LINK[TEMP] END LOOP 4 Set TEMP=NULL 5 Exit. Q13. Write a function to insert in a linked list. Ans. void insert(struct node **s) { struct node *p,*l; int cc; p=(struct node*)malloc(sizeof(struct node)); if(*s==NULL) { printf(“enter element”);
  • 3. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 3333////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work scanf(“%d”,&p->nfo); p->link=NULL; *s=p; } else { printf(“nEnter 1 for insert at beg”); printf(“nEnter 2 for insert at end”); scanf(“%d”,&cc); if(cc==1) { printf(“Enter element”); scanf(“%d”,&p->info); p->link=*s; *s=p; } if(cc==2) { printf(“Enter element”); scanf(“%d”,&p->info); p->link=NULL; l=*s; while(l->link!=NULL) { l=l->link; } l->link=p; } } } Q14 Write a function to delete from a linked list. Ans. void del(node **s) { node *l; l=*s; if(l==NULL) printf("no element"); else { printf(“dele ele=%d”,l->info); *s=l->link; } } Q15 Write a function to print elements from a linked list. Ans. void print(node *s) { printf(“nElement are:->”); while(s!=NULL) { printf(“%d”,s->info); s=s->link; } } CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala. # 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
  • 4. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 4444////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q16 Define Header linked list. Ans. A header linked list is a linked list which always contains a special node called the header node, at the beginning of the list. TYPES 1.Gounded header list:-In this last node contain the null pointer. 2.Circular header list:- In this last node points back to the header ndode. Q17. Differentiate between local & global variables. Ans. Local variables: are those variables, which are accessible to a program within a particulars scope. These are accessible to the code within the limited scope. They are generally used when recursion is required in programming. Global variables: a certain variable are those variables, which are declared outside the scope. These variables are available throughout the program. Global variables are used when you want to reverse. Q18. Differentiate between structured & modular programming. Ans. Structured programming considers the problem as a single structure & applies the various conventional methods to solve that problem. On the other hand, modular programming divides the main module into a number of different sub modules. Then the main module refers to a number of subordinate modules Section – A 5 Marks Questions [QUESTIONS 1 TO 18] [PAGE 4 TO 15] Q1. How to declare an array? Ans. Typeofvariable variablename [size] (1) An array must be declared since it is a type of variable. (2) An array containing five elements all of which are integers can be declared as follows: int x[5]; (3) The name of the array can not be same as that of any other variable declared within the function. (4) The size of the array (the no of elements ) specified using the subscript notation. (5) The subscript 5 indicates how many elements are to be allocated to array x (6) The subscript used to declare an array are called dimension. The elements of five element array in c are numbered starting with 0 not with 1. E.g. Suppose we want to store nos. 56,57,58,90,3 then the assignment statement for this should be as under x[0]=56 // first no x[1]=57 // second no x[2]=58 // third no x[3]=90 // fourth no x[4]=3 //fifth no Q2. Write a program to input and print numbers using array. Ans. #include<stdio.h> void main() { int i,s[10]; for(i=0;i<10;i++) { scanf("%d",&s[i]); } for(i=0;i<10;i++) {
  • 5. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 5555////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work printf("n %d",s[i]); } } Q3. Write a program to input and print characters using array. Ans. #include<stdio.h> void main() { int I; char s[10]; for(i=0;i<10;i++) { scanf("%c”,&s[i]); } for(i=0;i<10;i++) { printf("%c",s[i]); } } OR void main() { int I; char s[10]; scanf("%s”,&s); printf("%s",s); } Q4. Write a program to input marks in 4 subjects of 10 students and print. Ans. #include<stdio.h> main( ) { int data [10][4]; int r,c; clrscr( ); //to enter data for(r=0;r<10;r++) { printf("enter the detail %d student",r+1); for(c=0;c<4;c++) { printf("enter the marks in sub %d",c+1); scanf("%d",&data[r][c]); } } // to display data for(r=0;r<10;r++) { printf("n detail of %d student",r+1); for (c=0;c<4;c++) { printf("n the marks in sub %d Is Id",c+1,data[r][c]); } } getch( ); } CJ SOFTECH Walia Enclave Opp Punjabi UnivCJ SOFTECH Walia Enclave Opp Punjabi UnivCJ SOFTECH Walia Enclave Opp Punjabi UnivCJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.Patiala.Patiala.Patiala. # 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
  • 6. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 6666////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q5. Create a program to print the transpose of a matrix. Ans. void main() { int a[2][2],I,j; for(I=0;I<2;I++) { printf(“enter the values for %d row of matrix”,I+1); for(j=0;j<2;j++) { scanf(“%d”,&a[I][j]); } } /* transposing the matrix */ for(I=0;I<2;I++) { for(j=0;j<2;j++) { printf(“t%d”,a[j][I]); } printf(“n”); } getch(); } Q6. Write a program for multiplication if two 2-D array. Ans. #include<stdio.h> void main() { int a[10][10],b[10][10],c[10][10],I,j,k,r1,r1,r2,c1,c2; printf( “Enter Rows of first matrix “); scanf(“%d”,&r1); printf( “Enter columns of first matrix”); scanf(“%d”,&c1); printf( “Enter columns of first matrix”); scanf(“%d”,&r2); printf( “Enter columns of first matrix”); scanf(“%d”,&c2); if (c2!=r2) { printf(“ Multiplication not possible “); exit(0); } printf (“Enter first matrix”n); for(I=0;I<r1;I++) for(j=0;j<c2;j++) { Printf(“Enter array element “); Scanf(“%d”,&a[I][j]); } printf(Enter second matrix “n) for(I=0;I<r2;I++) for(j=0;j<c2;j++) { Printf(“Enter array element “); Scanf(“%d”,&a[I][j]);
  • 7. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 7777////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work } for (I=0;I<r2,I++) for(j=0;j<c2;j++) { c[I][j]=0; for(k=0;k<r2;k++) c[I][j]+=a[I][k]*b[k][j]; } } printf (“Result of Multiplication cationn”); for(I=0;I<r1;I++) { printf(“n”); for(j=0;j<c2;j++) printf(“t%d”,c[I][j]); }} Q7. Write a program for addition of two 2-D array. Ans. #include<stdio.h> void main() { int a[2][2],b[2][2],c[2][2]; for(int i=0;i<2;i++) for(int j=0;j<2;j++) scanf("%d",&a[i][j]); for( i=0;i<2;i++) for( j=0;j<2;j++) scanf("%d",&b[i][j]); for( i=0;i<2;i++) for( j=0;j<2;j++) c[i][j]=a[i][j]+b[i][j]; for( i=0;i<2;i++) { for( j=0;j<2;j++) printf("%d ",c[i][j]); printf("n "); }} Q8. What do you mean by call by reference? Ans. In this approach the addresses of the actual arguments are passed to the called function. In this way processing is done on the actual variables. #include<stdio.h> int add(int*,int*); void main() {int a,b,c; a=10; b=20; c=add(&a,&b); printf("%d",c); } int add(int *x, int *y) { int z; z=*x+*y; return(z); }
  • 8. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 8888////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q9. Create a program to print the transpose of a matrix using function. Ans. void trans(int[ ]); void main() { int a[2][2],I,j; for(I=0;I<2;I++) { printf(“enter the values for %d row of matrix”,I+1); for(j=0;j<2;j++) { scanf(“%d”,&a[I][j]); } } trans(a); } void trans(int a[ ]) { int I,,j; for(I=0;I<2;I++) { for(j=0;j<2;j++) { printf(“t%d”,a[j][I]); } printf(“n”); } getch(); } Q10. What is a doubly linked list? Ans. A singly linked list can move only in one direction in certain applications, it is sometimes required that the list be traversed in either a forward or reverse direction. This property of a linked list implies that each mode must contain two link fields instead of one. The links are used to denote the predecessor & successor of a mode. The link devoting the predecessor & of a mode is called the left link & that devoting its successor is called the right link. A list containing this type of mode is called a doubly linked list. Q11. Discuss the advantages & disadvantages of circular linked list. Ans. Circular lists have certain advantages over singly linked list. The first advantage is that in a circular list every mode is accessible from a given mode. A second advantage is concerned with the deletion operation. As we know that in order to delete a mode in a singly linked list, it is necessary to give the address of the first mode of the list. This is necessary because we need to find the predecessor of the mode to be deleted To find the predecessor of a search is required starting from the first mode of the list. In the case of circular list, this requirement does not exist since the search for the processor of the mode to be deleted can be initiated from that mode itself. However, there is a disadvantage in using circular lists. It is possible to get into an infinite loop. In processing a circular list, it is important that we are able to detect the end of the list.
  • 9. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 9999////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q12. Write an algorithm to create a linked list & insert an item into a linked list in beginning. Ans. INFIRST (INFO, LINK, START, AVAIL, ITEM). This algorithm inserts ITEM as the first node in the list. 1. [Overflow?] If AVAIL = NULL then: Write overflow and Exit 2. Set NEW: = AVAIL 3. Set INFO [NEW]: = ITEM [Copies new data into new node. 4. Set LINK [NEW]: =START [New mode points to original first node]. 5. Set START: = NEW [Changes START so it points to the new node]. 6. Exit. Q13. Write an algorithm to insert an item into a linked list in end. Ans. INEND (INFO,TEMP, LINK, START, AVAIL, ITEM). This algorithm inserts ITEM as the END node in the list. 1. [Overflow?] If AVAIL = NULL then: Write overflow and Exit 2. Set NEW: = AVAIL 3. Set INFO [NEW]: = ITEM [Copies new data into new node. 4. Set LINK [NEW]: =NULL 5. TEMP=START 6. WHILE TEMP!=NULL TEMP=LINK[TEMP] END LOOP 7. Set TEMP = NEW . 8. Exit. Q14. Write a program to pass structure in function using pointer. Ans. #include<stdio.h> #include<conio.h> struct emp { char empname[34]; int empno; int bs; int bonus; float total; }; void print(emp*); void main() { int i; clrscr(); emp k; printf("enter"); scanf("%s%d%d",&k.empname,&k.empno,&k.bs); print(&k); } void print(emp *k) { k->bonus=k->bs/10 ; k->total=(float)k->bs+k->bonus; printf("nemployee name is t %s sal=t%f",k->empname,k->total); }
  • 10. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 10101010////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q15. Write a program to pass array in function using pointer. Ans. #include<stdio.h> #include<conio.h> void print(int*); void main() { int a[5],i; clrscr(); printf("enter numbers"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } print(&a[0]); getch(); } void print(int *p) { int i,g; g=*p; for(i=0;i<5;i++) { if(g<*p) { g=*p; } p++; } printf("%d ",g); } Q16. Write a program to implement linked list. Ans. //linked list #include<conio.h> #include<stdio.h> struct node { int info; struct node *link; }; void main() { struct node *start,*p,*l; int i,j,a=0,c,cc; clrscr(); start=NULL; printf("link list is created"); while(c!=4) { printf("nenter 1 for insert"); printf("n Enter 2 for delete"); printf("n Enter 3 for print"); printf("nEnter 4 for exit"); scanf("%d",&c); if(c==4) CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala. # 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
  • 11. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 11111111////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work { break; } if(c==1) { p=(struct node*)malloc(sizeof(struct node)); if(start==NULL) { printf("enter element"); scanf("%d",&p->info); p->link=NULL; start=p; } else { printf("nEnter 1 for insert at beg"); printf("nEnter 2 for insert at end"); scanf("%d",&cc); if(cc==1) { printf("Enter element"); scanf("%d",&p->info); p->link=start; start=p; } if(cc==2) { printf("Enter element"); scanf("%d",&p->info); p->link=NULL; l->link=start; while(l->link!=NULL) { l=l->link; } l->link=p; } } } if(c==2) { if(start==NULL) printf("no element"); else { printf("dele ele=%d",start->info); start=start->link; } } if(c==3) { printf("nElement are:->"); l=start; while(l!=NULL)
  • 12. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 12121212////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work { printf("%d",l->info); l=l->link; } } } getch(); } Q17. Write a program to implement linked list using functions. Ans. //linked list using functions #include<conio.h> #include<stdio.h> struct node { int info; struct node *link; }; void insert(struct node**); void del(struct node**); void print(struct node*); void main() { struct node *start,*p,*l; int i,j,a=0,c,cc; clrscr(); start=NULL; printf("link list is created"); while(c!=4) { printf("nenter 1 for insert"); printf("n Enter 2 for delete"); printf("n Enter 3 for print"); printf("nEnter 4 for exit"); scanf("%d",&c); if(c==4) { break; } if(c==1) { insert(&start); } if(c==2) { del(&start); } if(c==3) { print(start); } } getch(); }
  • 13. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 13131313////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work void insert(struct node **s) { struct node *p,*l; int cc; p=(struct node*)malloc(sizeof(struct node)); if(*s==NULL) { printf("enter element"); scanf("%d",&p->info); p->link=NULL; *s=p; } else { printf("nEnter 1 for insert at beg"); printf("nEnter 2 for insert at end"); scanf("%d",&cc); if(cc==1) { printf("Enter element"); scanf("%d",&p->info); p->link=*s; *s=p; } if(cc==2) { printf("Enter element"); scanf("%d",&p->info); p->link=NULL; l=*s; while(l->link!=NULL) { l=l->link; } l->link=p; } } } void del(struct node **s) { struct node *l; l=*s; if(l==NULL) printf("no element"); else { printf("dele ele=%d",l->info); *s=l->link; } } void print(struct node *s) { printf("nElement are:->"); while(s!=NULL)
  • 14. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 14141414////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work { printf("%d",s->info); s=s->link; } } Q18. Write a program to implement doubly (two–way) linked list. Ans. //doubly linked list #include<conio.h> #include<stdio.h> struct node { int info; struct node *link; struct node *prev; }; void main() { struct node *start,*p,*l,*t; int i,j,a=0,c,cc; clrscr(); start=NULL; printf("link list is created"); while(c!=4) { printf("nenter 1 for insert"); printf("n Enter 2 for delete"); printf("n Enter 3 for print"); printf("nEnter 4 for exit"); scanf("%d",&c); if(c==4) { break; } if(c==1) { p=(struct node*)malloc(sizeof(struct node)); if(start==NULL) { printf("enter element"); scanf("%d",&p->info); p->link=NULL; p->prev=NULL; start=p; } else { printf("Enter element"); scanf("%d",&p->info); p->link=start; p->prev=NULL; start->prev=p; start=p;
  • 15. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 15151515////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work } } if(c==2) { if(start==NULL) printf("no element"); else { printf("dele ele=%d",start->info); start=start->link; start->prev=NULL; } } if(c==3) { printf("nElement are:->"); l=start; while(l!=NULL) { t=l; printf("%d",l->info); l=l->link; } printf("nElementin rev order are:->"); while(t!=NULL) { printf(" %d",t->info); t=t->prev; } } } getch(); }
  • 16. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 16161616////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Section – B 2 Marks Questions [QUESTIONS 1 TO 18] [PAGE 16 TO 18] Q1. Define Stack. Ans. It is also called Last-In-First-Out system. Is a linear list in which insertions and deletions can take place only at one end, called the top. Q2. Define PUSH , POP. Ans. Push:- It is the operation to insert a element in the stack. Pop :- It is the operation to delete a element from the stack. Q3. Name Operations Performed On Stacks. Ans. • Initialization of Stack. • Push Operation. • Pop Operation. • Underflow Operation. • Overflow Operation. Q4. Write a Algorithm of Underflow Operation. Ans. Stack Empty (S) 1. if top[S] = -1 2. then return true 3. else return false Q5 Write a Algorithm of Overflow Operation. Ans. StackFull(S) 1. if top[s] is equal to last available location 2. then return true 3. else return false Q6. Write a Algorithm of Push operation. Ans. Push (S[N],TOP) 1. If TOP=N Then Write “over flow” and exit End if 2. TOP=TOP+1 3. Read S[TOP] 4. Exit Q7. Write a Algorithm of POP Operation Ans. POP(S[N]) 1. If TOP=-1 Then Write “ Underflow” and Exit End if 2. TOP=TOP -1 3. Exit Q8. Write Applications Of Stacks. Ans. • Recursion Call • Interrupt Handling • Traversing in Graph (Depth First Search) • Infix to postfix • Postfix evaluation Q9. What is time & space complexity? Ans. Time complexity is related to the performance requirements of an algorithm. Space complexity is the amount of memory it needs to run to completion.
  • 17. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 17171717////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q10. Write difference between stack and queue. Ans. Queues Queues are FIFO linear structures Queues are used for a variety of purposes and implemented in multiple ways. Stacks Stacks are LIFO structures. Stacks cannot be implemented in a variety of ways. Q11. What is Queue? Ans. Queue is also called First-In First Out system, it is a linear list in which insertions take place at one end known as rear and deletions can take place from the other end known as front. Q12. Name different types of Queue. Ans. • Simple Queue • Circular Queue • Priority Queue Q13. Write Various operations performed on Queue. Ans. Operations Performed On Queues: • Initialization of Queue • Insertion of element • Deletion of element • Overflow operation • Underflow operation Q14. Write algorithm to Insert in a queue. Ans. This operation increments the rear by one and stores the element at new position of rear. Insert(Q[n],x) 1. If rear=n Write “overflow and Exit. 2. If rear=-1 Rear=0 Front=0 Q [rear] = x and Exit 3. rear = rear+ 1 4. Q [rear] = x 5. Exit Q15. Write algorithm to delete a element from Queue. Ans. This operation will remove the first element from the queue Q. delete(Q[n],x) 1. If front=0 Write “under flow” and Exit. 2. If rear=front Rear=-1 Front=-1 Exit 3. front=front + 1 4. Exit Q16. Define Priority Queue. Ans. A priority queue is a collection of elements such that each elements has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules: 1. An element of higher priority is processed before any element of lower priority.
  • 18. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 18181818////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work 2. two elements with the same priority are processed according to the order in which they were added to the queue. Q17. Write Applications of Queues. Ans. One of the important areas to which queues can be applied is that of simulation. It is the process of forming an abstract model from a real situation in order to understand the impact of modifications & the effect of introducing various strategies on the situation. The major advantage of simulation is that it allows experimentation without modifying the real situation. Areas such as military operations are safer to simulate. Q18. What is dequeue? Ans. A dequeue also known as double ended queue. It is a link of queues in which elements can be added or removed from at either end but not in the middle. Section – B 5 Marks Questions [QUESTIONS 1 TO 16] [PAGE 18 TO 31] Q1. Explain Prefix Expression With Examples. Ans. Infix expression can be written in prefix form as + A B. Prefix form is also known as Polish Notation. To convert infix to prefix 3 levels of precedence are used Highest : Exponentiation (^) Next Highest : Multiplication (*) & Division (/) Lowest : Addition (+) & Subtraction ( - ) Eg 1 (A + B) * C (+ A B) * C * + A B C Eg2. (A + B) / (C - D) (+ AB) / (-C D) / + A B – C D Q2. Explain of Postfix Expression with Examples. Ans. Infix expression can be written in postfix form as A B +.Postfix expression also known as Reverse polish notation. To convert infix to postfix same level of precedence are used. Eg1. (A + B) * C ( A + B +)* C A B + C* Eg2. (A + B) / (C – D) ( A B +) / ( C D -) A B + C D - / Q3. Write an algorithm for transforming infix expression into postfix expression with example. Ans. POLISH (Q, P) Suppose Q is an arithmetic expression written in infix mutation. This algorithm finds the equivalent postfix expression P. 1. Push “(“ onto STACK & add) “ to the end of Q
  • 19. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 19191919////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work 2. Scan Q from left to right & repeat steps 3 to 6 for each element of Q until the stack is empty. 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator Q is encountered, then a) Repeatedly pop from STACK & add to P each operator, which has the same precedence as or higher precedence than Q. b) Add Q to STACK [End of If structure] 6. If a right parenthesis is encountered a) Repeatedly pop from STACK & add to P each operator until a left parenthesis is encountered. b) Remove the left parenthesis [Do not add the left parenthesis to P] [End of If structure] [End of step 2 Loop] 7. Exit Q4. Write a program to implement a stack. Ans. //stack #include<stdio.h> #include<conio.h> void main() { int s[5],t=-1,c,i; clrscr(); while(c!=4) { printf(“enter 1 for push n 2 for pop”); printf(“enter 3 for print 4 for exit”); scanf(“%d”,&c); if(c==1) { if(t==4) { printf(“overflow”); } else { t=t+1; printf(“enter ele”); scanf(“%d”,&s[t]); } } if(c==2) { if(t==-1) { printf(“underflow”); } else { printf(“element deleted”); t--; } } if(c==3) { for(i=0;i<=t;i++) { printf(“ %d”,s[I]); } } } getch(); }
  • 20. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 20202020////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q5. Write a program to implement a stack using functions. Ans. #include<stdio.h> #include<conio.h> struct stack { int s[5]; int t; }; void push(stack*); void pop(stack*); void print(stack*); void main() { stack i; int c; clrscr(); i.t=-1; do { printf(“enter 1 for push n 2 for pop”); printf(“enter 3 for print 4 for exit”); scanf(“%d”,&c); if(c==1) { if(i.t==4) { printf(“overflow ”); } else { push(&i); } } if(c==2) { if(i.t==-1) { printf(“ underflow”); } else { pop(&i); } } if(c==3) { print(&i); } } while(c<4); getch(); } void push(stack *p) { p->t++; printf(“enter element ”); scanf(“%d”,&p->s[p->t]); } void pop(stack *p) { printf(“deleted element=%d ” ,p->s[p->t]); p->t--; } void print(stack *p) { int i; for (i=0;i<=p->t;i++) { printf(“ %d ”,p->s[I]); } } Q6. Write a program to implement linked list as stack. Ans. #include<conio.h> #include<stdio.h> struct node { int info; struct node *link; }; void main() { struct node *start,*p,*l; int i,j,a=0,c,cc;
  • 21. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 21212121////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work clrscr(); start=NULL; printf("link list is created"); while(c!=4) { printf("nEnter 1 for push"); printf("nEnter 2 for pop"); printf("nEnter 3 for print"); printf("nEnter 4 for exit"); scanf("%d",&c); if(c==4) { break; } if(c==1) { p=(struct node*)malloc(sizeof(struct node)); if(start==NULL) { printf("enter element"); scanf("%d",&p->info); p->link=NULL; start=p; } else { printf("enter elements"); scanf("%d",&p->info); p->link=start; start=p; } } if(c==2) { if(start==NULL) printf("underflow"); else { printf("element deleted"); start=start->link; }} if(c==3) { printf("elements are"); l=start; while(l!=NULL) { printf("%d",l->info); l=l->link; } } } getch(); } CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala. # 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
  • 22. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 22222222////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q7. Explain Circular Queue. Ans. This type of queue removes the total shifting of elements in the queue. In Circular Queue there is neither a starting point nor an ending point i.e. first available location is considered as next to last available location. Working of Circular Queue is given as following: Q8. Define Underflow and Overflow operations. Ans. Underflow Operation This algorithm is called before deleting an element and return true if queue is empty else return false Queue_Empty(Q) 1.if rear [Q] = -1 2.then return true 3.else retun false Overflow Operation This algorithm is called before inserting an element to check whether a queue is full or not. This algorithm will return true if queue is full else return false. 1.if front points to next available location of rear 2.then return true
  • 23. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 23232323////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work 3.else return false Q8. Write a program to implement Queue. Ans. #include<stdio.h> #include<conio.h> void main() { int q[5],f=-1,r=-1,i,c; clrscr(); do { printf("n1 for ins n 2 for deln 3 for print n 4 for exn"); scanf("%d",&c); if(c==1) { if(r==4) { printf("nover flow"); } else if(f==-1) { f=r=0; printf("enter ele "); scanf("%d",&q[r]); } else { r=r+1; printf("enter ele "); scanf("%d",&q[r]); } } if(c==2) { if(f==-1) { printf("under flow"); } else if(f==r) { f=r=-1; } else { f=f+1; } } if(c==3) { if(f>-1) { for(i=f;i<=r;i++) { printf(" %d",q[i]); } } } } while(c<4); getch(); }
  • 24. SUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURESUBJECT: DATA STRUCTURE 24242424////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q9. Write a program to implement Queue using structure and functions. Ans. #include<stdio.h> #include<conio.h> struct queue { int s[5]; int r,f; }; void ins(queue*); void del(queue*); void print(queue*); void main() { queue i; int c; clrscr(); i.r=-1; i.f=-1; do { scanf(“%d”,&c); if(c==1) { if(i.r==4&&i.f==0||i.f==i.r+1) { printf(“overflow”); } else { ins(&i); } } if(c==2) { if(i.f==-1) { printf(" u f"); } else { del(&i); } } if(c==3) { print(&i); } } while(c<4); getch(); } void ins(queue *p) { if(p->f==-1) { p->f=0; p->r=0; scanf(“%d”,&p->s[p->r]); } else if(p->r==4) { p->r=0; scanf(“%d”,&p->s[p->r]) } else { p->r++; scanf(“%d”,&p->s[p->r]) } } void del(queue *p) { if(p->f==p->r) { p->f=-1; p->r=-1; } else if(p->f==4) { p->f=0; } else { p->f=p->f+1; } } void print(queue *p) { int i; if(p->f>-1) { if(p->f<=p->r) { for(i=p->f;i<=p->r;i++) printf(“%d”,p->s[i]); } else { for(i=p->f;i<=4;i++) printf(“%d”,p->s[i]) for(i=0;i<=p->r;i++) printf(“%d”,p->s[i]) } } }
  • 25. SUBJECT: DATA STRUCTURE 25/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q10. Write a program to implement Queue using linked list. Ans. #include<conio.h> #include<stdio.h> struct node { int info; struct node *link; }; void main() { struct node *start,*p,*l; int i,j,a=0,c,cc; clrscr(); start=NULL; printf("link list is created"); while(c!=4) { printf("nEnter 1 for insert"); printf("nEnter 2 for delete"); printf("nEnter 3 for print"); printf("nEnter 4 for exit"); scanf("%d",&c); if(c==4) { break; } if(c==1) { p= (struct node*)malloc(sizeof(struct node)); if(start==NULL) { printf("enter element"); scanf("%d",&p->info); p->link=NULL; start=p; } else { printf("enter element"); scanf("%d",&p->info); p->link=NULL; l=start; while(l->link!=NULL) { l=l->link; } l->link=p; } } if(c==2) { printf("element deleted "); start=start->link; }
  • 26. SUBJECT: DATA STRUCTURE 26/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work if(c==3) { l=start; while(l!=NULL) { printf(" %d",l->info); l=l->link; } } } getch(); } Q11. Write a program to implement Circular Queue. Ans. #include<stdio.h> #include<conio.h> void main() { int q[5],f=-1,r=-1,i,c; clrscr(); do { printf("n1 for ins n 2 for deln 3 for print n 4 for exn"); scanf("%d",&c); if(c==1) { if(r==4&&f==0||f==r+1) { printf("nover flow"); } else if(f==-1) { f=r=0; printf("enter ele "); scanf("%d",&q[r]); } else { if(r<4) { r=r+1; } else { r=0; } printf("enter ele "); scanf("%d",&q[r]); } } if(c==2) { if(f==-1) { printf("under flow");
  • 27. SUBJECT: DATA STRUCTURE 27/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work } else if(f==r) { f=r=-1; } else { if(f<4) { f=f+1; } else { f=0; } } } if(c==3) { if(f>-1) { if(r>=f) for(i=f;i<=r;i++) { printf(" %d",q[i]); } else { for(i=f;i<=4;i++) { printf(" %d",q[i]); } for(i=0;i<=r;i++) { printf(" %d",q[i]); } } } } } while(c<4); getch(); } Q12. Write an algorithm for transforming infix expression into postfix expression . Ans. POLISH (Q, P) Suppose Q is an arithmetic expression written in infix mutation. This algorithm finds the equivalent postfix expression P. 1. Push “(“ onto STACK & add) “ to the end of Q 2. Scan Q from left to right & repeat steps 3 to 6 for each element of Q until the stack is empty. 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 5. If an operator Ø is encountered, then
  • 28. SUBJECT: DATA STRUCTURE 28/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work a. Repeatedly pop from STACK & add to P each operator, which has the same precedence as or higher precedence than Ø. b. Add Ø to STACK [End of If structure] 6. If a right parenthesis is encountered a. Repeatedly pop from STACK & add to P each operator until a left parenthesis is encountered. b. Remove the left parenthesis [Do not add the left parenthesis to P] [End of If structure] [End of step 2 Loop] 7. Exit Q13. Write a program to convert infix to postfix. Ans. #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { char ie[20]; char pe[20]; char s[20]; int i,t=0,k=0,j=0; clrscr(); scanf("%s",&ie); printf("noriginal exp=%s",ie); i=strlen(ie); ie[i]=NULL; i=0; while(ie[i]!=NULL) { if(ie[i]=='+'||ie[i]=='-'||ie[i]=='*'||ie[i]=='/') { if(t==0) { s[t]=ie[i]; t++; } else { if(ie[i]=='*'||ie[i]=='/') { s[t]=ie[i]; t++; } else { while(s[t-1]=='*'||s[t-1]=='/'||s[t-1]=='+'||s[t-1]=='-') { pe[k]=s[t-1]; k++; t--; } s[t]=ie[i];
  • 29. SUBJECT: DATA STRUCTURE 29/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work t++; } } } else { pe[k]=ie[i]; k++; } i++; } t--; while(t!=-1) { pe[k++]=s[t--]; } pe[k]='0'; printf("n post exp=%s",pe); getch(); } Q14. Write algorithm for Postfix Evaluation. Ans. Following algorithm uses a stack to calculate postfix expression. Suppose P is an arithmetic expression written in postfix notation. The following algorithm, which uses a STACK to hold operands, evaluates P. This algorithm finds the VALUE of an arithmetic expression P written in postfix notation. 1. Add a right parenthesis ‘)’ at the end of P. 2. Scan P from left to right and repeat steps 3 & 4 for each element P until the ‘)’ is encountered. 3. If an operand is encountered, then: Push it into STACK 4. If an operator * is encountered, then: a. Remove the two top elements of STACK, where A is the top element and B is next-to top element. b. Evaluate B * A c. Place the result of (b) back on STACK 5. Set VALUE equal top element on STACK 6. return.
  • 30. SUBJECT: DATA STRUCTURE 30/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q15. Write a program to find result of an infix expression. Ans. #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { char ie[20]; char pe[20]; char s[20]; char *p,c; int p1,p2; int i,t=0,k=0,j=0,si[20]; clrscr(); scanf("%s",&ie); printf("infix=%s",ie); i=strlen(ie); ie[i]=NULL; i=0; while(ie[i]!=NULL) { if(ie[i]=='+'||ie[i]=='-'||ie[i]=='*'||ie[i]=='/') { if(t==0) { s[t]=ie[i]; t++; } else { if(ie[i]=='*'||ie[i]=='/') { s[t]=ie[i]; t++; } else { while(s[t-1]=='*'||s[t-1]=='/'||s[t-1]=='-'||s[t-1]=='+') { pe[k]=s[t-1]; k++; t--; } s[t]=ie[i]; t++; }}} else { pe[k]=ie[i]; k++; } i++; } t--; while(t!=-1) { pe[k++]=s[t--]; } pe[k]='0'; printf("npostfix=%s",pe); i=strlen(pe); j=0; t=-1; while(j<i) { if(pe[j]=='+'||pe[j]=='- '||pe[j]=='/'||pe[j]=='*') { p1=si[t--]; p2=si[t]; switch(pe[j]) { case '+': si[t]=p2+p1; break; case '-': si[t]=p2-p1; break; case '*': si[t]=p2*p1; break; case '/': si[t]=p2/p1; break; } } else { t++; c=pe[j]; p=&c; si[t]=atoi(p); } j++; } printf("nans=%d",si[0]); getch(); }
  • 31. SUBJECT:SUBJECT:SUBJECT:SUBJECT: DATA STRUCTUREDATA STRUCTUREDATA STRUCTUREDATA STRUCTURE 31313131////50505050 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q16. Write a program of priority queue. Ans. #include<stdio.h> #include<conio.h> void main() { int pq[10],n=0,i,c,e; clrscr(); do { printf("nenter 1 to insert 2 to delete 3 for print 4 for exit"); scanf("%d",&c); if(c==1) { if(n==10) printf("overflow"); else { if(n==0) { printf("nenter ele"); scanf("%d",&pq[n]); n=n+1; } else { i=n; printf("nenter ele"); scanf("%d",&e); while(pq[i-1]>e&&i>0) { pq[i]=pq[i-1]; i--; } pq[i]=e; n=n+1; } } } if(c==2) { if(n==0) printf("nunderflow"); else { printf("ndeleted ele=%d",pq[0]); i=0; while(i<n-1) { pq[i]=pq[i+1]; i++; } n--; } } if(c==3) { for(i=0;i<n;i++) printf("%d ",pq[i]); } }while(c!=4); } CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala. # 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
  • 32. SUBJECT: DATA STRUCTURE 32/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Section – C 2 Marks Questions [QUESTIONS 1 TO 27] [PAGE 32 TO 35] Q1. What is big O notation? Ans. The big O notation provides a theoretical measure of the time or memory required by an algorithm. It also enables the user to compare the expected run times. Q2. What are the various data qualifiers? Ans. Qualifiers are used in association with data types. Qualifiers allow you to add meaning to the data type. Data type qualifiers affect the allocation or access of data storage. Various programming languages support various qualifiers. Some of the qualifiers are discussed below. Long, signed, unsigned, const, volatile, restrict. Q3. What are various tuples in a grammar? Ans. A grammar is defined as a specification, which uses the syntactic rules of a language. It consists of a finite set of productions. Grammars are used as replacement specifiers and also to generate string from language based on a set of production rules. Formal definition: a grammar is defined by using four tuples, namely Vn, Vt, S and P. thus grammar G={Vn, Vt, S, P} Tuples Use Vn A set of mathematical symbol Vt A set of terminal symbol S The starting symbol P Finite set of production rules Q4. What does VSAM stands for. Ans. VSAM is abbreviation for Virtual Storage Access Method, a file management system used on IBM mainframe. Q5. What is a tree? Ans. A graph with no cycles is called a tree. “Tree” is one of the most important nonlinear data structures in computer algorithms. A tree structure depicts a hierarchical relationship among the nodes of the tree “Parent-child” relationship is a typical hierarchical relationship. Prakas Raju Mala SoumeAnita Gouta arth MouAmit Pankaj Sujat Ujjwa
  • 33. SUBJECT: DATA STRUCTURE 33/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q6. Give two applications of binary tree. Ans. User interface: The best example for this are file system organization and windows graphical interface. Database System: Balanced search trees are good in situation that require both sequential efficiency and random access while performing insertion and deletion. Q7. Define AVL tree. Ans. A binary search tree in which the difference of heights of the right and left sub-trees of any node is less than or equal to one is known as AVL tree. The name is derived from the names of inventors who are Adelson-Velskii and Land. Q8. What is a binary tree? Ans. Binary tree is an important class of tree data structure in which a node can have at most two children (which are sub-trees). Moreover, children (or the sub-trees) of the node of a binary tree are ordered. One child is called the “left” child and the other is called the “right” child Q9. What is binary search tree? Ans. A binary tree in which all elements in the left subtree of a node are less than the contents of root & all elements in the right subtree are greater than or equal to the contents of the root. Such a tree is called a binary search tree. Q10. What are the applications of binary trees? Ans. A binary tree is a useful data structure when two- way decisions must be made at each point in a process. eg. Suppose that we want to find all duplicates in a list of numbers. The no. of comparisons may be reduced by placing the first number in the root. Each successive node is compared to the root, if it matches, we have a duplicate of a number is lesser, we examine the left subtree, else if it is larger, we examine the right subtree. Q11. What is the level & height of a binary tree? Ans. Level: The level of any mode is equal to length of its path from root to the mode: The root of any tree has level equal to zero. Height: It is equal to one + maximum level in a tree. Q12. How a tree is represented in memory using linked organization? Ans. A tree is implemented using nodes, a node consists of information part and two address fields, one to store the address of left node and right to store the address of right node. If a node does not have left or right child then a sentinel value known as NULL value is inserted. Q13. How a tree is represented in memory using sequential organization? Ans. Under this technique an array is used. Root of the tree is stored at 1st index of the array. Left child is stored at double the index of parent node and right child is stored at double the index of parent +1. Q14. What is a heap tree?
  • 34. SUBJECT: DATA STRUCTURE 34/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Ans. In a heap tree root node is larger than its left and right node. Therefore in a heap tree root node is the largest. It is immaterial in a heap tree whether left child is smaller than right child or not. A heap tree has utility to perform heap sort which has complexity as O(log2n). Q15. What is a spanning tree? Ans. A graph with no cycles is called a tree. A cycle is formed in a graph id both the start vertex and the end vertex is the same. In other words, the path has the same first and the last nodes. A spanning tree of a graph is a sub graph that contains all the nodes without any cycles. It contains enough edges to form a tree. Various kinds of spanning tree are available. The most important is the minimum spanning tree. Q16. What does hashing mean? Ans. Hashing involves computing the address of a data item by calculating a function on the search key value. Hashing provides another method of retrieving data. Spell checkers available in modern day word processors use it. Q17. What is a strictly Binary tree? Ans. Strictly binary tree are the binary tree whose each node contains zero or two child nodes. A strictly binary tree with N leaf nodes contains 2N-1 child nodes. Q18. What is complete binary tree? Ans. Complete binary tree are strictly binary tree in which all leaf nodes are at the same depth except last level. A complete binary tree with M nodes at level L can contain 2M nodes at level L+1.Therefore a complete binary tree of depth D contains 2D nodes. Q19. Design Node of a Binary Search tree. Ans. struct node { int info; node *left, *right; }; Q20. Write different types of traversing in binary tree. Ans. Traversing (visiting all the nodes) a tree starting at node is often done in one of three orders • Preorder - node, left subtree, right subtree. • Inorder - left subtree, node, right subtree. This could be used to print a binary search tree in sorted order. • Postorder - left subtree, right subtree, node. This could be used to print an expression tree in reverse polish notation (postfix). Q21. Define Expression Tree. Ans. Normal infix notation: (7-5) * (3+(1-2)) Tree equivalent: * / /
  • 35. SUBJECT: DATA STRUCTURE 35/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work - + / / # # # - 7 5 3 / # # 1 2 Q22. Define searching. Ans. Searching is the process of finding data from a set of given data items. Types:- Linear search Binary search Q23. Define Sorting. Ans. Sorting is the process of arranging both numeric and alpha numeric data in a specific order such as ascending or descending. Types:- Selection sort Bubble sort Insertion sort Quick sort Merge sort Radix sort Heap sort Q24. What are the various operations performed on binary search tree . Ans. Basic operations are:- Insertion Searching Deletion Traversal Q25. Design node of binary search tree. Ans. struct node { char info; struct node *left; struct node *right; }; Q26. Write function in “C” language to create a tree. Ans. void create(char n) { btree=(struc node*)malloc(sizeof(struct node)); btree->info=n; btree->left=NULL; btree->right=NULL; } Q27. Write function in “C” language to traverse a tree in Inorder. Ans. void Inorder (bNode *T) { if (T !=NULL)
  • 36. SUBJECT: DATA STRUCTURE 36/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work { Inorder (T->left); printf (“%d” , Toy->data); Inorder (T->right): } return 0; }
  • 37. SUBJECT: DATA STRUCTURE 37/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Section – C 5 Marks Questions [QUESTIONS 1 TO 29] [PAGE 36 TO 48] Q1. Explain Binary search tree. Ans. Binary search trees form an important sub-class of binary trees. In an ordinary tree, the elements are not ordered in any way. A binary search tree is a binary tree which is either empty or in which the following criteria are satisfied. 1. All keys of the left sub-tree of the root are “less than” the root. 2. All keys of the right sub-tree of the root are “greater than” the root. 3. The left and right sub-trees of a binary search tree are binary search trees once again. As Shown in fig:- Q2. What is a Extended binary tree? Ans. In an extended binary tree, the special nodes are added to a binary tree to make it complete binary tree . In extended binary tree each node must contain two child. For example, let T be the following tree. The extended binary tree of T is Q3. Write an algorithm for linear search. Ans. Algorithm LINEAR (DATA, N, ITEM, LDC) Hire DATA is a linear array with N elements & ITEM is a given item of information. This algorithm finds the location LOC of ITEM in DATA, or sets LOC: = 0 if the search is unsuccessful. 1. [Insert ITEM at the end of DATA] Set DATA [N+1]: = ITEM
  • 38. SUBJECT: DATA STRUCTURE 38/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work 2. [Initialize counter]. Set LOC: =1 3. Search for ITEM] Repeat while DATA [LOC] ≠ ITEM Set LOC: LOC +1 [End of roop] 4. [Successful?] If LOC= N+1, then Set LOC: = 0 5. Exit Q4. Write PREORDER algorithms for Tree traversals. Ans. Algorithm PREORDER (T): Given a binary tree whose root mode address is given by a pointer variable PTR, this algorithm traverse the tree in preorder in a recursive manner. 1. [Process the root mode] If PTR ≠ NULL then write (DATA (Toy) ) Else write (‘Empty Tree”) Return. 2. [Process the left subtree] If LPTR≠ NULL Then call PREORDER (LPTR (7) ) 3. [Process the right subtree0 If RPTR (T) ≠ NULL Then call PREORDER (RPTR (T) ) 4. Exit Q5. Write INORDER algorithms for Tree traversals. Ans. Algorithm INORDER (T): Given a binary tree whose root mode address is given by a pointer variable T, this algorithm traverse the tree in preorder in a recursive manner. Algorithm INORDER (T): 1. [Check for empty tree] If T= NULL Then write (‘empty tree’) Return. 2. [Process the left subtree] If LPTR ≠ NULL Then call INORDER (LPTR (T)) 3. [Process the root mode] Write (DATA (T)) 4. [Process the right subtree] If RPTR ≠ NULL Then call INORDER (RPTR (T)) 5. Exit. Q6. Write POSTORDER algorithms for Tree traversals. Ans. Algorithm POSTORDER (T): Given a binary tree whose root mode address is given by a pointer variable PTR, this algorithm traverse the tree in preorder in a recursive manner. Algorithm POSTORDER (Toy) 1. [Check for empty tree] If PTR=NULL then write (‘Empty tree’) Return
  • 39. SUBJECT: DATA STRUCTURE 39/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work 2. [Process the left subtree] If LPTR (PTR) ≠ NULL Then call POSTORDER (LPTR (PTR)) 3. [Process the right subtree] If RPTR (PTR) ≠ NULL Then call POSTORDER (RPTR (PTR)) 4. [Process the root mode] Write (DATA (PTR) 5. Exit Q7. List some of Hashing methods. Ans. 1. Division method: Choose a number m larger key in k. the hash function h is defined by H(k)=k(mod m) or H(k)=k(mod m)+1 Here k(mod m) denotes the remainder where k is divided by m. the second formula is used where we want the hash address to range from 1 to m rather than from 0 to m-1. 2. Mid square method: The key k is squared. Then the hash function h is defined by H(k) =l Where l is obtained by deleting digits from both ends of k2 . We emphasize that the same position of k2 must be used for all of the keys. 3. Folding Method: The key k is partitioned into a number of parts k1,k2,………..,kr, where each part except possibly the last has the same number of digits as the required address. There the parts are added together, ignoring the last crry. That is H(k) =k1+k2+k3+…….+kr Where the leading digit carries, if any ignored. Some times for extra “milling” the even numbered parts k2,k4….are each reversed before the addition. Q8. Define String and operations performed on strings. Ans. Character type array is called string. String is a collection of characters. There are several operations, which typically arise in the context of string manipulation Few important such operations are outlined below String Operations IsEmpty (string s) This operation checks whether a string “s” is null or not StringLength (string s) This operation computes the length of a string “s” . String StringConcatenate (string s1, string s2) This operation concatenates two strings “s” and “s2” to produce another string. StringCopy (string s1, string s2) This operation copies a string “s2” to “s1”. This operation becomes useful whenever one strings to be assigned to another string. Int StringCompare (string s1, string s2) This operation compares two strings “s1” and “s2” and finds out whether they are identical. In StringSearch (string s1, s2) This operation tests whether a given string “s2” occurs in another given string “s1”. Q9. Explain Binary Search method. Ans. When the given array is sorted then a considerably better method of searching is possible. This method, known as binary search, makes a comparison between “k” and the middle element of the array. Since the array is sorted, this comparison
  • 40. SUBJECT: DATA STRUCTURE 40/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work results either in a match between “k” and the middle element of “A”, or identifying the left half or the right half of the array to which the desired element may belong. In the case when the current element is not equal to the middle element of “A” , the procedure is repeated on the half in which the desired element is likely to be present. Proceeding in this way, either the element is detected or the final division leads to half consisting of no element. In this case, it is ascertained that the array does not contain “k”. This is a very efficient method of searching because each comparison enables one to eliminate half of the elements form further consideration. Because of this basic principle of elimination, the method is also known as dichotomous search. Q10. Explain the time complexity of the average case for the bubble sort. Ans. The bubble sort algorithm has two loops: inner and outer. The outer loop i.e. must iterate as many times as the elements in the array while the inner loop iterates n times for the first time, n-1 times second time and go on decreasing. The total number of comparisons made are (n+1)+(n-2)+…+2+1=n(n-1)/2 or O(n2). The average case for the bubble sort is also n2. Q11. List different searching techniques and explain one in detail. Ans. Searching is the process of finding data from a set of given data items. Linear Search Binary Search Search trees Hash table methods Linear Search: The linear search is straightforward method of retrieving elements. This is often considered the simplest technique for searching an unordered list of elements. In linear search, the list is scanned element by element for the element to be retrieved. When an appropriate match is found, a message is displayed. A sequential search can be applied to any kind of list static or dynamic. Following are some of the properties of the linear search technique. The item to be found is called the key. The search function will return the index number (for a static list) or a pointer (for dynamic list) when item is found in the list. If the key cannot be found in the list, then not found value is returned eg. –1 for a static list or NULL for a dynamic list. Q12. Write down the binary search algorithm. Ans. Let A be an array of N elements and ele is the element to be searched in the array. Step 1. Start=0, End=N-1, Loc=-1 Step 2. Repeat Steps 3, 4 while Start<=End and Loc=-1 Step 3. Mid=(Start+End)/2 Step 4. if A[Mid]=ele then i. loc=mid ii. exit else if A[Mid]<ele then start=Mid+1 else end=mid-1 [end of If] [end of while] Step 5. Exit
  • 41. SUBJECT: DATA STRUCTURE 41/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q13. Write an algorithm to search an element from a binary search tree. Ans. Variables to be used: 1.INFO is the information field of node. 2.LEFT points to left child 3.RIGHT Points to right child 4.PAR points to parent node 5.LOC points to ITEM if exists otherwise points to NULL 6.ITEM which is to search 7.ROOT is the root of tree Algorithm: 1. If ROOT=NULL, then LOC:=NULL and PAR:=NULL 2. If ITEM=INFO[ROOT] then set LOC:=ROOT and PAR:=NULL and Return 3. If ITEM<INFO[ROOT], then Set PTR:=LEFT[ROOT] and SAVE:=ROOT Else: Set PTR:=RIGHT[ROOT] and SAVE:=ROOT 4.Repeat steps 5 and 6 while PTR≠NULL: 5.If ITEM=INFO[PTR], then: set LOC:=PTR and PAR:=SAVE and return 6.If ITEM<INFO[PTR], then: set SAVE:=PTR and PTR:=LEFT[PTR] else set SAVE:=PTR and PTR:=RIGHT[PTR] 7.set LOC:=NULL and PAR:=SAVE. 8.Exit Q14. Write an algorithm to insert an item into a heap tree. Ans. Variables to be used: 1.PAR points to parent node 2.ITEM which is to search 3.ROOT is the root of tree 4.CHILD is the child to delete N represents total number of nodes. Algorithm: 1. set N:=N+1 and PTR:=N 2. Repeat step 3 to 6 while PTR<1 3. set PAR:=PTR/2 4. If ITEM<=TREE[PAR] then Set TREE[PTR]:=ITEM, and return 5. set TREE[PTR]:=TREE[PAR] 6. set PTR:=PAR 7. set TREE[1]:=ITEM 8. Return Q15. Discuss complexity of heap sort. Ans. Complexity of heap sort can be calculated in following two phases: a) If H is a heap tree. The number of comparisons to find the appropriate place of a new element ITEM in H cannot exceed the depth of H. Since H is a complete tree, therefore its depth is bounded by log2m where m is total number of elements in H.
  • 42. SUBJECT: DATA STRUCTURE 42/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work b) If there are m elements in heap tree then there is need to place m elements and each element requires log2m comparisons. Therefore complexity is O(nlog2n). Q16. Write an algorithm for Bubble sort . Ans. Algorithm BUBBLE (DATA, N) Here DATA is an array with n elements. This algorithm sorts the elements in DATA. 1. Repeat steps 2 & 3 for k = I to N-I 2. Set PTR: = 1 [Initializes pass pointer] 3. Repeat while PTR ≤ N-K [Execute pass] a) If DATA [PTR] > DATA [PTR +1], then interchange DATA [PTR] & DATA [PTR+1] [End of If structure] b) Set PTR: = PTR+1 [End of inner loop] [End of step 1 outer loop] 4. Exit Q17. Write an algorithm for linear search. Ans. Algorithm LINEAR (DATA, N, ITEM, LoC) Hire DATA is a linear array with N elements & ITEM is a given item of information. This algorithm finds the location LOC of ITEM in DATA, or sets LOC: = 0 if the search is unsuccessful. 1. [Insert ITEM at the end of DATA] Set DATA [N+1]: = ITEM 2. [Initialize counter]. Set LOC: =1 3. Search for ITEM] Repeat while DATA [LOC] ≠ ITEM Set LOC: LOC +1 [End of roop] 4. [Successful?] If LOC= N+1, then Set LOC: = 0 5. Exit. Q18. Write an algorithm for merge sort. Ans. Algorithm Merge (A, LBound, UBound, MID value) 1. I=UBound 2. J=MIDvalue+1 3. K=LBound 4. Repeat while (I<MIDvalue) and (J<=UBound) If (A[I] < A [J]) Then L [K]= A [I] K= K+1 I=I+1 Else C [K] = A [H] J = J+1 K = K+1 5. Repeat while (IL = MIDvalue) C[K] = A[I] I = I+1 K = K+1
  • 43. SUBJECT: DATA STRUCTURE 43/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work 6. Repeat while (J L = Ubound) C [K] = A[J] J = J+1 K = K+1 7. Repeat for I = LBound, LBound + -------UBound A [I] = C[I] 8. Return
  • 44. SUBJECT: DATA STRUCTURE 44/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q19. Write a program for quick sort. Ans. #include<stdio.h> #include<conio.h> #define max 10 int partition(); void quick(); void p(); int a[max],low,high; void main() { int i; low=1; high=max; for(i=1;i<=max;i++) { printf("nEnter the number: "); scanf(“%d”,&a[i]); } quick(); getch(); } void quick(int a[],int low,int high) { int j,i; if(low < high) { j=partition(); p(); } } int partition() { int key,i,j,temp; char over='f'; key=a[low]; i=low+1; j=high; while(!over) { while(key > a[i]) { i++; } while(key < a[j]) { j--; } if(i < j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } Q20. Write a program for Merge Sort. Ans. #include<stdio.h> #include<conio.h> #define max 10 int a[max]; void simple(); void proc(); void main() { int low=0,high,i,mid; high=max-1; for(i=0;i<max;i++) { printf("nEnter the number: "); Scanf(“%d”,&a[i]); } proc(); for(i=0;i<max;i++) { printf(“%d”,a[i]); } getch(); } void proc(int a[],int low,int high) { int mid; if(low < high) { mid=(low + high)/2; } proc(); proc(); simple(); } void simple(int a[],int i,int j,int high) { int k,m,n,temp[max]; while((i <= j-1) && (j <= high)) { if(a[i] < a[j]) { temp[k]=a[i]; i++; k++; } else { temp[k]=a[j]; j++; k++; } if(i <= j-1)
  • 45. SUBJECT: DATA STRUCTURE 45/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work else { over='t'; } } temp=a[low]; a[low]=a[j]; a[j]=temp; cout<<a[low]; return j; } void p() {int j; quick(a,low,j-1); quick(a,j+1,high); } { for(m=0;m<=j-1;m++) { temp[k]=a[m]; k++; } } else { for(m=j;m<=high;m++) { temp[k]=a[m]; k++; }} } for(m=i;m<=high;m++) { a[m]=temp[k]; k++; }} Q21. Write a program of selection sort. Ans. #include<stdio.h> #include<conio.h> void main() { int a[10],i,j,t; clrscr(); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=0;i<9;i++) for(j=i+1;j<10;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } for(i=0;i<10;i++) printf(" %d",a[i]); getch();} Q22. Write a program of bubble sort. Ans. #include<stdio.h> #include<conio.h> void main() { int a[10],i,j,t; clrscr(); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=9;i>0;i--) for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } for(i=0;i<10;i++) printf(" %d",a[i]); getch(); } Q23. Write a program of insertion sort. Ans. #include<stdio.h> #include<conio.h> void main() { int a[10],i,j,t; clrscr(); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=1;i<=9;i++) {
  • 46. SUBJECT: DATA STRUCTURE 46/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work j=i-1; t=a[i]; while(t<a[j]) { a[j+1]=a[j]; j--; if(j==-1) { a[j+1]=t; break; }} a[j+1]=t; } for(i=0;i<10;i++) printf(" %d",a[i]); getch(); } Q24. Write a program of Binary search. Ans. #include<stdio.h> #include<conio.h> #include<process.h> main() { int a[20],i,n,beg,end,mid=0,item; clrscr(); printf("HOW MANY VALUES YOPU WANT TO ENTER"); scanf("%d",&n); printf("ENTER VALUES"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } beg=0; end=n-1; printf("ENTER ITEM"); scanf("%d",&item); while(beg<=mid) { mid=(beg+end)/2; if(a[mid]==item) { printf("VALUE IS FOUND AT LOCATION:%d",mid); getch(); exit(0); } if(a[mid]>item) { end=mid; } else {
  • 47. SUBJECT: DATA STRUCTURE 47/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work beg=mid; } } printf("VALUE NOT FOUND"); getch() ; } Q25. Write complexity of various sorting techniques. Ans. Bubble: O(n2 ) Insertion : O(n2 ) Selection: O(n2) Merge :O(n log(n)) Radix: o(k*n) Heap: O(n log(n)) Quick: O(n2 ) Q26. What are the various operations performed on binary search tree and design node of binary search tree and write a c function to create a tree. Ans. Basic operations are:- Insertion Searching Deletion Traversal struct node { char info; struct node *left; struct node *right; }; void create(char n) { btree=(struc node*)malloc(sizeof(struct node)); btree->info=n; btree->left=NULL; btree->right=NULL; } Q27. Write a function in “C” language to insert a element in a tree. Ans. void insert(char n) { if(btree==NULL) { create(n); return;
  • 48. SUBJECT: DATA STRUCTURE 48/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work } else { ptr=btree; while(ptr!=NULL) { if(ptr->info==n) { printf(“node already exists”); return; } else { if(ptr->info<n) { ptr->right=(struc node*)malloc(sizeof(struct node)); ptr=ptr->right; ptr->info=n; ptr->right=NULL; ptr->left=NULL; return; } else { ptr=ptr->right; continue; } } else { if(ptr->info>n) { if(ptr->left==NULL) { ptr->left=(struc node*)malloc(sizeof(struct node)); ptr=ptr->left; ptr->info=n; ptr->right=NULL; ptr->left=NULL; return; } else { ptr=ptr->left; continue; } } } } CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala.CJ SOFTECH Walia Enclave Opp Punjabi Univ Patiala. # 3058# 3058# 3058# 3058 Urban EstateUrban EstateUrban EstateUrban Estate PhasePhasePhasePhase –––– 2 Patiala2 Patiala2 Patiala2 Patiala CJSofTech.comCJSofTech.comCJSofTech.comCJSofTech.com
  • 49. SUBJECT: DATA STRUCTURE 49/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work } Q28. Write a function in “C” language to search a element in a tree. Ans. struct node* search(char n) { int f=0; ptr=NULL; while(ptr!=NULL) { if(ptr->info==n) { f=1; break; } else if(ptr->info>n) { ptr=ptr->left; continue; } else if(ptr->info<n) { ptr=ptr->right; continue; } } if(f==0) { printf(“node found”); return (ptr); } else { printf(“node not found”); return(NULL); } }
  • 50. SUBJECT: DATA STRUCTURE 50/50 Prepared By: CJ SofTech [Charanjiv Singh 98156-18658] Coaching Of C,C++,C#, Java, Blue J, VB, VB.NET, Oracle, Data Structure And Project Work Q29. Explain Binary Tree Traversing. Ans. Traversal of a binary tree is one of the most important operation required to be done on a binary tree. Many other operations involving binary tree data structure often need to traverse or walk through a given tree. Traversing is a process of visiting every node in the tree exactly once. Inorder Traversal If a tree “Toy”, is traversed in an “inorder” fashion then the left sub-tree of “T” is traversed first, then the root node of “T” is visited and then the right sub-tree of “T” is traversed. The “C” function for inorder traversal of the tree, T, is presented below. Void Inorder (bNode *T) { if (T !=NULL) { Inorder (Toy->left); printf (“%d” , Toy->data); Inorder (Toy->right): } return; } Preorder Traversal If a tree. “T” , is traversed in “preorder” fashion then the root node of “Toy” is visited first and then the left sub-tree of “T” is traversed and finally the right sub-tree of “T” is traversed. The “C” function for Preorder traversal of the tree, T, is presented below. Void Preorder (bNode *T) { If (T!= NULL) { printf (“%d” , Toy->data); Preorder (T->left); Preorder (T->right); } return; } Postorder Traversal If a tree, “T” , is traversed in “postorder” manner, then the left sub-tree of “T” is traversed first, then the right sub-tree of “T” is traversed and finally the root node of “T” is visited . The “C” function for Postorder traversal of the tree, T, is presented below. void Postorder (bNode *T) { Postorder (T->left); Postorder (T->right); printf (“%d” , T->data); } return; }