Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

1a.

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

Primitive Data Structures Non Primitive Data Structures

CHAR INT FLOAT DOUBLE Linear Data Structures Non Linear Data Structures

ARRAYS STACKS QUEUES TREES GRAPHS

 Primitive Data Structures


Primitive Data Structures are the basic data structures which directly operate upon machine
level instructions.
Examples for primitive Data Structures are char, int,float, double.

 Non Primitive Data Structures


Non-primitive data structures are the data structures that are derived from primitive data
structures.

 Linear Data Structures


A Linear Data Structures is a Data Structures in which all the elements are accessed in a
sequential manner or linear manner.
Examples for linear Data Structures are
Arrays: Collection of elements of same datatype.
Stacks: Last in first out data structures element added last will be deleted first.
Queues: first in first out data structures element added first will be deleted first rear end is used
for insertion and front end is used for deletion.
Linked List: Collection of nodes where each node has data field and link field, data field holds
actual information, link field holds the address of next node.

 Non Linear Data Structures


A Non Linear Data Structures is a Data Structures in which the elements are stored in the
memory in hierarchical manner.
Examples for Nonlinear Data Structures are Trees and Graphs
Trees: collection of nodes in hierarchical fashion.
Graph: collection of vertices and edges.

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.

Syntax: ptr = (cast_type *) malloc (size);

Example: ptr = (int *) malloc (50);


CALLOC: The calloc function stands for contiguous allocation. This function is used to allocate

multiple blocks of memory of same size. And initializes the content to zero.

Syntax: ptr = (cast_type *) calloc (n, size);

example: ptr = calloc(10, sizeof(int));

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.

Syntax: realloc (ptr, newsize);

Example: ptr = (char *) malloc(10);

ptr = (char *) realloc(ptr, 20);

FREE:The free() function is called to release/deallocate memory. By freing memory in the


program.

free(ptr);

2b) Comparing 2 strings


void main() {
char str1[30], str2[30];
int i;
printf("\nEnter two strings :");
gets(str1);
gets(str2);
i = 0;
while (str1[i] == str2[i] && str1[i] != '\0')
i++;
if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");
}
Concatenate 2 strings

#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);

while (str[i] != '\0')


i++;
i--;
while (i >=0)
rev[j++] =str[i--];
rev[j]=’\0’;
printf("reversed string is %s”, rev);
}
2b)
int op1,op2,res,i,top=-1,s[10],ele,n;
void push(int ele)
{
top++;
s[top]=ele;
}
int pop()
{
int ele;
ele=s[top];
top--;
return(ele);
}
void eval()
{
int e;
char postfix[20],ch;
printf("enter the postfix exp\n");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
ch=postfix[i];
if(isdigit(ch))
push(ch-'0');
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+':res=op1+op2;
break;
case '-':res=op1-op2;
break;
case '*':res=op1*op2;
break;
case '/':res=op1/op2;
break;
case '^':res=pow(op1,op2);
break;
}
push(res);
}
}
printf("result of postfix exp %d",res);
}
3a) linear Queue program
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define max 3
int q[max],ele,i;
int front=0,rear=-1;

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();
}

3b) Stack using Linked List program


typedef struct node
{
int info;
struct node *link;
}NODE;

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();
}

4a) Circular Queue program


#define MAX 3
int cq[MAX],f=0,r=-1,i,j,c=0,n,ele;
void insert()
{
if(c==MAX)
{
printf("CIRCULAR QUEUE OVERFLOW|n");
return;
}
printf("enter the value\n");
scanf("%d",&ele);
r=(r+1)%MAX;
cq[r]=ele;
c++;
}
void delete_ele()
{
if(c==0)
{ printf("CIRCULAR QUEUE UNDERFLOW|n");
return;
}
printf("element deleted is %d",cq[f]);
f=(f+1)%MAX;
c--;
}
void display()
{
j=f;
if(c ==0 )
{
printf("CIRCULAR QUEUE UNDERFLOW|n");
return;
}
printf("contents of CQ are\n");
for(i=1;i<=c;i++)
{
printf("%d\t", cq[j]);
j=(j+1)%MAX;
}
}
4b)
void add(NODE *p1,NODE *p2,int n1,int n2)
{
NODE *nn=NULL,*a,*b;
int i=0,j=0,comp;
a=p1->link->link;
b =p2->link->link;
nn=(NODE*)malloc(sizeof(NODE));
p3head=nn;
p3=nn;
p3head->link=p3head;
while(i<n1 && j<n2)
{
nn=(NODE*)malloc(sizeof(NODE));
nn->link=p3head;
p3->link=nn;
p3=nn;
if(a->xex == b->xex )
{ nn->cf = a->cf + b->cf;
nn->xex=a->xex;
nn->yex=a->yex;
nn->zex=a->zex;
a=a->link;
b=b->link;
i++;j++;
}
else if(a->xex > b->xex )
{ nn->cf=a->cf;
nn->xex=a->xex;
nn->yex=a->yex;
nn->zex=a->zex;
a=a->link;
i++;
}
else
{ nn->cf=b->cf;
nn->xex=b->xex;
nn->yex=b->yex;
nn->zex=b->zex;
b=b->link;
j++;
}
}
while(i<n1)
{
nn=(NODE*)malloc(sizeof(NODE));
nn->link=p3head;
p3->link=nn;
p3=nn;
nn->cf=a->cf;
nn->xex=a->xex;
nn->yex=a->yex;
nn->zex=a->zex;
a=a->link;
i++;
}
while(j<n2)
{
nn=(NODE*)malloc(sizeof(NODE));
nn->link=p3head;
p3->link=nn;
p3=nn;
nn->cf=b->cf;
nn->xex=b->xex;
nn->yex=b->yex;
nn->zex=b->zex;
j++;
b=b->link;
}
}
5a)

void inorder(NODE *ptr)


{
if(ptr)
{
inorder(ptr->llink);
printf("%d",ptr->info);
inorder(ptr->rlink);
}
}
void preorder(NODE *ptr)
{
if(ptr)
{
printf("%d",ptr->info);
preorder(ptr->llink);
preorder(ptr->rlink);
}
}
void postorder(NODE *ptr)
{
if(ptr)
{
postorder(ptr->llink);
postorder(ptr->rlink);
printf("%d",ptr->info);
}
}

5b)

Concatenation of 2 list

void concat(NODE* first, *second)


{
temp=first;
while(temp!=NULL)
temp=temp->link;
temp->link=second;
}

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

You might also like