Data Structures for Students
Data Structures for Students
1. Write a program to read ‘N’ numbers of elements into an array and insert an
element in specified position of that array.
Program Code:
#include<stdio.h>
#include<conio.h>
int main(){
int arr[100] = { 0 };
int i, x, pos, n;
clrscr();
printf("\n Enter Number of array elements: ");
scanf("%d",&n);
printf("\n Enter elements: ");
for (i = 0; i < n; i++)
scanf("%d",&arr[i]);
// print the original array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
printf("\nElement to be inserted: ");
scanf("%d",&x);
printf("\n Enter position to insert: ");
scanf("%d",&pos);
// increase the size by 1
n++;
// shift elements forward
for (i = n-1; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
// print the updated array
printf("\n Array after insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
getch();
return 0;
}
Output:
2. Write Programs to implement the Stack operations using an array
#include<stdio.h>
#include<conio.h>
#define SIZE 100
void push(int);
void pop();
void display();
int stack[SIZE], top = -1;
void main(){
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection, Try again.");
}
}
}
void pop(){
if(top == -1)
printf("\nStack is Empty, Deletion is not possible.");
else{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display(){
if(top == -1)
printf("\nStack is Empty");
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}
Output:
3. Write Programs to implement the Queue operations using an array.
Ans:
#include <stdio.h>
# define SIZE 100
void enqueue();
void dequeue();
void show();
int queue[SIZE];
int rear = - 1;
int front = - 1;
main(){
int ch;
while (1) {
printf("1.Enqueue Operation\n");
printf("2.Dequeue Operation\n");
printf("3.Display the Queue\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("Incorrect choice \n");
}
}
}
void enqueue(){
int item;
if (rear == SIZE - 1)
printf("Overflow \n");
else{
if (front == - 1)
front = 0;
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &item);
rear = rear + 1;
queue[rear] = item;
}
}
void dequeue(){
if (front == - 1 || front >rear){
printf("Underflow \n");
return ;
}else{
printf("Element deleted from the Queue: %d\n", queue[front]);
front = front + 1;
}
}
void show(){
int i;
if (front == - 1 || front>rear)
printf("Empty Queue \n");
else{
printf("Queue: \n");
for ( i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
Output:
4. Write Programs to implement Singly linked list
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head;
void beginsert() {
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}
void lastinsert() {
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last delete() {
struct node *ptr,*ptr1;
if(head == NULL){
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete() {
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion
\n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++){
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL){
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search() {
struct node *ptr;
int item,i=0,flag=1;
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);
break;
}
i++;
ptr = ptr -> next;
}
if(flag==1){
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
Output:
5. Write Programs to implement doubly linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main (){
int choice =0;
while(choice != 9){
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random
location\n4.Delete from Beginning\n");
printf("5.Delete from last\n6.Delete the node after the given
data\n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice){
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning(){
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
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("\nitem found at location %d ",i+1);
flag=0;
break;
} else{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
}
Output:
6. Write Programs to implement the Stack operations using Liked List.
#include <stdio.h>
#include <stdlib.h>
void pop() {
if (top == NULL) {
printf("\nStack Underflow\n");
return 0;
} else {
struct Node *temp = top;
temp->data = top->data;
top = top->next;
printf(“Element deleted id %d”, temp->data);
free(temp);
}
}
void display() {
struct Node *temp;
// Display the elements of the stack
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
printf("The stack is \n");
temp = top;
while (temp->next != NULL) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL\n\n", temp->data);
}
}
int main() {
int choice, value;
printf("\nImplementaion of Stack using Linked List\n");
while (1) {
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}
Output:
7. Write Programs to implement the Queue operations using Liked List.
#include <stdio.h>
#include <stdlib.h>
void main() {
int choice, value;
clrscr();
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
printf("Deleted element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
Output:
8. Write a program to convert a given infix expression to postfix expression using
Ans:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
void main()
{
char exp[100];
char *e, x;
clrscr();
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}
getch();
}
Output:
9. Write a program for Binary Search Tree operations and its traversals
Ans:
/*
* C program to implement the Binary Search Tree
*/
#include <stdio.h>
#include <stdlib.h>
// structure of a node
struct node
{
int data;
struct node *left;
struct node *right;
};
// function prototyping
struct node *create_node(int);
void insert(int);
struct node *delete (struct node *, int);
int search(int);
void inorder(struct node *);
void postorder();
void preorder();
struct node *smallest_node(struct node *);
struct node *largest_node(struct node *);
int get_data();
void main(){
int userChoice;
int data;
struct node* result = NULL;
while (1){
printf("\n\n------- Binary Search Tree ------\n");
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n4. Get Larger Node Data");
printf("\n5. Get smaller Node data");
printf("\n\n-- Traversals --");
printf("\n\n6. Inorder ");
printf("\n7. Post Order ");
printf("\n8. Pre Oder ");
printf("\n9. Exit");
switch(userChoice)
{
case 1:
data = get_data();
insert(data);
break;
case 2:
data = get_data();
root = delete(root, data);
break;
case 3:
data = get_data();
if (search(data) == 1){
printf("\nData was found!\n");
}
else{
printf("\nData does not found!\n");
}
break;
case 4:
result = largest_node(root);
if (result != NULL)
{
printf("\nLargest Data: %d\n", result->data);
}
break;
case 5:
result = smallest_node(root);
if (result != NULL)
{
printf("\nSmallest Data: %d\n", result->data);
}
break;
case 6:
inorder(root);
break;
case 7:
postorder(root);
break;
case 8:
preorder(root);
break;
case 9:
exit(0);
default:
printf("\n\tInvalid Choice\n");
break;
}
if (new_node == NULL){
printf("\nMemory for new node can't be allocated");
return NULL;
}
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
if (new_node != NULL){
// if the root is empty then make a new node as the root node
if (root == NULL){
root = new_node;
printf("\n* node having data %d was inserted\n", data);
return;
}
// traverse through the BST to get the correct position for insertion
while (temp != NULL){
prev = temp;
if (data > temp->data){
temp = temp->right;
}
else{
temp = temp->left;
}
}
// found the last node where the new node should insert
if (data >prev->data){
prev->right = new_node;
}
else{
prev->left = new_node;
}
Output:
10. Write a program to search an item in a given list using the following Searching
Algorithms
a. Linear Search
b. Binary Search.
/* Linear Search */
#include <stdio.h>
#include<conio.h>
int main(){
clrscr();
scanf("%d", &n);
scanf("%d", &array[i]);
scanf("%d", &num);
break;
if (i == n)
return 0;
Output:
/* Binary Search */
#include <stdio.h>
void main(){
clrscr();
scanf("%d",&n);
scanf("%d",&array[i]);
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
low = mid + 1;
break;
else
high = mid - 1;
getch();
Output:
11. Write a program for implementation of the following Sorting Algorithms
a. Bubble Sort
b. Insertion Sort
/* Bubble Sort */
#include <stdio.h>
#include<conio.h>
void main(){
clrscr();
scanf("%d", &num);
scanf("%d", &arr[i]);
temp = arr[j];
arr[j + 1] = temp;
}
}
}
printf("Sorted List: ");
for(i = 0; i < num; i++){
printf("%d ", arr[i]);
}
getch();
}
Output:
/* Insertion Sort*/
#include <stdio.h>
#include<conio.h>
void main(){
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
j = i - 1;
/* Move the elements greater than temp to one position ahead from their current position*/
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
printf("Sorted List: ");
getch();
Output:
12. Write a program for implementation of Merge Sort Algorithms
Ans:
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 22 };
int b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
b[i] = a[l1++];
else
b[i] = a[l2++];
b[i++] = a[l1++];
b[i++] = a[l2++];
a[i] = b[i];
}
int mid;
sort(low, mid);
sort(mid+1, high);
} else {
return;
void main() {
int i;
clrscr();
sort(0, max);
printf("\nList after sorting\n");
getch();
Output: