[go: up one dir, main page]

0% found this document useful (0 votes)
33 views30 pages

DS Practical

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

DS Practical

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

Program - 1

Write the program to Copy Elements of Array in another Array

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

Array before deletion


12 16 20 40 50 70 26
Array after deletion
12 16 20 50 70 26
Program - 3
Write the program using pointers to read in an array of integers and print
elements in reverse order
Code:
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void reverse(int array[], int array_size){
int *pointer1 = array, *pointer2 = array + array_size - 1;
while (pointer1 < pointer2) {
swap(pointer1, pointer2);
pointer1++;
pointer2--;
}
}
void print(int* array, int array_size){
int *length = array + array_size, *position = array;
printf("Array = ");
for (position = array; position < length; position++){
printf("%d ", *position);
}
}
int main(){
int array[] = { 2, 4, -6, 5, 8, -1 };
printf("Original ");
print(array, 6);
printf("\nReverse ");
reverse(array, 6);
print(array, 6);
return 0;
}

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

Enter the element to be added onto the stack: 2


Elements present in the stack:
2
1

Enter the element to be added onto the stack: 3


Elements present in the stack:
3
2
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];

char push(char ch){


if(top==(size-1)){
printf("Stack is Overflow\n");
}
else{
stack[++top]=ch;
}
}
char pop(){
if(top==-1){
printf("Stack is Underflow\n");
}
else{
return stack[top--];
}
}
int main(){
char str[20];
int i;
printf("Enter the string : " );
scanf("%s", &str);
for(i=0;i<strlen(str);i++){
push(str[i]);
}
for(i=0;i<strlen(str);i++){
str[i]=pop();
}
printf("Reversed string is : %s", str);
}

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

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:- 10

Press 1: Insert an element


Press 2: Delete an element
Press 3: Display the element
Enter your choice:- 3

Elements in a Queue are :5,10,


Press 1: Insert an element
Press 2: Delete an element
Press 3: Display the element
Enter your choice:-
Program - 7
Write the program to insert and delete an element into the Queue
Code:
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main() {
int i=0;
while(1) {
printf("Enter the element in queue to see the deleted element later
press 1 else 0\n");
scanf("%d",&i);
if(i==1)
insert();
else
break;
}
delete();
display();
return 0;
}
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;
}
}
void delete(){
if (front == - 1 || front > rear) {
printf("Queue Underflow \n");
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[fron]);
front = front + 1;
}
}
Output:
Enter the element in queue to see the deleted element later press 1 else 0

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;
}

Code (For Doubly Linked List):


#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *previous;
struct node *next;
};
struct node *head, *tail = NULL;
void addNodeD(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL) {
head = tail = newNode;
head->previous = NULL;
tail->next = NULL;
}
else {
tail->next = newNode;
newNode->previous = tail;
tail = newNode;
tail->next = NULL;
}
}
void displayD() {

struct node *current = head;


if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of doubly linked list: \n");
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
}

int main()
{
addNodeD(1);
addNodeD(2);
addNodeD(3);
addNodeD(4);
addNodeD(5);
displayD();
return 0;
}

Output (For Singly Linked List):


11 22 33 44

Output (For Doubly Linked List):


12345
Program - 9
Write the program to sort N numbers in ascending order using Bubble sort
Code:
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
printf("Unsorted array: ");
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
bubble_sort(arr, n);
printf("\nSorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
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;

int L[n1], M[n2];

for (int i = 0; i < n1; i++)


L[i] = arr[p + i];
for (int j = 0; j < n2; j++)
M[j] = arr[q + 1 + j];
int i, j, k;
i = 0;
j = 0;
k = p;
while (i < n1 && j < n2) {
if (L[i] <= M[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = M[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = M[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;

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

You might also like