Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views36 pages

DSU Programs

Dsu program
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views36 pages

DSU Programs

Dsu program
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

DSU Programs

1) Array Operation

#include <stdio.h>
#include <string.h>

int n = 5;
int a[50];

void insert()
{
int val,pos;
printf("Enter the value to be inserted: ");
scanf("%d", &val);
printf("Enter position to insert : ");
scanf("%d" , &pos);
a[pos] = val;
}

void delete()
{
int pos;
printf("Enter position to delete : ");
scanf("%d" , &pos);
a[pos] = 0;
}

void update()
{
int val,pos;
printf("Enter the value to be updated: ");
scanf("%d", &val);
printf("Enter position to update : ");
scanf("%d" , &pos);
a[pos] = val;
}

void search()
{
int key , i , flag = 0 ;
printf("Enter the value to be searched: ");
scanf("%d", &key);
for(i=0;i<n;i++)
{
if(key == a[i])
{
printf("Element found at %d", i);
flag = 1;
break;
}
}

if(flag == 0)
{
printf("Element not found");
}
}

void display()
{
int i;
for(i=0;i<n;i++)
printf("%d ", a[i]);

}
int main() {

int ch;

while (ch != 9) {
printf("\n 1. Insert \n 2. Delete \n 3. Update \n 4. Search \n 5. "
"Display \n "
"9. Exit \n");
printf("Enter choice : ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
delete();
break;

case 3:
update();
break;

case 4:
search();
break;
case 5:
display();
break;
case 9:
break;
}
}

return 0;
}

2) Linear Search

#include <stdio.h>
#include <string.h>

int main()
{

int key, i,a[50], n,flag = 0;

printf("Enter Size : ");


scanf("%d", &n);

printf("Enter Array : ");


for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter Key : ");
scanf("%d", &key);

for (i = 0; i < n;i++)


{
if(a[i] == key)
{
printf("Key found at index %d", i);
flag = 1;
break;
}
}
if(flag == 0)
{
printf("Key not found ");
}

return 0;
}
3) Binary search

#include <stdio.h>
#include <string.h>

int main()
{
int key, i, mid, top = 0, bottom, a[50], n,flag = 0;
printf("Enter Size : ");
scanf("%d", &n);
bottom = n - 1;

printf("Enter Array : ");


for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter Key : ");
scanf("%d", &key);

while(top <= bottom)


{
mid = (top + bottom) / 2;

if(a[mid] == key)
{
printf("Key found at index %d", mid);
flag = 1;
break;
}

if(key < a[mid])


{
bottom = mid - 1;
}else{
top = mid + 1;
}
}
if(flag == 0)
{
printf("Key not found ");
}
return 0;
}
4) Bubble Sort

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

int i,j,k,temp,n,a[50];

printf("Enter Size : ");


scanf("%d", &n);

printf("Enter Array : ");


for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

for (i = 0; i < n - 1;i++)


{
for (j = 0; j < n - 1;j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}

for (i = 0; i < n; i++)


{
printf("%d\t", a[i]);
}

return 0;
}
5) Insertion sort

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

int i,j,k,temp,n,a[50];

printf("Enter Size : ");


scanf("%d", &n);

printf("Enter Array : ");


for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

for (i = 1; i < n;i++)


{
temp = a[i];
k = i - 1;

while(k>= 0 && a[k] > temp)


{
a[k + 1] = a[k];
k--;
}

a[k + 1] = temp;
}

for (i = 0; i < n; i++)


{
printf("%d\t", a[i]);
}

return 0;
}
6) Selection Sort

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

int i, j, k, temp, n, a[50];

printf("Enter Size : ");


scanf("%d", &n);

printf("Enter Array : ");


for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

for (i = 0; i < n; i++)


{
for (j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

for (i = 0; i < n; i++)


{
printf("%d\t", a[i]);
}

return 0;
}
7) Stack

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int n, a[50], top = -1;

void push()
{
int val;
if (top == n - 1)
{
printf("\nStack Overflow!");
}
else
{
printf("\nEnter Element : ");
scanf("%d", &val);
top++;
a[top] = val;
}
}

void pop()
{
if (top == -1)
{
printf("\nStack Underflow!");
}
else
{
printf("\nDeleted Element : %d", a[top]);
top--;
}
}

void display()
{
int i;
if (top == -1)
{
printf("Stack Underflow!");
}
else
{
for (i = top; i >= 0; i--)
{
printf("%d\n", a[i]);
}
}
}

void peek()
{
if (top == -1)
{
printf("Stack Underflow!");
}
else
{
printf("\nTop : %d", a[top]);
}
}

int main()
{
int option = 0;
printf("\n Enter Size of Stack : ");
scanf("%d", &n);

while (option != 5)
{

printf("\nChoose one from the below options...\n");


printf("\n1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit");
printf("\n Enter your option \n");
scanf("%d", &option);
switch (option)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
case 5:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid option");
}
}
}

return 0;
}
8) Linear Queue
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int n, a[50], front = -1, rear = -1;

void enqueue()
{
int val;
if (rear == n - 1)
{
printf("\nQueue Full!");
}
else
{
printf("\nEnter Element : ");
scanf("%d", &val);
rear++;
a[rear] = val;
}

if (rear == 0)
front++;
}

void dequeue()
{
int val;
val = a[front];
if (front == -1)
{
printf("\nQueue Empty!");
}
else if (front == rear)
{
printf("\nDeleted Element : %d\n", val);
front = -1;
rear = -1;
}
else
{
printf("\nDeleted Element : %d\n", val);
front++;
}
}

void display()
{
int i;
if (front == -1)
{
printf("Queue Empty!");
}
else
{
printf("\n Elements in the queue are\n");
for (i = front; i <= rear; i++)
{
printf("%d\n", a[i]);
}
}
}

void peek()
{
if (front == -1)
{
printf("Queue Empty!");
}
else
{
printf("\nFront : %d", a[front]);
}
}

int main()
{
int option = 0;
printf("\n Enter Size of Queue : ");
scanf("%d", &n);

while (option != 5)
{

printf("\nChoose one from the below options...\n");


printf("\n1.Enqueue\n2.Dequeue\n3.Peek\n4.Display\n5.Exit");
printf("\n Enter your option \n");
scanf("%d", &option);
switch (option)
{
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
case 5:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid option");
}
}
}

return 0;
}

9) Circular Queue
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int n, a[50], front = -1, rear = -1;

void enqueue()
{
int val;
if (front == -1 && rear == -1)
{
front++;
rear++;
printf("\nEnter Element : ");
scanf("%d", &val);
a[rear] = val;
}
else if ((rear + 1) % n == front)
{
printf("\nQueue Full!");
}
else
{
printf("\nEnter Element : ");
scanf("%d", &val);
rear = (rear + 1) % n;
a[rear] = val;
}
}

void dequeue()
{
int val;
val = a[front];
if (front == -1)
{
printf("\nQueue Empty!");
}
else if (front == rear)
{
printf("\nDeleted Element : %d\n", val);
front = -1;
rear = -1;
}
else
{
printf("\nDeleted Element : %d\n", val);
front = (front + 1) % n;
}
}

void display()
{
int i = front;
if (front == -1)
{
printf("Queue Empty!");
}
else
{
printf("\n Elements in the queue are\n");
while (i != rear)
{
printf("%d\n", a[i]);
i = (i + 1) % n;
}
printf("%d\n", a[rear]);
}
}

void peek()
{
if (front == -1)
{
printf("Queue Empty!");
}
else
{
printf("\nFront : %d", a[front]);
}
}

int main()
{
int option = 0;
printf("\n Enter Size of Queue : ");
scanf("%d", &n);

while (option != 5)
{

printf("\nChoose one from the below options...\n");


printf("\n1.Enqueue\n2.Dequeue\n3.Peek\n4.Display\n5.Exit");
printf("\n Enter your option \n");
scanf("%d", &option);
switch (option)
{
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
case 5:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid option");
}
}
}

return 0;
}
10) Singly Linked List

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void traverse();

struct node
{
int data;
struct node *link;
};

struct node *head = NULL;

void createlist()
{
if (head != NULL)
{
printf("\nList already present!: ");
exit;
}
else
{
int n, i, data;
struct node *temp, *newnode;
printf("\nEnter no of nodes : ");
scanf("%d", &n);
if (n != 0)
{
newnode = malloc(sizeof(struct node));
head = newnode;
temp = head;
printf("\nEnter Data of head node : ");
scanf("%d", &data);
head->data = data;

for (i = 2; i <= n; i++)


{
newnode = malloc(sizeof(struct node));
temp->link = newnode;
printf("\nEnter Data of next node : ");
scanf("%d", &data);
newnode->data = data;
newnode->link = NULL;
temp = temp->link;
}
}
}
}

void traverse()
{
struct node *temp;
temp = head;

if (head == NULL)
{
printf("\nNo List Present!");
}
else
{

while (temp != NULL)


{
printf("%d\t", temp->data);
temp = temp->link;
}
}
}

void search()
{
struct node *temp;
temp = head;
int key, flag = 0;

if (head == NULL)
{
printf("\nNo List Present!");
}
else
{
printf("\nEnter element to be searched : ");
scanf("%d", &key);
while (temp != NULL)
{
if (temp->data == key)
{
printf("\nData Present in list!");
flag = 1;
break;
}
temp = temp->link;
}

if (flag == 0)
printf("\nData is not Present in list!");
}
}
void insertAtFront()
{
struct node *newnode;
int data;
newnode = malloc(sizeof(struct node));
printf("\nEnter Data of New node : ");
scanf("%d", &data);
newnode->data = data;
newnode->link = head;
head = newnode;
}

void insertAtEnd()
{
struct node *newnode, *temp;
int data;
newnode = malloc(sizeof(struct node));
printf("\nEnter Data of New node : ");
scanf("%d", &data);
newnode->data = data;
newnode->link = NULL;
temp = head;
while (temp->link != NULL)
{
temp = temp->link;
}
temp->link = newnode;
}

void insertAtPosition()
{
struct node *temp, *newnode;
int pos, data;
printf("Enter position to insert : ");
scanf("%d", &pos);
newnode = malloc(sizeof(struct node));
printf("\nEnter Data of New node : ");
scanf("%d", &data);
newnode->data = data;
temp = head;

if (pos == 1)
{
newnode->link = head;
head = newnode;
}
else
{
pos--;
while (pos != 1)
{
temp = temp->link;
pos--;
}
newnode->link = temp->link;
temp->link = newnode;
}
}
void deleteFirst()
{
struct node *temp;
if (head == NULL)
{
printf("\nNo List Present!");
}
else
{
temp = head;
head = head->link;
free(temp);
temp = NULL;
}
}

void deleteEnd()
{
struct node *temp;
temp = head;
if (head == NULL)
{
printf("\nNo List Present!");
}
else
{
while (temp->link->link != NULL)
{
temp = temp->link;
}

free(temp->link);
temp->link = NULL;
}
}
void deletePosition()
{
int pos;
struct node *current, *previous;
current = head;
previous = head;
printf("Enter position of node to delete : ");
scanf("%d", &pos);

if(pos == 1)
{
head = head->link;
free(current);
}
else
{
while(pos!=1)
{
previous = current;
current = current -> link;
pos--;
}
previous->link = current->link;
free(current);
}
}

int main()
{
int ch = 0;
createlist();

while (ch != 10)


{
printf("\n1.Traverse \n2.insertAtFront \n3.insertAtEnd \
n4.insertAtPosition \n5.deleteFirst \n6.deleteEnd \n7.deletePosition \n
8.search \n 10.Exit");
printf("\nEnter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
search();
break;
case 10:
printf("\nExiting....!");
break;

default:
printf("\nEnter valid choice!");
break;
}
}
return 0;
}
11) Circular Linked List
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct node
{
int data;
struct node *link;
};

struct node *head = NULL;

void createlist()
{

int data, i, n;
printf("Enter no. of nodes : ");
scanf("%d", &n);

if (head != NULL)
{
printf("List Exists!");
}
else
{
if (n != 0)
{

struct node *newnode, *temp;


newnode = malloc(sizeof(struct node));

printf("Enter data of head node : ");


scanf("%d", &data);
newnode->data = data;
newnode->link = head;
head = newnode;
temp = head;
for (i = 2; i <= n; i++)
{
newnode = malloc(sizeof(struct node));
printf("Enter data of head node : ");
scanf("%d", &data);
newnode->data = data;
newnode->link = head;
temp->link = newnode;
temp = temp->link;
}
}
}
}

void display()
{
struct node *ptr = head;
if (head == NULL)
{
printf("No list ");
}
else
{
do
{
printf("%d\t", ptr->data);
ptr = ptr->link;
} while (ptr != head);
}
}

void insertbegin()
{
int data;
struct node *newnode = malloc(sizeof(struct node));
struct node *ptr;
ptr = head;
printf("Enter data of new node : ");
scanf("%d", &data);
newnode->data = data;
newnode->link = head;
while (ptr->link != head)
{
ptr = ptr->link;
}
ptr->link = newnode;
head = newnode;
}

void insertend()
{
int data;
struct node *newnode, *ptr;
ptr = head;
newnode = malloc(sizeof(struct node));
printf("Enter data of new node : ");
scanf("%d", &data);
newnode->data = data;
newnode->link = head;
while (ptr->link != head)
{
ptr = ptr->link;
}

ptr->link = newnode;
}

void insertpos()
{
int pos, data;
struct node *newnode, *ptr;
ptr = head;
newnode = malloc(sizeof(struct node));
printf("Enter position : ");
scanf("%d", &pos);
printf("Enter data of new node : ");
scanf("%d", &data);
newnode->data = data;

if (pos == 1)
{
newnode->link = head;
while (ptr->link != head)
ptr = ptr->link;

ptr->link = newnode;
head = newnode;
}
else
{
pos--;
while (pos != 1)
{
ptr = ptr->link;
pos--;
}
newnode->link = ptr->link;
ptr->link = newnode;
}
}

void deletebegin()
{
struct node *temp = head, *ptr = head;
if (head == NULL)
{
printf("List Empty!");
}
else
{

while (ptr->link != head)


ptr = ptr->link;

ptr->link = head->link;
head = head->link;
free(temp);
temp = NULL;
}
}
void deleteend()
{
struct node *temp;
if (head == NULL)
{
printf("List Empty!");
}
else
{
temp = head;
while (temp->link->link != head)
{
temp = temp->link;
}
free(temp->link);
temp->link = head;
}
}

void deletepos()
{
int pos;
struct node *previous = head, *current = head , *temp = head;
printf("Enter position : ");
scanf("%d", &pos);

if (pos == 1)
{
while (temp->link != head)
{
temp = temp->link;
}
temp->link = head->link;
head = head->link;
free(current);
current = NULL;
}
else
{
while (pos != 1)
{
previous = current;
current = current->link;
pos--;
}

previous->link = current->link;
free(current);
current = NULL;
}
}

void search()
{
int key;
printf("Enter key : ");
scanf("%d", &key);
struct node *ptr;
ptr = head;
do
{
if (key == ptr->data)
{
printf("Key Found!");
break;
}
ptr = ptr->link;
} while (ptr != head);

if (ptr == head)
{
printf("Key Not Found!");
}
}
int main()
{
createlist();
int ch = 0;
while (ch != 9)
{
printf("\n1. insertbegin \n 2. insertend \n 3. insertpos \n 4.
deletebegin \n 5. deleteend \n 6. deletepos \n 7. search \n 8. Display
\n 9. Exit");
printf("\nEnter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insertbegin();
break;
case 2:
insertend();
break;
case 3:
insertpos();
break;
case 4:
deletebegin();
break;
case 5:
deleteend();
break;
case 6:
deletepos();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
break;
}
}

return 0;
}
12) Tree traversal in C

#include <stdio.h>

#include <stdlib.h>

struct node {

int item;

struct node* left;

struct node* right;

};

// Inorder traversal

void inorderTraversal(struct node* root) {

if (root == NULL) return;

inorderTraversal(root->left);

printf("%d ->", root->item);

inorderTraversal(root->right);

// preorderTraversal traversal

void preorderTraversal(struct node* root) {

if (root == NULL) return;

printf("%d ->", root->item);

preorderTraversal(root->left);

preorderTraversal(root->right);
}

// postorderTraversal traversal

void postorderTraversal(struct node* root) {

if (root == NULL) return;

postorderTraversal(root->left);

postorderTraversal(root->right);

printf("%d ->", root->item);

// Create a new Node

struct node* createNode(value) {

struct node* newNode = malloc(sizeof(struct node));

newNode->item = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// Insert on the left of the node

struct node* insertLeft(struct node* root, int value) {

root->left = createNode(value);

return root->left;

}
// Insert on the right of the node

struct node* insertRight(struct node* root, int value) {

root->right = createNode(value);

return root->right;

int main() {

struct node* root = createNode(1);

insertLeft(root, 12);

insertRight(root, 9);

insertLeft(root->left, 5);

insertRight(root->left, 6);

printf("Inorder traversal \n");

inorderTraversal(root);

printf("\nPreorder traversal \n");

preorderTraversal(root);

printf("\nPostorder traversal \n");

postorderTraversal(root);

13) Deleting element in array

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a[50], n, pos, i;
printf("Enter size of array : ");
scanf("%d", &n);

for (i = 0; i < n; i++)


{
printf("A[%d] = ", i+1);
scanf("%d", &a[i]);
}

printf("Enter postision of element to be deleted : ");


scanf("%d", &pos);

if (pos >= n)
{
printf("\nPosition invalid!");
}
else
{
for (i = pos-1; i < n - 1; i++)
{
a[i] = a[i + 1];
}

for (i = 0; i < n-1; i++)


{
printf("%d\t ", a[i]);
}
}

return 0;
}

You might also like