data_structure_fina Journal
data_structure_fina Journal
DATA
STRUCTURE
{TOTAL SIXTEEN SLIPS}
SUSHMITHAA PANDIDHAR
SY BBA {CA}
304: DATA STRUCTURE
1. Slip 1A Binary Search Tree Create,Insert & Delete & InOrder Traversing
Code:
#include <iostream>
using namespace std;
Node(int value) {
data = value;
left = right = nullptr;
}
};
BST() {
root = nullptr;
}
return root;
}
inOrder(root->left);
cout << root->data << " ";
inOrder(root->right);
}
return root;
}
};
int main() {
BST bst;
bst.root = bst.insert(bst.root, 50);
bst.insert(bst.root, 30);
bst.insert(bst.root, 20);
bst.insert(bst.root, 40);
bst.insert(bst.root, 70);
bst.insert(bst.root, 60);
bst.insert(bst.root, 80);
return 0;
}
Output:
void main()
{
int a[10],n,c,i,e,x;
printf("Enter the Degree of polynomial : ");
scanf("%d",&n);
printf("\n Enter the coefficient : ");
for(i=n;i>=0;i--)
{
printf("\n Enter coefficient A[%d]",i);
scanf("%d",&a[i]);
}
printf("\n Enter the polynomial : ");
for(i=n;i>=0;i--)
{
if(a[i]!=0)
printf("%dX^%d + ",a[i],i);
}
printf("%d",a[i]);
// Stack structure
struct Stack {
int top;
char items[MAX]; // Array of characters (for the string)
};
int i;
// Push all characters of the string into the stack
for (i = 0; i < strlen(str); i++) {
push(&s, str[i]);
}
// Pop all characters from the stack and put them back into the string
for (i = 0; i < strlen(str); i++) {
str[i] = pop(&s);
}
}
int main() {
char str[MAX];
return 0;
}
OUTPUT:
struct node {
int data;
struct node *next;
};
// Prototype declaration
NP create();
void disp(NP start);
NP insert(NP start, int info);
NP del(NP start, int pos);
int main() {
NP start = NULL;
int ch, pos, info;
while (1) {
printf("\n1: Create\n2: Display\n3: Insert\n4: Delete\n5: Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
start = create();
break;
case 2:
disp(start);
break;
case 3:
printf("Enter information: ");
scanf("%d", &info);
start = insert(start, info);
break;
case 4:
printf("Enter position of node to delete: ");
scanf("%d", &pos);
start = del(start, pos);
break;
case 5:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
do {
temp = getnode();
if (root == NULL) {
root = temp;
} else {
ptr = root;
while (ptr != NULL) {
if (temp->data < ptr->data) {
if (ptr->lchild == NULL) {
ptr->lchild = temp;
break;
} else {
ptr = ptr->lchild;
}
} else {
if (ptr->rchild == NULL) {
ptr->rchild = temp;
break;
} else {
ptr = ptr->rchild;
}
}
}
}
return root;
}
while (1) {
printf("\n\nMenu:\n");
printf("1: Create a BST\n");
printf("2: Inorder Traversal of BST\n");
printf("3: Postorder Traversal of BST\n");
printf("4: Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
root = create();
break;
case 2:
printf("\nInorder Traversal: ");
inorder(root);
break;
case 3:
printf("\nPostorder Traversal: ");
postorder(root);
break;
case 4:
exit(0);
default:
printf("\nInvalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
// Function to allocate memory for a new node and get data from the user
node* getnode() {
node *temp = (node *)malloc(sizeof(node));
if (temp == NULL) {
printf("Memory allocation failed!\n");
exit(1); // Exit if memory allocation fails
}
printf("\nEnter the data: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
do {
temp = getnode();
if (root == NULL) {
root = temp; // Initialize root if tree is empty
} else {
ptr = root; // Start from root to find the position
while (1) {
if (temp->data < ptr->data) {
if (ptr->lchild == NULL) {
ptr->lchild = temp; // Insert as left child
break;
} else {
ptr = ptr->lchild; // Move to left child
}
} else {
if (ptr->rchild == NULL) {
ptr->rchild = temp; // Insert as right child
break;
} else {
ptr = ptr->rchild; // Move to right child
}
}
}
}
printf("\nDo you want to add more nodes (Y/N): ");
scanf(" %c", &ch); // Read user response
} while (ch == 'Y' || ch == 'y');
return root;
}
// Main function
int main() {
int ch;
node *root = NULL;
while (1) {
printf("\n1: Create a BST.");
printf("\n2: Inorder of BST.");
printf("\n3: Preorder of BST.");
printf("\n4: Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
root = create();
break;
case 2:
printf("Inorder Traversal:");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal:");
preorder(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0; // Return success
}
OUTPUT:
struct NODE
{
int data;
struct NODE *next;
};
void display()
{
node *ptr;
for (ptr = list; ptr != NULL; ptr = ptr->next)
printf("%d->", ptr->data);
printf("NULL");
}
void main()
{
int num;
printf("\nEnter the No: ");
scanf("%d", &num);
createnumll(num);
printf("\nLinked after separating the digits of num: ");
display();
}
OUTPUT:
struct NODE {
struct NODE *lchild;
int data;
struct NODE *rchild;
};
node* getnode() {
node *temp = (node *)malloc(sizeof(node));
if (!temp) {
printf("Memory allocation failed!\n");
exit(1); // Exit if memory allocation fails
}
printf("\nEnter the data: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
node *create() {
char ch;
node *root = NULL, *temp, *ptr;
do {
temp = getnode();
if (root == NULL) {
root = temp;
} else {
ptr = root;
while (ptr != NULL) {
if (temp->data < ptr->data) {
if (ptr->lchild == NULL) {
ptr->lchild = temp;
break;
} else {
ptr = ptr->lchild;
}
} else {
if (ptr->rchild == NULL) {
ptr->rchild = temp;
break;
} else {
ptr = ptr->rchild;
}
}
}
}
printf("\nDo you want to add more nodes (Y/N): ");
scanf(" %c", &ch); // Added space before %c to ignore any newline
character
} while(ch == 'Y' || ch == 'y');
return root;
}
void postorder(node *ptr) {
if (ptr != NULL) {
postorder(ptr->lchild);
postorder(ptr->rchild);
printf(" %d", ptr->data);
}
}
int main() {
int ch;
node *root = NULL;
while (1) {
printf("\n1: Create a BST.");
printf("\n2: Preorder of BST.");
printf("\n3: Postorder of BST");
printf("\n4: Exit");
printf("\nEnter the Choice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
root = create();
break;
case 2:
printf("Preorder Traversal:");
preorder(root);
printf("\n");
break;
case 3:
printf("Postorder Traversal:");
postorder(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0; // Return statement for main
}
OUTPUT:
9. Slip 7B Create SLL and count number of nodes
#include<stdio.h>
#include<stdlib.h>
struct NODE
{
int data;
struct NODE *next;
};
temp->next=list; list=temp;
}
}
}
void display()
{
node *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next) printf("%d->",ptr->data);
printf("NULL");
}
void main()
{
int num;
printf("\nEnter the No: "); scanf("%d",&num); createnumll(num);
struct NODE {
struct NODE *lchild;
int data;
struct NODE *rchild;
};
node* getnode() {
node *temp;
temp = (node *)malloc(sizeof(node));
printf("\nEnter the data: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
node* create() {
char ch;
node *root = NULL, *temp, *ptr;
do {
temp = getnode();
if (root == NULL) {
root = temp; // The first node becomes the root
} else {
ptr = root;
while (ptr != NULL) {
if (temp->data < ptr->data) {
if (ptr->lchild == NULL) {
ptr->lchild = temp; // Insert left
break;
} else {
ptr = ptr->lchild; // Go left
}
} else {
if (ptr->rchild == NULL) {
ptr->rchild = temp; // Insert right
break;
} else {
ptr = ptr->rchild; // Go right
}
}
}
}
printf("\nDo you want to add more nodes (Y/N): ");
scanf(" %c", &ch); // Notice the space before %c to consume newline
} while (ch == 'Y' || ch == 'y');
return root;
}
int main() {
int ch, val, resp;
node *root = NULL;
while (1) {
printf("\n1: Create a BST.");
printf("\n2: Display");
printf("\n3: Search");
printf("\n4: Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
root = create();
break;
case 2:
printf("BST Elements: ");
display(root);
printf("\n");
break;
case 3:
printf("\nEnter the value to search: ");
scanf("%d", &val);
resp = search(root, val);
if (resp == 1)
printf("\nValue is found in the Tree.");
else
printf("\nValue not found in the Tree.");
break;
case 4:
exit(0);
default:
printf("\nInvalid choice. Please try again.");
}
}
11. Write a menu driven program using ‘C’ for singly linked list-
- To create linked list.
- To display linked list
- To search node in linked list.
- Insert at last position
Code:
#include<stdio.h>
#include<stdlib.h>
if(list == NULL) {
list = newNode; // If list is empty, make the new node the head
} else {
node *temp = list;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode; // Insert the node at the end of the list
}
int main() {
int choice;
do {
choice = menu();
switch(choice) {
case 1:
createLinkedList();
break;
case 2:
displayLinkedList();
break;
case 3:
searchNode();
break;
case 4:
insertAtLast();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while(choice != 5);
return 0;
}
OUTPUT:
12. Write a menu driven program using ‘C’ for Dynamic implementation
of Queue for
integers. The menu includes
- Insert
- Delete
- Display
- Exit
CODE:
#include<stdio.h>
#include<stdlib.h>
if(rear == NULL) { // If queue is empty, both front and rear point to the
new node
front = rear = newNode;
} else {
rear->next = newNode; // Link the new node to the end of the queue
rear = newNode; // Update rear to point to the last node
}
printf("Value inserted.\n");
}
// Main function
int main() {
int choice;
do {
choice = menu();
switch(choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while(choice != 4);
return 0;
}
OUTPUT:
// Main function
int main() {
int n, i;
return 0;
}
OUTPUT:
14. Write a ‘C’ program to create doubly link list and display nodes
having odd value:
#include <stdio.h>
#include <stdlib.h>
node *head = NULL; // Head pointer for the doubly linked list
if(head == NULL) {
head = newNode; // If the list is empty, newNode becomes the head
} else {
node *temp = head;
while(temp->next != NULL) {
temp = temp->next; // Traverse to the end of the list
}
temp->next = newNode; // Insert at the end
newNode->prev = temp; // Set the previous pointer
}
}
if(!foundOdd) {
printf("No odd values found.");
}
printf("\n");
}
// Main function
int main() {
int n, i, value;
return 0;
}
15. Write menu driven program using ‘C’ for Dynamic implementation
of Stack. The
menu includes following operations:
- Push
- Pop
- Display
- Exit
#include <stdio.h>
#include <stdlib.h>
if(top == NULL) { // If the stack is empty, the new node becomes the top
top = newNode;
} else {
newNode->next = top; // Link the new node to the top
top = newNode; // Update top to the new node
}
printf("Value pushed onto the stack.\n");
}
// Main function
int main() {
int choice;
do {
choice = menu();
switch(choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while(choice != 4);
return 0;
}
OUTPUT:
16. Write a ‘C’ program to create a singly linked list, reverse it and
display both the list.
#include <stdio.h>
#include <stdlib.h>
if (head == NULL) { // If the list is empty, the new node becomes the
head
head = newNode;
} else {
node *temp = head;
while (temp->next != NULL) {
temp = temp->next; // Traverse to the last node
}
temp->next = newNode; // Insert the new node at the end
}
}
// Main function
int main() {
int n, i, value;
return 0;
}