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

DATA STRUCTURE IMPP

Data Structure

Uploaded by

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

DATA STRUCTURE IMPP

Data Structure

Uploaded by

rtaru99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 30
Q1A.What is self referential structure? ANS) A self-referential structure is like when you have a box that contains another box, and that box can also contain another box, and so on. It's like a never-ending nesting of boxes! It's pretty cool, right? Q1B.What are the different types of graph? ANS) The types of graphs include: - Undirected graph: A graph where edges do not have a direction. - Directed graph: A graph where edges have a direction. - Weighted graph: A graph where edges have associated weights or values. - Connected graph: A graph where there is a path between any two vertices. - Disconnected graph: A graph where there are one or more isolated vertices or subgraphs. Q1C.What are the applications of stack? ANS) The applications of a stack are quite diverse! It's used in things like function calls, memory management, and even in undo-redo features in software! It's like a versatile tool that helps in organizing and managing data. Q1D.List out different types of tree? ANS) There are different types of trees like binary trees, AVL trees, B-trees, red-black trees, and heap trees. They each have their unique characteristics and uses. Q1E.What is searching? ANS) Searching is like looking for something specific, like finding a particular book on a shelf. It's used to find data ina when you have a box that contains another box, and that box can also contain another box, and so on. It's like a never-ending nesting of boxes! It's pretty cool, right? Q1F.What is pointer to pointer.? ANS) A pointer to a pointer is like having a sticky note that points to another sticky note that points to an actual object. It's used in programming to indirectly access and modify data. Q1G.What is non-primitive data structure? ANS) A non-primitive data structure is like a complex way to store and organize data, using multiple simple data types or other data structures. Q1H.Define Data structure.? ANS) Ah, data structure! It's like a way to store and organize data in a computer's memory, making it easier to access and manipulate. Q11.What is sorting? State the techniques of sorting.? ANS) Sorting is like arranging a list of things in a specific order, like sorting a deck of cards from smallest to largest. Techniques of sorting. Q1J.What is almost complete binary tree.? ANS)An almost complete binary tree is a binary tree in which all levels are completely filled, except possibly the last level, which is filled from left to right. In other words, an almost complete binary tree is a tree that is almost complete, with the exception of the rightmost elements on the last level. Q1K.i) Leaf node: In a tree or graph, a leaf node is a node that does not have any children. It is also known as a terminal node or a external node. Q1L.Cyclic graph: A cyclic graph is a graph that contains one or more cycles, which are paths that start and end at the same vertex. Q1M. Parent node:In a tree or graph, a parent node is a node that is connected to one or more child nodes. It is also known as an internal node. Q1. Directed Graph? :-A directed graph, also known as a digraph, is a type of graph where the edges have a specific direction associated with them. In a directed graph, each edge has an origin vertex and a destination vertex. The direction of the edge indicates the relationship between the vertices. For example, if there is an edge from vertex A to vertex B, it means that there is a directed connection from A to B, but not necessarily from B to A. Q1.Strict Binary Tree? A strict binary tree is a type of binary tree where each node has at most two children. In a strict binary tree, every node can have either zero, one, or two children. This means that each node has either no children (a leaf node), one left child, one right child, or both a left and right child. There are no nodes with only one child in a strict binary tree. QiN. How to measure performance of an algorithm? To measure the performance of algorithm, we usually consider two factors: time complexity and space complexity. Time complexity refers to how long an algorithm takes to run, while space complexity refers to how much memory or space the algorithm requires. Q10. What is polynomial? How is it differ from structure? ANS) A polynomial is an algebraic expression consisting of variables, coefficients, and exponents, where the variables are raised to non-negative integer powers. It represents a mathematical function. On the other hand, a structure is a user-defined data type that allows you to combine different types of variables under one name. Q1P.What is balance factor? How is it calculated? ANS) The balance factor is a concept used in balanced binary trees, such as AVL trees. It measures the difference in height between the left and right subtrees of a node. It is calculated as the height of the left subtree minus the height of the right subtree. Q1Q.What are Abstract Data types? ANS) Abstract Data Types (ADTs) are high-level data types that define a set of operations without specifying how those operations are implemented. They provide a way to abstract the behavior of data and allow us to focus on what can be done with the data, rather than how it is represented. Q1R.What is Ancestor of Node? ANS) An ancestor of a node in a tree or graph is any node that lies on the path from the root of the tree/graph to that node. It is located higher (closer to the root) in the hierarchy than the given node. Q1S.Differentiate array and structure.? Ans) Arrays and structures are both ways to organize and store data, but they have some differences. An array is a data type that can store elements of different types under one name. Structures allow for more flexibility in organizing and accessing data. Q1T. What is space and time complexity? ANS) Space complexity refers to the amount of memory or space required by an algorithm to solve a problem. Time complexity refers to the amount of time taken by an algorithm to run, usually measured in terms of the number of operations performed. Q1U.What is spanning tree? ANS)A spanning tree is a tree-like subgraph that includes all the vertices of a connected graph, without any cycles. It is formed by selecting a subset of edges from the original graph, such that all the vertices are connected and there are no loops. Spanning trees are useful in various applications, such as network design and optimization. Q2A. What is a height-balanced tree, and why is it important? Explain LL and LR rotations with an example.? Ans)A height-balanced tree is a binary tree in which the height of the two child subtrees of every node differs by at most one, ensuring efficient search and insertion operations. **LL Rotation (Left-Left Rotation):** In LL rotation, if a tree becomes unbalanced with a node having a left child with a right-heavy subtree, a rotation is performed to restore balance. Example: A / B \ C After LL Rotation B /\ AC *“*LR Rotation (Left-Right Rotation):** In LR rotation, if a tree becomes unbalanced with a node having a left child with a left-heavy subtree, a rotation is performed. Example: A \ B / Cc After LR Rotation: B /\ AC Q2B. Describe the selection sort technique. Provide an example to illustrate its steps.? Selection sort is a simple sorting algorithm that repeatedly finds the minimum element from the unsorted part of the array and swaps it with the first unsorted element. * Example: Array: 64 25 12 22 11 11 25 12 22 64 1112 25 22 64 11 12 22 25 64 11 12 22 25 64 Q2C Define a stack and elaborate on the different operations used ina stack.? A stack is a data structure that follows the Last In, First Out (LIFO) principle. “Stack Operations:** 1. Push: Adds an item to the stack. 2. Pop: Removes the top item from the stack. 3. Peek or Top:Returns the top item without removing it. 4. isEmpty:Checks if the stack is empty. Q2D. What is a Graph, and how is it represented using an adjacency list? Ans) A graph is a collection of nodes (vertices) and edges connecting these nodes. **Adjacency List:** It represents a graph as an array of lists, where each list represents the neighbors of a vertex. * Example: 0:1,2 :0,2 -0,1 Q2E.Present an algorithm for converting a given infix expression to postfix expression. Explain the steps involved with an example.? ANS) Algorithm: 1. Initialize an empty stack and an empty output string. 2. Scan the infix expression from left to right. 3. If an operand is encountered, append it to the output string. 4. lf an operator is encountered, pop operators from the stack and append them to the output until the stack is empty or the top has lower precedence. Push the current operator onto the stack. 5. If an opening parenthesis is encountered, push it onto the stack. 6. If a closing parenthesis is encountered, pop operators from the stack and append them to the output until an opening parenthesis is encountered (not added to the output). Pop the opening parenthesis from the stack. 7. Repeat steps 3-6 until the infix expression is scanned. 8. Pop any remaining operators from the stack and append them to the output. 9. The output string is the postfix expression. * Example: Infix Expression: “A + B * (C - D) Postfix Expression: “ABC D-* + Q3A.Write a function to create and display circular singly linked list.? #Hinclude Q3A.Write a function to create and display circular singly linked list.? #include #include struct Node { int data; struct Node* next; } struct Node* createCircularLinkedList(int elements{], int size) { if (size == 0) { return NULL; } struct Node* head = (struct Node*)malloc(sizeof(struct Node)); head->data = elements[0]; head->next = NULL; struct Node* current = head; for (int i = 1; i < size; i++) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = elements[i]; new_node->next = NULL; current->next = new_node; current = current->next; } current->next = head; // Make the last node point to the head to form a circular list return head; * void displayCircularLinkedList(struct Node head) { if (head == NULL) { return; } struct Node* current = head; do { printf("%d ", current->data); current = current->next; } while (current != head); int main() { int elements[] = {1, 2, 3, 4, 5}; int size = sizeof(elements) / sizeof(elements[0]); struct Node* head = createCircularLinkedList(elements, size); displayCircularLinkedList(head); return 0; Q3B.Wirte a function a dynamic implementation of a stack .? #include #include struct Stack { int* array; int top;int capacity; hi struct Stack* createStack(int capacity) { struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); stack->capacity = capacity; stack->top = -1; stack->array = (int*)malloc(stack->capacity * sizeof(int)); return stack; int isFull(struct Stack* stack) { return stack->top == stack->capacity - 1; } int isEmpty(struct Stack* stack) { return stack->top == -1; } void push(struct Stack* stack, int item) { if (isFull(stack)) { printf("Stack overflow!\n"); return; } stack->array[++stack->top] = item; printf("%d pushed to the stack.\n", item); } int pop(struct Stack* stack) { if (isEmpty(stack)) { printf("Stack underflow!\n"); return -1; return stack->array[stack->top--]; int neek(ctriict Stack* ctack) f int peek(struct Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty!\n"); return -1; return stack->array[stack->top]; } int main() { struct Stack* stack = createStack(5); push(stack, 1); push(stack, 2); push(stack, 3); printf("Top element: %d\n", peek(stack)); printf("Popped element: %od\n", pop(stack)); printf("Popped element: %d\n", pop(stack)); printf("Top element: %d\n", peek(stack)); return 0; } Q3C. Write a function to traverse a graph using DFS technique.? void addEdge(struct Graph* graph, int src, int dest) { struct Node* newNode = createNode(dest); newNode->next = graph->adjLists[src]; graph->adjLists[src] = newNode; newNode = createNode(src); newNode->next = graph->adjLists[dest]; graph->adjLists[dest] = newNode; } void DFS(struct Graph* graph, int vertex) { struct Node* adjList = graph->adjLists[vertex]; struct Node* temp = adjList; graph->visited[vertex] = 1; printf("Visited %d\n", vertex); while (temp != NULL) { int connectedVertex = temp->data; if (graph->visited[connectedVertex] == ) { DFS(graph, connectedVertex); } temp = temp->next; } } Q3D.Write a function to remove given node from singly linked list and add it? at the given position in singly linked list.? void insertAtPosition(struct Node** head, int position, struct Node* newNode) { if (position == 0) { newNode->next = *head; *head = newNode; } else { struct Node* current = *head; int currentPosition = 0; while (current != NULL && currentPosition < position - 1) { current = current->next; currentPosition++; } if (current == NULL) { printf("Invalid position\n"); return; } newNode->next = current->next; current->next = newNode; }} void removeNode(struct Node** head, struct Node* nodeToRemove) { if (“head == NULL) { printf("Empty list\n"); return} if *Head == nodeToRemove) { *head = nodeToRemove->next; free(nodeToRemove); return;} struct Node* current = *head; while (current->next != NULL && current->next != nodeToRemove) { current = current->next; } if (Current->next == NULL) { printf("Node not found\n"); return; } current->next = nodeToRemove->next; free(nodeToRemove);} Q3E.Write a function to check whether a given string is palindrome or not? #include #include #include #define size 15 char stk[size]; int top=-1; void push(char); char pop(); void is(char []); void main() { char str1 [size]; clrscr(); printf("enter a string "); scanf("%s",str1); is(str1); getch(); } void push(char s) { topt++; stk[top]=s; } char pop() { char ch; ch=stk[top]; top--; return ch;} void is(char str1[]) { inti; int len=strlen(str1); for(i=0;i #include #include struct node { int data; struct node*next; } struct node*temp,”*newnode,*head; struct node*create() { char ch; newnode=head=temp=NULL; dof newnode=(struct node*)malloc(sizeof(struct node)); newnode->next=NULL; printf("\nenter data:"); scanf("%d",&newnode->data); if(head==NULL) { temp=head=newnode; } else { temp->next=newnode; temp=temp->next; } printf("\n you can continue"); fflush(stdin); ch=getche(); while(ch=='y’); return head; } void display(struct node*head) { struct node*temp=head; while(temp!=NULL) { printf(" %d",temp->data); temp=temp->next; } void countnode(struct node*temp) { int cnt=0; while(temp!=NULL) { cnt++; temp=temp->next; } printf("number of total element is:%d\n",cnt); } int main() { int ch; struct node*head=NULL; dof printf("\n1.createnode\n2.dispaly\n3.count node\n"); scanf("%d",&ch); switch(ch) { case 1: head=create(); break; case 2: display(head); break; case 3: countnode(head); break; } while(ch!=0); getch();} Q2A. Explain Insertion sort technique with an example.? ans) Insertion sort is a simple sorting technique where the array is divided into two parts: the sorted part and the unsorted part. Initially, the sorted part is empty, and we start picking elements from the unsorted part and inserting them into the correct position in the sorted part. Let me show you an example: Suppose we have an array [5, 2, 4, 6, 1, 3]. We start with the first element and consider it as the sorted part. Then, we take the next element (2) and compare it with the elements in the sorted part. Since 2 is smaller than 5, we shift 5 to the right and insert 2 at the correct position. Now, the sorted part becomes [2, 5]. We repeat this process for the remaining elements until the array is sorted. Q2B.What is ircular queue? How it is differ from static queue? Ans) A circular queue is a data structure that follows the FIFO (First-In-First-Out) principle, where the last position is connected to the first position to form a circular structure. This allows for efficient utilization of memory. Unlike a static queue, a circular queue can dynamically adjust its size and wrap around to the beginning when the end is reached. Q2C.What is stack? What are the various applications of stack. List operations performed on stack.? A stack is a linear data structure that follows the LIFO principle, where the last element inserted is the first one to be removed. It is like a stack of plates, where we can only remove or add items from the top.Some applications of stacks include: - Function call stack in programming languages. - Undo/Redo operations in text editors. - Evaluating arithmetic expressions. - Browser history functionality. Operations performed on a stack include: - Push: Adds an element to the top of the stack. - Pop: Removes the top element from the stack. - Peek/Top: Returns the top element without removing it. - isEmpty: Checks if the stack is empty. - isFull: Checks if the stack is full (in case of a fixed-size stack). Q2D.Explain different types of AVL rotations with an example? 1. Left Rotation: In a left rotation, we pivot the tree around the right child of a node, promoting the right child to become the new root. This helps in maintaining the balance of the AVL tree.example: Before rotation: A \ B \ Cc After left rotation: ) /\ AC 2. Right Rotation: In a right rotation, we pivot the tree around the left child of a node, promoting the left child to become the new root. This also helps in maintaining balance.example: Before rotation: Cc / B / A After right rotation: B /\ AC 3. Left-Right Rotation: A left-right rotation le QA annmbhbinatinn nf 4 latt ratatinn fallaywsad 3. Left-Right Rotation: A left-right rotation is a combination of a left rotation followed by a right rotation. It helps in balancing the tree when the imbalance occurs on the left side. example: Before rotation: A \ C / B After left-right rotation: ) /\ AC 4. Right-Left Rotation: A right-left rotation is a combination of a right rotation followed by a left rotation. It helps in balancing the tree when the imbalance occurs on the right side. Here's an example: Before rotation: A \ C \ B After right-left rotation: B /\ AC These rotations ensure that the AVL tree remains balanced, which results in efficient search operations. Q2E. Explain various types of Dynamic Memory Allocation functions? Ans) various types of dynamic memory allocation functions. 1. ‘malloc()*: It stands for "memory allocate" and is used to allocate a block of memory of a specified size. It returns a pointer to the starting address of the allocated memory. 2. calloc()’: It stands for "contiguous allocation" and is used to allocate a block of memory for an array of elements, initializing all the bytes to zero. It takes two arguments, the number of elements and the size of each element. 3. ‘realloc()*: It stands for "reallocate" and is used to modify the size of a previously allocated block of memory. It can be used to both expand or shrink the memory block. 4. ‘free()': It is used to deallocate the memory allocated using ‘malloc()’, ‘calloc()’, or ‘realloc()’. It releases the memory back to the system for future use. These dynamic memory allocation functions provide flexibility and efficiency in managing memory during program execution. Q3A.Write a function to create and display doubly link list.? Struct node * create() { char ch='y'; start=temp=newnode=NULL; while(ch=='y') newnode=(struct node *) malloc(size of(struct node)); newnode->prev=NULL; newnode->next=NULL; printf("enter no:"); scanf("%d",&newnode->data); if(temp==NULL) { start=temp=newnode; } else { temp->next=newnode; newnode->prev=temp; temp=temp->next; printf("want to continue:\n"); ch=getche(); } return(start); void display(struct node * first) { Struct node * temp=first; printf("\ngiven list is:\n"); while(temp!=NULL) { printf("%d\n" temp->data); temp=temp->next; \} Q3B) Write a recursive functions to traverse a tree by using inorder (), preorder () and postorder traversing functions.? void inorder(tptr root) { totr temp = root ; if (temp!=NULL) { inorder(temp->left); printf("%d ",temp->data); inorder(temp->right); } } void preorder(tptr root) { totr temp = root ; if (temp!=NULL) { printf("%d ",temp->data); preorder(temp->left); preorder(temp->right); } void postorder(tptr root) { totr temp = root ; if (temp!=NULL) postorder(temp->left); postorder(temp->right); printf("%od ",temp->data); } Q3C.Single link list Program.? #include #include struct node int data; struct node “prev; struct node * next; ). Q3C.Single link list Program.? #include #include struct node { int data; struct node *prev; struct node * next; }; struct node * head,*temp,*newnode; struct node * create() { char ch; head=temp=newnode=NULL; dof newnode=(struct node *) malloc(sizeof(struct node)); newnode->prev=NULL; newnode->next=NULL; printf("enter no:"); scanf("%d",&newnode->data); if(temp==NULL) { head=temp=newnocde; } else { temp->next=newnode; newnode->prev=temp; temp=temp->next; printf("want to continue:\n"); ch=getche(); }while(ch=='y'); return head; } void display(struct node * start) void display(struct node * start) { struct node * temp=start; printf("\ngiven head is:\n"); while(temp!=NULL) { printf("%d\n",temp->data); temp=temp->next; }} void search(struct node *head) { struct node * temp=head; int cnt=1; int value; printf("“enter value"): scanf("%d",&value); while(temp!=NULL) { if(temp->data==value) { printf("\nposition is:"); printf("Yod\n\n",cnt); } cnt=cnt+1; temp=temp->next; } } struct node * insert(struct node * start) { struct node * newnode,*temp=start; int pos,x,i; printf("\nenter the data to be inserted:"): scanf("%d",&x); printf("enter position:"); scanf("%d",&pos); newnode=(struct node *) malloc(sizeof(struct node)); newnode->data=x; newnode->prev=NULL; newnode->next=NULL; if(pos==1) { start->prev=newnode; newnode->next=start; start=newnode; } else else { for(i=1;inext; newnode->next=temp->next; temp->next=newnode; newnode->prev=temp; } return start; } struct node * delete(struct node * start) { struct node * temp,*temp1; int p,i; printf("enter position"); scanf("%d",&p); if(p==1) { temp=start; start=start->next; free(temp); \else { temp=start; for(i=1;inext; } if(temp!=NULL) { temp1=temp->next; temp->next=temp1->next; free(temp1); }} return(start); } void sort(struct node * head) { struct node * t1,*t2,*t3; int x; for(t1=head;t1!=NULL;t1=t1->next) { for(t2=t1->next;t2!=]NULL;t2=t2->next) { if(t1->data>t2->data) { x=t1->data; t1->data=t2->data; t2->data=x; y}}} int countnode(struct node * start) { int cnt=0; struct node *temp=start; while(temp!=NULL) { temp=temp->next; cnt++; } return cnt; void revhead(struct node * start) { struct node * p=start,*q=start; int t; while(q->next!=NULL) {q=q->next;} while(pdata; p->data=q->data; q->data=t; p=p->next; q=q-=prev, }} void reverseprint(struct node * start) { temp=start; while(temp->next!=NULL) {temp=temp->next;} while(temp!=NULL) printf("%d\n" temp->data); temp=temp->prev; }} main() { int ch; struct node * head; clrscr(); head=temp=newnode=NULL; while(1) while(1) { printf("1.create\n2.display\n3.insert\n4.sear ch\n5.delete\n6.sort\n7.countnode\n8.revh ead\n9.reverseprint\n"); printf("enter no for ch:"); scanf("%d",&ch); switch(ch) { case 0: exit(0); break; case 1: head=create(); break; case 2: display(head); break; case 3: head=insert(head); break; case 4: search(head); break; case 5: head=delete(head); break; case 6: sort(head); break; case 7: printf("Y%d\n\n", countnode(head)); break; case 8: revhead(head); break; case 9: reverseprint(head); }}} Q3D. Write a function to reverse a string using stack.? #include #include #include #define size 15 char stk[size]; int top = -1; void push(char); char pop(); void is(char []); void main() { char str1[size]; printf("Enter a string: "); gets(str1); is(str1); getch(); } void push(char s) { top++; stk[top] =; } char pop() { char ch; ch = stk[top]; top--; return ch; void is(char str1[]) { inti; char str2[size]; for (i = 0; str1[i] != \O'; i++) { push(str1[i]); for (i = 0; top != -1; i++) { str2[i] = pop() } str2[i] = '\0'; printf("%s", str2); }

You might also like