You are on page 1of 28

LINKED LIST

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

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

struct Node* mergeLists(struct Node* head1, struct Node* head2) {


if (head1 == NULL) {
return head2;
}
if (head2 == NULL) {
return head1;
}
if (head1->data < head2->data) {
head1->next = mergeLists(head1->next, head2);
return head1;
} else {
head2->next = mergeLists(head1, head2->next);
return head2;
}
}

void printList(struct Node* head) {


struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}

int main() {
struct Node* head1 = NULL;
struct Node* head2 = NULL;
struct Node* merged = NULL;
int n1, n2, val;

printf("Enter the number of elements in the first list: ");


scanf("%d", &n1);
printf("Enter the elements of the first list: ");
for (int i = 0; i < n1; i++) {
scanf("%d", &val);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head1;
head1 = newNode;
}

printf("Enter the number of elements in the second list: ");


scanf("%d", &n2);
printf("Enter the elements of the second list: ");
for (int i = 0; i < n2; i++) {
scanf("%d", &val);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head2;
head2 = newNode;
}

printf("The first list is: ");


printList(head1);
printf("The second list is: ");
printList(head2);

merged = mergeLists(head1, head2);

printf("The merged list is: ");


printList(merged);

return 0;
}

Mioddle element

int main() {

struct Node* head1 = NULL;

struct Node* head2 = NULL;

struct Node* merged = NULL;

int n1, n2, val;

printf("Enter the number of elements in the first list: ");

scanf("%d", &n1);

printf("Enter the elements of the first list: ");

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

scanf("%d", &val);

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

newNode->data = val;

newNode->next = head1;

head1 = newNode;

printf("Enter the number of elements in the second list: ");


scanf("%d", &n2);

printf("Enter the elements of the second list: ");

for (int i = 0; i < n2; i++) {

scanf("%d", &val);

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

newNode->data = val;

newNode->next = head2;

head2 = newNode;

merged = mergeLists(head1, head2);

printf("The merged list is: ");

printList(merged);

struct Node* slow = merged;

struct Node* fast = merged;

while (fast != NULL && fast->next != NULL) {

slow = slow->next;

fast = fast->next->next;

printf("The middle element of the merged list is: %d\n", slow->data);

return 0;

}
reverse

int main() {

struct Node* head1 = NULL;

struct Node* head2 = NULL;

struct Node* merged = NULL;

int n1, n2, val;

printf("Enter the number of elements in the first list: ");

scanf("%d", &n1);

printf("Enter the elements of the first list: ");

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

scanf("%d", &val);

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

newNode->data = val;

newNode->next = head1;

head1 = newNode;

printf("Enter the number of elements in the second list: ");

scanf("%d", &n2);

printf("Enter the elements of the second list: ");

for (int i = 0; i < n2; i++) {

scanf("%d", &val);

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

newNode->data = val;

newNode->next = head2;

head2 = newNode;
}

merged = mergeLists(head1, head2);

printf("The merged list is: ");

printList(merged);

struct Node* current = merged;

struct Node* prev = NULL;

struct Node* next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

merged = prev;

printf("The reversed merged list is: ");

printList(merged);

return 0;

}
sum

int main() {

struct Node* head1 = NULL;

struct Node* head2 = NULL;

struct Node* merged = NULL;

int n1, n2, val, sum = 0;

printf("Enter the number of elements in the first list: ");

scanf("%d", &n1);

printf("Enter the elements of the first list: ");

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

scanf("%d", &val);

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

newNode->data = val;

newNode->next = head1;

head1 = newNode;

printf("Enter the number of elements in the second list: ");

scanf("%d", &n2);

printf("Enter the elements of the second list: ");

for (int i = 0; i < n2; i++) {

scanf("%d", &val);

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

newNode->data = val;

newNode->next = head2;

head2 = newNode;

merged = mergeLists(head1, head2);


printf("The merged list is: ");

printList(merged);

// Traverse the merged list and add up the data values

struct Node* current = merged;

while (current != NULL) {

sum += current->data;

current = current->next;

printf("The sum of the elements in the merged list is: %d\n", sum);

return 0;

Sum of alternative

int main() {

struct Node* head1 = NULL;

struct Node* head2 = NULL;

struct Node* merged = NULL;

int n1, n2, val, sum = 0;

int count = 1; // to keep track of position

printf("Enter the number of elements in the first list: ");

scanf("%d", &n1);

printf("Enter the elements of the first list: ");

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

scanf("%d", &val);

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

newNode->data = val;
newNode->next = head1;

head1 = newNode;

printf("Enter the number of elements in the second list: ");

scanf("%d", &n2);

printf("Enter the elements of the second list: ");

for (int i = 0; i < n2; i++) {

scanf("%d", &val);

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

newNode->data = val;

newNode->next = head2;

head2 = newNode;

merged = mergeLists(head1, head2);

printf("The merged list is: ");

printList(merged);

// Traverse the merged list and add up alternate node's data values

struct Node* current = merged;

while (current != NULL) {

if (count % 2 == 0) {

sum += current->data;

current = current->next;

count++;

printf("The sum of the alternate positions of the merged list is: %d\n", sum);

return 0;
}

#include <stdio.h>

#include <limits.h>

#define V 5 // number of vertices in the graph

int minKey(int key[], bool mstSet[]) {

int min = INT_MAX, min_index;

// find the minimum key value in the set of vertices not yet included in the MST

for (int v = 0; v < V; v++)

if (mstSet[v] == false && key[v] < min)

min = key[v], min_index = v;

return min_index;

void printMST(int parent[], int graph[V][V]) {

printf("Edge \tWeight\n");

for (int i = 1; i < V; i++)

printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);

void primMST(int graph[V][V]) {

int parent[V]; // array to store constructed MST

int key[V]; // key values used to pick minimum weight edge in cut

bool mstSet[V]; // set of vertices included in MST

// initialize all keys as INFINITE and mstSet[] as false

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

key[i] = INT_MAX, mstSet[i] = false;


// always include first vertex in MST

key[0] = 0; // make key 0 so that this vertex is picked as first vertex

parent[0] = -1; // first node is always root of MST

// the MST will have V vertices

for (int count = 0; count < V - 1; count++) {

// pick the minimum key vertex from the set of vertices not yet included in MST

int u = minKey(key, mstSet);

// add the picked vertex to the MST set

mstSet[u] = true;

// update key and parent arrays of adjacent vertices of the picked vertex

for (int v = 0; v < V; v++)

// graph[u][v] is non-zero only for adjacent vertices of m

// mstSet[v] is false for vertices not yet included in MST

// update the key only if graph[u][v] is smaller than key[v]

if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])

parent[v] = u, key[v] = graph[u][v];

// print the constructed MST

printMST(parent, graph);

int main() {

/* Example graph

2 3

(0)--(1)--(2)

| /\ |
6| 8/ \5 |7

|/ \|

(3)-------(4)

9 */

int graph[V][V] = {{0, 2, 0, 6, 0},

{2, 0, 3, 8, 5},

{0, 3, 0, 0, 7},

{6, 8, 0, 0, 9},

{0, 5, 7, 9, 0},

};

// run Prim's algorithm on the graph

primMST(graph);

return 0;

}
Dijkstras

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#define V 6 // Number of vertices in the graph

int minDistance(int dist[], int visited[]) {

int min = INT_MAX, min_index;

for (int i = 0; i < V; i++) {

if (!visited[i] && dist[i] <= min) {

min = dist[i];

min_index = i;

return min_index;

void dijkstra(int graph[V][V], int start) {

int dist[V];

int visited[V];

for (int i = 0; i < V; i++) {

dist[i] = INT_MAX;

visited[i] = 0;

dist[start] = 0;

for (int count = 0; count < V - 1; count++) {

int u = minDistance(dist, visited);


visited[u] = 1;

for (int v = 0; v < V; v++) {

if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {

dist[v] = dist[u] + graph[u][v];

printf("Vertex\t Distance from Start\n");

for (int i = 0; i < V; i++) {

printf("%d\t %d\n", i, dist[i]);

int main() {

int graph[V][V] = {

{0, 5, 0, 9, 2, 0},

{5, 0, 2, 0, 0, 7},

{0, 2, 0, 3, 0, 0},

{9, 0, 3, 0, 0, 8},

{2, 0, 0, 0, 0, 0},

{0, 7, 0, 8, 0, 0}

};

dijkstra(graph, 0);

return 0;

}
#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#define V 100 // Maximum number of vertices in the graph

int minDistance(int dist[], int visited[], int n) {

int min = INT_MAX, min_index;

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

if (!visited[i] && dist[i] <= min) {

min = dist[i];

min_index = i;

return min_index;

void dijkstra(int graph[V][V], int start, int n) {

int dist[V];

int visited[V];

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

dist[i] = INT_MAX;

visited[i] = 0;

dist[start] = 0;

for (int count = 0; count < n - 1; count++) {

int u = minDistance(dist, visited, n);

visited[u] = 1;
for (int v = 0; v < n; v++) {

if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
{

dist[v] = dist[u] + graph[u][v];

printf("Vertex\t Distance from Start\n");

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

printf("%d\t %d\n", i, dist[i]);

int main() {

int n;

printf("Enter the number of vertices in the graph: ");

scanf("%d", &n);

int graph[V][V];

printf("Enter the weight of each edge in the graph:\n");

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

for (int j = 0; j < n; j++) {

scanf("%d", &graph[i][j]);

int start;

printf("Enter the starting vertex: ");


scanf("%d", &start);

dijkstra(graph, start, n);

return 0;

Prims

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#define MAX_VERTICES 1000

int graph[MAX_VERTICES][MAX_VERTICES];

int n;

void primMST() {

int parent[n];

int key[n];

int mstSet[n];

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

key[i] = INT_MAX;

mstSet[i] = 0;

key[0] = 0;

parent[0] = -1;
for (int count = 0; count < n - 1; count++) {

int minKey = INT_MAX, minIndex;

for (int v = 0; v < n; v++) {

if (mstSet[v] == 0 && key[v] < minKey) {

minKey = key[v];

minIndex = v;

mstSet[minIndex] = 1;

for (int v = 0; v < n; v++) {

if (graph[minIndex][v] && mstSet[v] == 0 && graph[minIndex][v] < key[v]) {

parent[v] = minIndex;

key[v] = graph[minIndex][v];

printf("Edges in MST:\n");

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

printf("%d - %d\n", parent[i], i);

int main() {

printf("Enter number of vertices: ");

scanf("%d", &n);

printf("Enter adjacency matrix:\n");

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


for (int j = 0; j < n; j++) {

scanf("%d", &graph[i][j]);

primMST();

return 0;

Krushals

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int i,j,k,a,b,u,v,n,ne=1;

int min,mincost=0,cost[9][9],parent[9];

int find(int);

int uni(int,int);

void main()

clrscr();

printf("\n\tImplementation of Kruskal's algorithm\n");

printf("\nEnter the no. of vertices:");

scanf("%d",&n);

printf("\nEnter the cost adjacency matrix:\n");

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

{
for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;

printf("The edges of Minimum Cost Spanning Tree are\n");

while(ne < n)

for(i=1,min=999;i<=n;i++)

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

if(cost[i][j] < min)

min=cost[i][j];

a=u=i;

b=v=j;

u=find(u);

v=find(v);

if(uni(u,v))

{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);

mincost +=min;

cost[a][b]=cost[b][a]=999;

printf("\n\tMinimum cost = %d\n",mincost);

getch();

int find(int i)

while(parent[i])

i=parent[i];

return i;

int uni(int i,int j)

if(i!=j)

parent[j]=i;

return 1;

return 0;

}
Stack

#include <stdio.h>

int stack[100],i,j,choice=0,n,top=-1;

void push();

void pop();

void show();

void main ()

printf("Enter the number of elements in the stack ");

scanf("%d",&n);

printf("*********Stack operations using array*********");

printf("\n----------------------------------------------\n");

while(choice != 4)

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

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

printf("\n Enter your choice \n");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();
break;

case 3:

show();

break;

case 4:

printf("Exiting....");

break;

default:

printf("Please Enter valid choice ");

};

void push ()

int val;

if (top == n )

printf("\n Overflow");

else

printf("Enter the value?");

scanf("%d",&val);

top = top +1;

stack[top] = val;
}

void pop ()

if(top == -1)

printf("Underflow");

else

top = top -1;

void show()

for (i=top;i>=0;i--)

printf("%d\n",stack[i]);

if(top == -1)

printf("Stack is empty");

}
Queue

#include<stdio.h>   
#include<stdlib.h>  
#define maxsize 5  
void insert();  
void delete();  
void display();  
int front = -1, rear = -1;  
int queue[maxsize];  
void main ()  
{  
    int choice;   
    while(choice != 4)   
    {     
        printf("\n*************************Main Menu*****************************\n");  
        printf("\
n===================================================
==============\n");  
        printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\
n4.Exit\n");  
        printf("\nEnter your choice ?");  
        scanf("%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            insert();  
            break;  
            case 2:  
            delete();  
            break;  
            case 3:  
            display();  
            break;  
            case 4:  
            exit(0);  
            break;  
            default:   
            printf("\nEnter valid choice??\n");  
        }  
    }  
}  
void insert()  
{  
    int item;  
    printf("\nEnter the element\n");  
    scanf("\n%d",&item);      
    if(rear == maxsize-1)  
    {  
        printf("\nOVERFLOW\n");  
        return;  
    }  
    if(front == -1 && rear == -1)  
    {  
        front = 0;  
        rear = 0;  
    }  
    else   
    {  
        rear = rear+1;  
    }  
    queue[rear] = item;  
    printf("\nValue inserted ");  
      
}  
void delete()  
{  
    int item;   
    if (front == -1 || front > rear)  
    {  
        printf("\nUNDERFLOW\n");  
        return;  
              
    }  
    else  
    {  
        item = queue[front];  
        if(front == rear)  
        {  
            front = -1;  
            rear = -1 ;  
        }  
        else   
        {  
            front = front + 1;  
        }  
        printf("\nvalue deleted ");  
    }  
      
      
}  
      
void display()  
{  
    int i;  
    if(rear == -1)  
    {  
        printf("\nEmpty queue\n");  
    }  
    else  
    {   printf("\nprinting values .....\n");  
        for(i=front;i<=rear;i++)  
        {  
            printf("\n%d\n",queue[i]);  
        }     
    }  

You might also like