Professional Documents
Culture Documents
Faculty of Engineering
MEDI-CAPS UNIVERSITY
INDORE- 453331
Data Structure
BY
Submitted to
Ms. Anusha Jain
1. Write a program to perform insertion in 1-D Array
#include<stdio.h>
int main() {
int arr[30], element, num, i, location;
printf("\nEnter no of elements :\n");
scanf("%d", &num);
for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}
printf("\nEnter the element to be inserted :\n");
scanf("%d", & element);
printf("\nEnter the location\n");
scanf("%d", &location);
for (i = num; i >= location; i--) {
arr[i] = arr[i - 1];
}
num++;
arr[location - 1] = element;
for (i = 0; i < num; i++)
printf("\n %d", arr[i]);
return (0);
}
2. Write a program to perform traversing in 1-D Array
#include <stdio.h>
void printArray(int* arr, int n)
{
int i;
printf("Array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = { 2, -1, 5, 6, 0, -3 };
int n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
return 0;
}
15. Write a program to perform insertion at the beginning in singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void createNodeList(int n);
void NodeInsertatBegin(int num);
void displayList();
int main()
{
int n,num;
printf("\n\n Linked List : Insert a new node at the beginning of a Singly Linked
List:\n");
printf("------------------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert at the beginning of the list : ");
scanf("%d", &num);
NodeInsertatBegin(num);
printf("\n Data after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void NodeInsertatBegin(int num)
{
struct node *fnNode;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num;
fnNode->nextptr = stnode;
stnode = fnNode;
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
16. Write a program to perform insertion at the end in singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void createNodeList(int n);
void NodeInsertatEnd(int num);
void displayList();
int main()
{
int n,num;
printf("\n\n Linked List : Insert a new node at the end of a Singly Linked List :\n");
printf("-------------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert at the end of the list : ");
scanf("%d", &num);
NodeInsertatEnd(num);
printf("\n Data, after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void NodeInsertatEnd(int num)
{
struct node *fnNode, *tmp;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num;
fnNode->nextptr = NULL;
tmp = stnode;
while(tmp->nextptr != NULL)
tmp = tmp->nextptr;
tmp->nextptr = fnNode;
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the empty list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
17. Write a program to perform insertion at specific location singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void insertNodeAtMiddle(int data, int position);
void displayList();
int main()
{
int n, data, position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("nEnter data to insert at middle of the list: ");
scanf("%d", &data);
printf("Enter the position to insert new node: " );
scanf("%d", &position);
insertNodeAtMiddle(data, position);
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void insertNodeAtMiddle(int data, int position)
{
int i;
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = NULL;
temp = head;
for(i=2; i<=position-1; i++)
{
temp = temp->next;
if(temp == NULL)
break;
}
if(temp != NULL)
{
newNode->next = temp->next;
temp->next = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
else
{
printf("UNABLE TO INSERT DATA AT THE GIVEN POSITION\n");
}
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
18. Write a program to perform deletion of node at the beginning.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete first node: ");
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteFirstNode()
{
struct node *toDelete;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->data);
free(toDelete);
printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
19. Write a program to perform deletion of node at the end.
#include <stdio.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data) {
struct node newNode = (struct node)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else{
tail->next = newNode;
tail = newNode;
}
}
void deleteFromEnd() {
if(head == NULL) {
printf("List is empty \n");
return;
}
else {
if(head != tail ) {
struct node *current = head;
while(current->next != tail) {
current = current->next;
}
tail = current;
tail->next = NULL;
}
else {
head = tail = NULL;
}
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);
while(head != NULL) {
deleteFromEnd();
printf("Updated List: \n");
display();
}
return 0;
}
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}
}
}
void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
21. Write a program to perform insertion, deletion and traversing in a queue using
an array.
#include <stdio.h>
#define MAX 10
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
22. Write a program to perform insertion, deletion and traversing in a circular queue
using an array.
#include <stdio.h>
#define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if ((front == 0 && rear == MAX - 1) || (front == rear + 1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item;
}
void deletion()
{
if (front == -1)
{
printf("Queue Underflow\n");
return;
}
printf("Element deleted from queue is : %d\n", cqueue_arr[front]);
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if (front_pos <= rear_pos)
while (front_pos <= rear_pos)
{
printf("%d ", cqueue_arr[front_pos]);
front_pos++;
}
else
{
while (front_pos <= MAX - 1)
{
printf("%d ", cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
printf("%d ", cqueue_arr[front_pos]);
front_pos++;
}
}
printf("n");
}
int main()
{
int choice, item;
do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Input the element for insertion in queue : \n");
scanf("%d", &item);
insert(item);
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}
} while (choice != 4);
return 0;
}
23. Write a program to perform linear search and binary search.
(a) Linear Search
#include <stdio.h>
int main()
{
int array[10], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0; }
(b) Binary Search
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[10];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
24. Write a program to perform bubble sort and selection sort.
(a) Bubble Sort
#include <stdio.h>
int main()
{
int array[10], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}