0% found this document useful (0 votes)
45 views8 pages

Data Structures Lab Programs

The document contains two programming examples from the Data Structure Programming Lab at Padre Conceicao College of Engineering. The first example demonstrates the implementation of the Merge Sort algorithm, while the second example illustrates operations on a Singly Linked List, including insertion, deletion, and display functionalities. Both programs are written in C and include user interaction for input and output.

Uploaded by

sashamadpcce
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)
45 views8 pages

Data Structures Lab Programs

The document contains two programming examples from the Data Structure Programming Lab at Padre Conceicao College of Engineering. The first example demonstrates the implementation of the Merge Sort algorithm, while the second example illustrates operations on a Singly Linked List, including insertion, deletion, and display functionalities. Both programs are written in C and include user interaction for input and output.

Uploaded by

sashamadpcce
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/ 8

Padre Conceicao College of Engineering

PROGRAMS :

1. MERGE SORT
#include <stdio.h>
#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];

void merging(int low, int mid, int high) {


int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}

while(l1 <= mid)


b[i++] = a[l1++];

while(l2 <= high)


b[i++] = a[l2++];

for(i = low; i <= high; i++)


a[i] = b[i];
}

void sort(int low, int high) {


int mid;

if(low < high) {


mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}

int main() {
int i;

printf("List before sorting\n");

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


printf("%d ", a[i]);

sort(0, max);

printf("\nList after sorting\n");

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


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

Data Structure Programming Lab, Department of Computer Engineering RollNo. 21CE58


Padre Conceicao College of Engineering

Output:

Data Structure Programming Lab, Department of Computer Engineering RollNo. 21CE58


Padre Conceicao College of Engineering

1.SINGLY LINKED LIST


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* head = NULL, * temp, * ptr;
void insert_begin()
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
if (newnode == NULL)
printf("\n Memory not allocated");
else
{
int element;
printf("ENTER THE ELEMENT TO BE INSERTED\n");
scanf_s("%d", &element);
newnode->data = element;
newnode->next = NULL;
if (head == NULL)
{
head = newnode;
printf("element inserted");
}
else
{
newnode->next = head;
head = newnode;
printf("element inserted");
}
}
}
void insert_end()
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
if (newnode == NULL)
printf("\n Memory not allocated");
else
{
int element;
printf("ENTER THE ELEMENT TO BE INSERTED\n");
scanf_s("%d", &element);
newnode->data = element;
newnode->next = NULL;
if (head == NULL)
{
head = newnode;
printf("element inserted");
}
else
{
temp = head;
while (temp->next != NULL)
{
temp = temp->next;

Data Structure Programming Lab, Department of Computer Engineering RollNo. 21CE58


Padre Conceicao College of Engineering

temp->next = newnode;
printf("element inserted");
}
}
}
void insert_pos()
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
if (newnode == NULL)
printf("\n Memory not allocated");
else
{
int element, pos;
printf("ENTER THE ELEMENT TO BE INSERTED AND ITS POSITION\n");
scanf_s("%d%d", &element, &pos);
newnode->data = element;
newnode->next = NULL;
temp = head;
for (int i = 1; i <= pos - 1; i++)
{
ptr = temp;
temp = temp->next;
}
ptr->next = newnode;
newnode->next = temp;
printf("element inserted");

}
}
void del_begin()
{
if (head == NULL)
printf("LINKED LIST EMPTY");
else
{
temp = head;
head = head->next;
free(temp);
printf("element deleted");
}
}
void del_end()
{
if (head == NULL)
printf("LINKED LIST EMPTY");
else
{
temp = head;
while (temp->next != NULL)
{
ptr = temp;
temp = temp->next;
}
ptr->next = NULL;
free(temp);
printf("element deleted");
}
}
void del_pos()
{
if (head == NULL)
printf("LINKED LIST EMPTY");

Data Structure Programming Lab, Department of Computer Engineering RollNo. 21CE58


Padre Conceicao College of Engineering

else
{
int pos;
printf("ENTER THE POSITION TO BE DELETED\n");
scanf_s("%d", &pos);
temp = head;
for (int i = 1; i <= pos - 1; i++)
{
ptr = temp;
temp = temp->next;
}
if (temp->next == NULL)
{
free(temp);
head = NULL;
printf("element deleted");
}
else
{
ptr->next = temp->next;
free(temp);
printf("element deleted");
}
}
}
void peek()
{
if (head == NULL)
printf("STACK EMPTY");
else
{
printf("frontmost element is %d", head->data);
}
}
void display()
{
if (head == NULL)
printf("LINKED LIST EMPTY");
else
{
temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
}
void main()
{
int ch;
do
{
printf("\n1-insert at beginning\n2-insert at end\n3-insert at a position\n4-delete
beginning\n5-delete end\n6-delete at position\n7-peek\n8-display\n9-exit\n");
scanf_s("%d", &ch);
switch (ch)
{
case 1: insert_begin();
break;
case 2: insert_end();
break;

Data Structure Programming Lab, Department of Computer Engineering RollNo. 21CE58


Padre Conceicao College of Engineering

case 3: insert_pos();
break;
case 4: del_begin();
break;
case 5: del_end();
break;
case 6: del_pos();
break;
case 7: peek();
break;
case 8: display();
break;
case 9:break;
default:printf("invalid");
break;
}
} while (ch != 9);
_getch();
}

OUTPUT:

Data Structure Programming Lab, Department of Computer Engineering RollNo. 21CE58


Padre Conceicao College of Engineering

Data Structure Programming Lab, Department of Computer Engineering RollNo. 21CE58


Padre Conceicao College of Engineering

Data Structure Programming Lab, Department of Computer Engineering RollNo. 21CE58

You might also like