Data Structure Ans
Data Structure Ans
{3rd SEM}
• Organization of data
• Accessing methods
• Degree of associability
• Process alternatives for information
Data types are declarations for variables. This determines the type
and size of data associated with variables. Different types are
mentioned below.
Derived data types are used to store complex and huge amount of
data, they include :
• Array
• Structure
• Union
• Pointer
1- Create a new node. allocate memory for newNode and assign the
value A to it.
2- Set prev and next pointers of new node point next of A to the B of
the doubly linked list (As shown in the above figure).
3- Make new node as head node. Point prev of the first node to A
(now the Head is A and the second node is B)
5. Define polish notation –
TOTAL WEIGHT = 17
11. Briefly describe the notation of the space time trade off of algorithm;
The compiler will give an error because the program will crash if
some important data gets overwritten. Example;
#include<stdio.h>
int main()
{
int arr[2]=[1,2,5,6] //we can only store 3 values
printf("%d",arr[2]);
return 0;
}
*
+ + + +
a b b c d e e f
int main() {
char str1[20] = "C programming";
char str2[20];
// copying str1 to str2
strcpy(str2, str1);
puts(str2); // C programming
return 0;
}
OUTPUT “ C programming “
strcmp()- Compares two character strings ( str1 and str2 ) using the
standard EBCDIC collating sequence.
Eg;
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
return 0;
}
OUTPUT ” strcmp(str1, str2) = 1
strcmp(str1, str3) = 0 “
Linked lists
A linked list is a collection of data elements whose order is not given by
their physical location in memory. It consists of nodes that contain data
and link to the next node. The structure allows easiness in insertion and
deletion operations. The last nodes are linked to NULL and signify the
end of the list.
Stack
Stack, LIFO(Last In First Out) system, is a linear data structure in which
insertion(PUSH) and deletion(POP) are restricted to one endpoint called
TOP.
Queue
A queue is a FIFO(First In First Out) system, in which insertion(ENQUEUE)
can take place only at an end called REAR and deletion(DEQUEUE) can
take place only at another end called FRONT.
Trees
Some data contains a hierarchical relationship between various
elements. These data are represented in the form of a Rooted tree
graph or simply Tree. Here node A is called the root of the tree.
Graphs
Sometimes data contain relationships that are not hierarchical in nature.
This type of relationship can be expressed in the form of a graph data
structure.
#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX], n, i, j;
int num, countDel;
//delete elements
countDel = 0; for (i
= 0; i < n; i++) { if
(arr[i] == num) {
countDel++;
//shift all other elements up
for (j = i; j < n; j++) {
arr[j] = arr[j + 1];
}
}
}
if (countDel)
printf("%d found %d times and deleted successfully.", num,
countDel);
else
printf("%d not found.", num);
23. What are circular queues ? Write down functions for deleting
elements from a circular queue implemented using array.
24. What is singly linked list? Program to insert and search in SLL
A singly linked list is a type of linked list that is unidirectional, that is,
it can be traversed in only one direction from head to the last node
(tail).
#include<stdio.h>
#include<stdlib.h>
void create(int); void
search();
struct node
{
int data; struct
node *next;
};
struct node *head;
void main ()
{
int choice,item,loc;
do
{
printf("\n1.Create\n2.Search\n3.Exit\n4.Enter your choice?");
scanf("%d",&choice);
switch(choice)
{ case
1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item); break;
case 2: search();
case 3: exit(0);
break; default:
printf("\nPlease enter valid choice\n");
}
}while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else {
ptr->data = item;
ptr->next = head; head
= ptr;
printf("\nNode inserted\n");
}
}
void search()
{
struct node *ptr;
int item,i=0,flag; ptr
= head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0; } else
{ flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}
25. What are binary trees? Explain how it is represented in memory
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n); printf("Enter %d
Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i]; a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]); return
0;
}
A linear search scans one item at a time, without jumping to any item
}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{ if (b[i]
[j]==0)
continue; else
{ t[c2][1]=i;
t[c2][2]=j;
t[c2][3]=b[i][j];
c2++;
}
}
}
printf("\nThe sparse matrix form of A is:\n");
for(i=1;i<c1;i++)
{
for(j=1;j<=3;j++) printf("%d\
t",s[i][j]); printf("\n");
}
printf("\nThe sparse matrix form of B is:\n");
for(i=1;i<c2;i++)
{
for(j=1;j<=3;j++) printf("%d\
t",t[i][j]); printf("\n");
}
for(i=1;i<c1;i++)
{ for(j=1;j<c2;j+
+)
{
if ((s[i][1]==t[j][1])&&(s[i][2]==t[j][2]))
{
sum[count][1]=s[i][1];
sum[count][2]=s[i][2];
sum[count][3]=s[i][3]+t[j][3];
s[i][3]=0; t[j][3]=0;
count++;
}
}
}
for(i=1;i<c1;i++)
{ if(s[i][3]!
=0)
{
sum[count][1]=s[i][1];
sum[count][2]=s[i][2];
sum[count][3]=s[i][3]; count+
+;
}
for(i=1;i<c2;i++)
{ if(t[i][3]!
=0)
{
sum[count][1]=t[i][1];
sum[count][2]=t[i][2];
sum[count][3]=t[i][3]; count+
+;
}
}
printf("\nThe sparse matrix form of sum is:\n");
for(i=1;i<count;i++)
{
for(j=1;j<=3;j++) printf("%d\
t",sum[i][j]); printf("\n");
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
last[i][j]=0;
}
for(i=1;i<count;i++)
{
last[sum[i][1]][sum[i][2]]=sum[i][3];
}
printf("\nSum is:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++) printf("%d\
t",last[i][j]); printf("\n");
}
}
29. (a) Define circular linked list. Write algorithm to insert elements at
the middle of a circular linked list
Algorithm
Define a Node class which represents a node in the list. It has two
properties data and next which will point to the next node.
Define another class for creating the circular linked list and it has two
nodes: head and tail. Variable size stores the size of the list. It has two
methods: addInMid() and display() .
addInMid() will add the node to the middle of the list:
It first checks whether the head is null (empty list), then it will insert the
node as the head.
Both head and tail will point to the newly added node.
If the list is not empty, then we calculate size and divide it by 2 to get the
mid-point.
Define node temp that will point to head and current will point to a
node previous to temp.
Iterate through the list until the middle of the list is reached by
incrementing temp to temp.next.
The new node will be inserted after current and before temp such that
current will point to the new node and the new node will point to temp.
display() will show all the nodes present in the list.
Define a new node 'current' that will point to the head.
Print current.data till current will points to head again.
Current will point to the next node in the list in each iteration.
Insertion sort is a simple sorting algorithm that builds the final sorted
array one item at a time. It is much less efficient on large lists than
more advanced algorithms such as quicksort, heapsort, or merge
sort.
PROGRAM
// Quick sort in C
#include <stdio.h>
The algorithm begins at the root node and then it explores each
branch before backtracking. It is implemented using stacks. Often
while writing the code, we use recursion stacks to backtrack. By using
recursion we are able to take advantage of the fact that left and right
subtrees are also trees and share the same properties.
- In-Order
- Pre-Order
- Post-Order
Breadth-First Search (BFS)
This algorithm also begins at the root node and then visits all nodes
level by level. That means after the root, it traverses all the direct
children of the root. After all direct children of the root are traversed,
it moves to their children and so on. To implement BFS we use a
queue.
(b) Explain the algorithm of evaluating postfix expression
➢ Division Method.
➢ Mid Square Method.
➢ Folding Method.
➢ Multiplication Method.