0% found this document useful (0 votes)
5 views3 pages

Data Structures Programs

The document contains C programs demonstrating various data structures, including an Insertion Sort algorithm, a Queue implementation using an array, and a method for inserting a node at a given position in a singly linked list. Each program includes user input for elements and displays the results after performing the respective operations. The code snippets illustrate fundamental concepts in data structures and algorithms.
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)
5 views3 pages

Data Structures Programs

The document contains C programs demonstrating various data structures, including an Insertion Sort algorithm, a Queue implementation using an array, and a method for inserting a node at a given position in a singly linked list. Each program includes user input for elements and displays the results after performing the respective operations. The code snippets illustrate fundamental concepts in data structures and algorithms.
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

Data Structures Programs

Insertion Sort
#include <stdio.h>

int main()
{
int n, i, j, key;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];

printf("Enter elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);

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


{
key = a[i];
j = i - 1;

while(j >= 0 && a[j] > key)


{
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}

printf("Sorted array:\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);

return 0;
}
Queue Using Array
#include <stdio.h>

#define MAX 100

int queue[MAX];
int front = -1, rear = -1;

void enqueue(int item)


{
if(rear == MAX - 1)
{
printf("Queue Overflow\n");
return;
}

if(front == -1)
front = 0;

queue[++rear] = item;
}

void dequeue()
{
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
return;
}

printf("Deleted element: %d\n", queue[front++]);


}

void display()
{
int i;

if(front == -1 || front > rear)


{
printf("Queue is empty\n");
return;
}

printf("Queue elements: ");


for(i = front; i <= rear; i++)
printf("%d ", queue[i]);
}

int main()
{
int n, x, i;

scanf("%d", &n);

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


{
scanf("%d", &x);
enqueue(x);
}

display();
dequeue();
display();

return 0;
}
Insertion at Given Position in Singly Linked List
#include <stdio.h>
#include <stdlib.h>

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

int main()
{
struct node *head = NULL, *temp, *newnode;
int n, i, pos, value;

scanf("%d", &n);

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


{
newnode = (struct node *)malloc(sizeof(struct node));
scanf("%d", &newnode->data);
newnode->next = NULL;

if(head == NULL)
{
head = newnode;
temp = head;
}
else
{
temp->next = newnode;
temp = newnode;
}
}

scanf("%d%d", &pos, &value);

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


newnode->data = value;

if(pos == 1)
{
newnode->next = head;
head = newnode;
}
else
{
temp = head;

for(i = 1; i < pos - 1 && temp != NULL; i++)


temp = temp->next;

newnode->next = temp->next;
temp->next = newnode;
}

temp = head;

while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}

return 0;
}

You might also like