Professional Documents
Culture Documents
DSAnswers-Model QP
DSAnswers-Model QP
Definition :Data Structure is a way of organizing and storing data in a computer so that data
can be accessed efficiently. Example: Arrays, Stacks, Queues, Linked List, Trees and graphs.
Data Structures
CHAR INT FLOAT DOUBLE Linear Data Structures Non Linear Data Structures
1b) What do you mean by pattern matching? Outline the Knuth Morris Pratt(KMP)
algorithm and illustrate it to find the occurrences of the following pattern.
Pattern searching is an algorithm that involves searching for a specific sequence or placement
of characters such as strings, words, images, etc. in a given set of data.
Knuth Morris Pratt(KMP): The Knuth-Morris-Pratt (KMP) algorithm is a string searching
algorithm used to find occurrences of a word within a larger body of text.
1. Preprocessing (Building the Prefix Table):
Create a prefix table for the pattern where each value represents the length of the
longest proper prefix (which is also a suffix) for each substring.
The prefix table helps determine where to resume comparing characters when a
mismatch occurs.
2. Matching:
Compare characters of the pattern and the text.
If there's a match, move both pointers forward.
If there's a mismatch, use information from the prefix table to decide how far to
move the pattern pointer without rechecking characters that have already matched.
Now, let's use the example:
Pattern (P): ABCDABD Text (S): ABC ABCDAB ABCDABCDABDE
1. Preprocessing (Building the Prefix Table):
Prefix Table: [0, 0, 0, 0, 1, 2, 0]
2. Matching:
Start comparing the pattern with the text:
Match A, B, C.
Mismatch: D (pattern) and A (text).
Use prefix table to move the pattern pointer: PrefixTable[3] = 0, so move
back to index 0.
Continue comparing from the next character in the text.
Repeat this process until all occurrences are found.
1c)
#include<stdio.h>
#define MAX 5
int s[MAX],top=-1,ele,i;
void push()
{
if(top = = MAXSIZE-1)
{
printf(“Stack Overflow\n”);
return;
}
printf(“ enter the element to be inserted\n”);
scanf(“%d”,&ele);
top++;
s[top]=ele;
}
void pop()
{
if(top==-1)
{
printf("Stack underflow");
return;
}
printf(“element deleted is %d”,s[top]);
top--;
}
void display()
{
if(top==-1)
{
printf("Stack underflow");
return;
}
printf("Stack Contents are\n");
for(i=top;i>=0;i--)
printf("%d\n",s[i]);
}
void main()
{
int ch;
do
{
printf(“1:push 2:pop 3:display\n”);
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: delete();
break;
case 3:display();
break;
default: printf("invalid choice\n");
}
}while(ch<=3);
}
2a)
MALLOC: The malloc() function stands for memory allocation. It is a function which is used to
allocate
a block of memory dynamically. It reserves memory space of specified size and returns the
starting
address to the pointer ptr. If sufficient memory is not available then it returns null pointer
pointing to ptr.
multiple blocks of memory of same size. And initializes the content to zero.
REALLOC:
Using the realloc() function, you can add more memory size to already allocated memory. It
expands the
current block while leaving the original content as it is. realloc stands for reallocation of
memory. realloc
can also be used to reduce the size of the previously allocated memory.
free(ptr);
#include<stdio.h>
void main() {
char str1[30], str2[30];
int i;
printf("\nEnter two strings :");
gets(str1);
gets(str2);
i = 0;
while (str1[i] != '\0')
i++;
while (str2[j] != '\0')
{
str1[i] =str2[j];
i++;
j++;
}
printf("concatenated string is %s”, str1);
}
String Reverse
#include<stdio.h>
void main()
{
char str[30],rev[30];
int i=0,j=0;
printf("\nEnter strings :");
gets(str);
void insert()
{
if(rear==max-1)
{
printf("q is ful\n");
return;
}
printf("enter the ele to be inserted\n");
scanf("%d",&ele);
rear++;
q[rear]=ele;
}
void qdelete()
{
if(front>rear)
{
printf("q is empty\n");
rear=-1;
front=0;
return;
}
printf("ele deleted is %d\n",q[front]);
front++;
void qdisplay()
{
if(front>rear)
{
printf("q is empty\n");
return;
}
printf("content of the queue are\n");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("1:insert 2:delete 3:display default :exit\n");
printf("enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: qinsert();
break;
case 2: qdelete();
break;
case 3: qdisplay();
break;
case 4: exit(0);
}
}
getch();
}
NODE * first=NULL;
void insert_front()
{
NODE *nn;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the info\n");
scanf("%d",&nn->info);
nn->link=first;
first=nn;
}
void del_front()
{
NODE *temp;
if(first==NULL)
{
printf("empty list\n");
return;
}
temp=first;
printf("element deleted is %d",temp->info);
first=first->link;
free(temp);
}
void display()
{
NODE *temp;
if(first==NULL)
{
printf("list is empty");
return;
}
printf("\ncontents are\n");
for(temp=first;temp!=NULL;temp=temp->link)
printf("%d\t",temp->info);
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\nenter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_front();
break;
case 2: del_front();
break;
case 3: display();
break;
default: exit(0);
}
}
getch();
}
5b)
Concatenation of 2 list
5c)
6a)
void insert_front()
{
NODE *nn;
nn=(NODE*)malloc(sizeof(NODE));
printf("enter ssn,name,dept,designation,salary,phno\n");
scanf(" %s%s%s%s%s%s",nn->ssn,nn->name,nn->dept,nn->desg,nn->sal,nn->phno);
nn->rlink=first;
first->llink=NULL;
first=nn;
}
void delete_end()
{
NODE *temp,*prev;
if(first==NULL)
{
printf("list is empty\n");
return;
}
prev=NULL;
temp=first;
if(first->rlink==NULL)
{
first=NULL;
free(temp);
return;
}
while(temp->rlink!=NULL)
{
prev=temp;
temp=temp->rlink;
}
prev->rlink=NULL;
free(temp);
}
6b)
6C. Define the Threaded binary tree. Construct Threaded binary for the following
elements: A, B, C, D, E, F, G, H, I.
A threaded binary tree is a binary tree in which every node has a "thread" that points to
the next or previous node in the traversal order, in addition to its left and right child pointers.
This thread enables efficient traversal of the tree without using recursion or maintaining a stack.
There are two types of threaded binary trees:
1. Inorder threaded binary tree: Every node's thread points to its inorder successor.
2. Preorder threaded binary tree: Every node's thread points to its preorder successor.
Here, we'll describe an inorder threaded binary tree.
In the inorder threaded binary tree:
For a node with no right child, its thread points to its inorder successor.
For a node with a right child, its thread is NULL.
Constructing an inorder threaded binary tree for the elements A, B, C, D, E, F, G, H, I:
H
/ \
D I
/\ /
B FG
/\ \
A C E
In the above tree:
Inorder traversal: A, B, C, D, E, F, G, H, I
Threads:
Thread of A points to B
Thread of B points to C
Thread of C points to D
Thread of D points to E
Thread of E points to F
Thread of F points to G
Thread of G points to H
Thread of H points to I
Thread of I is NULL
So, the inorder threaded binary tree would have the following threads:
A
/ \
B I
/\ /
C DH
/\
E F
\
G