DS Practical
DS Practical
Code:
#include <stdio.h>
int main() {
int original[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int copied[10];
int i;
for(i= 0; i < 10; i++) {
copied[i] = original[i];
}
printf("original -> copied \n");
for(i = 0; i < 10; i++) {
printf(" %2d %2d\n", original[i], copied[i]);
}
return 0;
}
Output:
original -> copied
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
0 0
Program - 2
Write the program to insert, delete and search an element in an Array
Code:
#include <stdio.h>
// Function to implement search operation
int findElement(int arr[], int n, int key){
int i;
for (i = 0; i < n; i++)
if (arr[i] == key){
return i;
}
return -1;
}
// Function to implement insert operation
int insertSorted(int arr[], int index, int key, int capacity){
if (index >= capacity){
return index;
}
arr[index] = key;
return (index + 1);
}
// Function to delete an element
int deleteElement(int arr[], int n, int key){
int pos = findElement(arr, n, key);
if (pos == -1) {
printf("Element not found");
return n;
}
int i;
for (i = pos; i < n - 1; i++){
arr[i] = arr[i + 1];
}
return n - 1;
}
int main(){
int arr[7] = { 12, 16, 20, 40, 50, 70 };
int n = sizeof(arr) / sizeof(arr[0]);
// Searching
int key = 40;
int position = findElement(arr, n, key);
if (position == -1){
printf("Element not found");
}
else{
printf("Element Found at Position: %d",position + 1);
}
printf("\n");
// Insertion
int capacity = sizeof(arr) / sizeof(arr[0]);
int index = 6;
int i;
key = 26;
printf("\nBefore Insertion: ");
for (i = 0; i < index; i++){
printf("%d ", arr[i]);
}
index = insertSorted(arr, index, key, capacity);
printf("\nAfter Insertion: ");
for (i = 0; i < index; i++){
printf("%d ", arr[i]);
}
printf("\n\n");
// Deletion
key = 40;
printf("Array before deletion\n");
for (i = 0; i < n; i++){
printf("%d ", arr[i]);
}
n = deleteElement(arr, n, key);
printf("\nArray after deletion\n");
for (i = 0; i < n; i++){
printf("%d ", arr[i]);
}
return 0;
}
Output:
Element Found at Position: 4
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26
Output:
Original Array = 2 4 -6 5 8 -1
Reverse Array = -1 8 5 -6 4 2
Program - 4
Write the program to implement Stack and perform PUSH and POP Operation
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
int top = -1, inp_array[SIZE];
void push()
{
int x;
if (top == SIZE - 1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter the element to be added onto the stack: ");
scanf("%d", &x);
top = top + 1;
inp_array[top] = x;
}
printf("\nElements present in the stack: \n");
for (int i = top; i >= 0; --i){
printf("%d\n", inp_array[i]);
}
}
void pop()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d", inp_array[top]);
top = top - 1;
}
printf("\nElements present in the stack: \n");
for (int i = top; i >= 0; --i){
printf("%d\n", inp_array[i]);
}
}
int main() {
push();
push();
push();
pop();
return 0;
}
Output:
Enter the element to be added onto the stack: 1
Elements present in the stack:
1
Popped element: 3
Elements present in the stack:
2
1
Program - 5
Write the program to reverse the string using stack
Code:
#include<stdio.h>
#include<string.h>
#define size 20
int top = -1;
char stack[size];
Output:
Enter the string : dog
Reversed string is : god
Program - 6
Write the program to implement circular queue through array
Code:
#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
void enqueue(int element)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
int dequeue()
{
if((front==-1) && (rear==-1))
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1, x;
while(choice<4 && choice!=0)
{
printf("\nPress 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice:- ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element which is to be inserted:- ");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}
return 0;
}
Output:
Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice:- 1
Enter the element which is to be inserted:- 5
1
Inset the element in queue : 10
Enter the element in queue to see the deleted element later press 1 else 0
1
Inset the element in queue : 20
Enter the element in queue to see the deleted element later press 1 else 0
1
Inset the element in queue : 30
Enter the element in queue to see the deleted element later press 1 else 0
0
Element deleted from queue is : 10
Queue is :
20 30
Program - 8
Write the program to implement Singly Linked List and Doubly Linked List
Code (For Singly Linked List):
#include <stdio.h>
#include <stdlib.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 display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of singly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main(){
addNode(11);
addNode(22);
addNode(33);
addNode(44);
display();
return 0;
}
int main()
{
addNodeD(1);
addNodeD(2);
addNodeD(3);
addNodeD(4);
addNodeD(5);
displayD();
return 0;
}
Output:
Unsorted array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
Program – 10
Write the program to sort N numbers in ascending order using Merge sort
Code:
#include <stdio.h>
void merge(int arr[], int p, int q, int r) {
int n1 = q - p + 1;
int n2 = r - q;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
printArray(arr, size);
mergeSort(arr, 0, size - 1);
printf("Sorted array: \n");
printArray(arr, size);
return 0;
}
Output:
Unsorted array:
6 5 12 10 9 1
Sorted array:
1 5 6 9 10 12