Wednesday, September 28, 2022

Data Structures and Algorithms[CODES]

 

Stack using array in C

 #include<stdio.h>

#include<stdlib.h>

#define Size 4

int Top=-1, inp_array[Size];

void Push();

void Pop();

void show();

int main()

{

    int choice;

     while(1)   

    {

        printf("\nOperations performed by Stack");

        printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");

        printf("\n\nEnter the choice:");

        scanf("%d",&choice);

        switch(choice)

        {

            case 1: Push();

                    break;

            case 2: Pop();

                    break;

            case 3: show();

                    break;

            case 4: exit(0);

            

            default: printf("\nInvalid choice!!");

        }

    }

}

 void Push()

{

    int x;

     if(Top==Size-1)

    {

        printf("\nOverflow!!");

    }

    else

    {

        printf("\nEnter element to be inserted to the stack:");

        scanf("%d",&x);

        Top=Top+1;

        inp_array[Top]=x;

    }

}

  void Pop()

{

    if(Top==-1)

    {

        printf("\nUnderflow!!");

    }

    else

    {

        printf("\nPopped element:  %d",inp_array[Top]);

        Top=Top-1;

    }

}

 void show()

{

     if(Top==-1)

    {

        printf("\nUnderflow!!");

    }

    else

    {

        printf("\nElements present in the stack: \n");

        for(int i=Top;i>=0;--i)

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

    }

}


 Queue using array in C 

 #include <stdio.h>

#include<stdlib.h>

#define MAX 20

void insert();

void delete();

void display();

int queue_array[MAX];

int rear = - 1;

int front = - 1;

int main()

{

int choice;

printf("\t\t ***Welcome to DSA COURSE*** :\n\t This is a demonstration of QUEUE implementation \n\n");

while (1)

{

printf("1.Insert element to queue \n");

printf("2.Delete element from queue \n");

printf("3.Display elements of queue \n");

printf("4.Quit \n");

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

scanf("%d", &choice);

switch(choice)

{

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong choice \n");

}

}

}

void insert()

{

int item;

if(rear == MAX - 1)

printf("Queue Overflow n");

else

{

if(front== - 1)

front = 0;

printf("Inset the element in queue : \n");

scanf("%d", &item);

rear = rear + 1;

queue_array[rear] = item;

}

}

void delete()

{

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

{

printf("Queue Underflow \n");

return;

}

else

{

printf("Element deleted from queue is : %d \n", queue_array[front]);

front = front + 1;

}

}

void display()

{

int i;

if(front == - 1)

printf("Queue is empty \n");

else

{

printf("Queue is : \n");

for(i = front; i <= rear; i++)

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

printf("\n");

}

} 

Circular queue using array  in C

#include<stdio.h>

#define MAX 5

int MyQ[MAX];

int front = -1; // initialization of front and rear to -1;

int rear = -1; // that means that the both front & rear //pointers have not even started.

void insert(int item)

{

if((front == 0 && rear == MAX-1) || (front == rear+1))

{

printf("Queue Overflow \n");

return;

}

if(front == -1)

{

front = 0;

rear = 0;

}

else

{

if(rear == MAX-1)

rear = 0;

else

rear = rear+1;

}

MyQ[rear] = item ;

}

void deletion()

{

if(front == -1)

{

printf("Queue Underflow \n");

return ;

}

printf("Element deleted from queue is : %d \n",MyQ[front]);

if(front == rear)

{

front = -1;

rear=-1;

}

else

{

if(front == MAX-1)

front = 0;

else

front = front+1;

}

}

void show()

{

int front_pos = front,rear_pos = rear;

if(front == -1)

{

printf("Queue is empty \n");

return;

}

printf("Queue elements :\n");

if( front_pos <= rear_pos )

while(front_pos <= rear_pos)

{

printf("%d ",MyQ[front_pos]);

front_pos++;

}

else

{

while(front_pos <= MAX-1)

{

printf("%d ",MyQ[front_pos]);

front_pos++;

}

front_pos = 0;

while(front_pos <= rear_pos)

{

printf("%d ",MyQ[front_pos]);

front_pos++;

}

}

printf("\n");

}

int main()

{

int choice,item;

do

{

printf("Welcome to Debugging Corner \n");

printf("1.Insert \n");

printf("2.Delete \n");

printf("3.show \n");

printf("4.Quit \n");

printf("Give your choice : \n");

scanf("%d",&choice);

switch(choice)

{

case 1 :

printf("Input the element for insertion in queue : \n");

scanf("%d", &item);

insert(item);

break;

case 2 :

deletion();

break;

case 3:

show();

break;

case 4:

break;

default:

printf("This choice is not correct \n");

}

}while(choice!=4);

return 0;

}


Create an unordered linked list to enrol the students who wish to participate in a gaming event by taking details like Name, Register No., Age, and Phone number. Ensure that no more than five members are there on the list of the same age. Perform insertion(), deletion() and display() operations on the Linked List.

#include <stdio.h> 

#include <stdlib.h> 

#include <string.h> 

#define MAX 30 

struct stu_data 

 int regno; 

 int age; 

            char stuName[MAX]; 

 char phonenum[MAX]; 

            struct stu_data *next; 

}; 

struct stu_data *insert(struct stu_data *front, int id, int new_age, char name[], char  

phone[], int cage) 

 if (cage < 5) 

 { 

 struct stu_data *newnode; 

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

 if (newnode == NULL) 

 { 

 printf("\n Allocation failed \n"); 

 exit(2); 

 } 

 newnode->regno = id; 

            newnode->age = new_age; 

 strcpy(newnode->stuName, name); 

            strcpy(newnode->phonenum, phone); 

            newnode->next = front; 

            front = newnode; 

 return (front); 

 } 

 else 

 { 

            printf("more than 5 people of same age are present"); 

 } 

void printNode(struct stu_data *p) 

            printf("\n Student Details...\n"); 

 printf("\n Student Number : %d", p->regno); 

            printf("\n Student Age : %d", p->age); 

            printf("\n Name : %s", p->stuName); 

 printf("\n phone number : %s\n", p->phonenum); 

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

struct stu_data *deleteNode(struct stu_data *front, int id) 

 struct stu_data *ptr; 

 struct stu_data *bptr; 

 if (front->regno == id) 

 { 

            ptr = front; 

            printf("\n Node deleted:"); 

 printNode(front); 

 front = front->next; 

 free(ptr); 

 return (front); 

 } 

 for (ptr = front->next, bptr = front; ptr != NULL; ptr = ptr->next, bptr = bptr->next) 

 { 

 if (ptr->regno == id) 

 { 

            printf("\n Node deleted:"); 

 printNode(ptr); 

 bptr->next = ptr->next; 

 free(ptr); 

 return (front); 

 } 

 } 

 printf("\n Student Number %d not found ", id); 

 return (front); 

void search(struct stu_data *front, int key) 

 struct stu_data *ptr; 

 for (ptr = front; ptr != NULL; ptr = ptr->next) 

 { 

 if (ptr->regno == key) 

 { 

 printf("\n Key found:"); 

 printNode(ptr); 

 return; 

 } 

 } 

 printf("\n Student Number %d not found ", key); 

int count(struct stu_data *front, int key) 

 struct stu_data *ptr; 

 int c = 0; 

 for (ptr = front; ptr != NULL; ptr = ptr->next) 

 { 

 if (ptr->age == key) 

 { 

 c++; 

 } 

 } 

 return c; 

void display(struct stu_data *front) 

 struct stu_data *ptr; 

 for (ptr = front; ptr != NULL; ptr = ptr->next) 

 { 

 printNode(ptr); 

 } 

void menu() 

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

 printf("Press 1 to INSERT a node into the list \n"); 

 printf("Press 2 to DELETE a node from the list \n"); 

 printf("Press 3 to DISPLAY the list \n"); 

 printf("Press 4 to SEARCH the list \n"); 

            printf("Press 5 to EXIT \n"); 

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

char option() 

 char choice; 

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

 switch (choice = getchar()) 

 { 

 case '1': 

            case '2': 

 case '3': 

 case '4': 

 case '5': 

 return (choice); 

            default: 

 printf("\n Invalid choice."); 

 } 

 return choice; 

void main() 

            struct stu_data *linkList; 

 char name[21], phone[51]; 

 int age; 

            char choice; 

 int sno; 

 linkList = NULL; 

 printf("M Ashwin Kumar(19BCE0048)\n"); 

 printf("\n Welcome to demonstration of singly linked list \n"); 

 menu(); 

 do 

 { 

 /* choose oeration to be performed */ 

 int cage = count(linkList, age); 

 choice = option(); 

 switch (choice) 

 { 

 case '1': 

 printf("\n Enter the Student Reg Number : "); 

 scanf("%d", &sno); 

 printf("\n Enter the Student Age: "); 

 scanf("%d", &age); 

            printf("Enter the Student name : "); 

 fflush(stdin); 

 gets(name); 

 printf("Enter the Student phone number: "); 

 gets(phone); 

 linkList = insert(linkList, sno, age, name, phone, cage); 

 break; 

 case '2': 

 printf("\n\n Enter the Student number to be deleted: "); 

 scanf("%d", &sno); 

            linkList = deleteNode(linkList, sno); 

            break; 

            case '3': 

 if (linkList == NULL) 

 { 

 printf("\n List empty."); 

 break; 

 } 

            display(linkList); 

 break; 

            case '4': 

            printf("\n\n Enter the employee number to be searched: "); 

 scanf("%d", &sno); 

            search(linkList, sno); 

 break; 

 case '5': 

 break; 

 } 

 }         while (choice != '5'); 

Output:







Queue using two stacks in C

#include<stdio.h>

#include<stdlib.h>

 void firstpushop(int);

void secondpushop(int);

int firstpopop();

int secondpopop();

void EnQ();

void DeQ();

void Show();

void Make();

 int array1[50], array2[50];

int topmostarray1= -1, topmostarray2 = -1;

int counting = 0;

 void main()

{

    int ch;

    printf("\n 1 - EnQ element to the queue");

    printf("\n 2 - Dequeu element from the queue");

    printf("\n 3 - Show the queue");

    printf("\n 4 - Exit");

    Make();

    while (1)

    {

        printf("\nEnter the choice");

        scanf("%d", &ch);

        switch (ch)

        {

        case 1:

            EnQ();

            break;

        case 2:

            DeQ();

            break;

        case 3:

            Show();

            break;

        case 4:

            exit(0);

        default:

            printf("Miss choice");

        }

    }

}

 /*Q creation*/

void Make()

{

    topmostarray1= topmostarray2 = -1;

}

 /*Function to push the element on to the stack*/

void firstpushop(int data)

{

    array1[++topmostarray1] = data;

}

 /*pop from stack*/

int firstpopop()

{

    return(array1[topmostarray1--]);

}

 

/* pushing elements */

void secondpushop(int data)

{

    array2[++topmostarray2] = data;

}

 /*Function to pop an element from th stack*/

 int secondpopop()

{

    return(array2[topmostarray2--]);

}

 

/*Function to add an element into the queue using stack*/

void EnQ()

{

    int data, i;

 

    printf("Enter data to the queue");

    scanf("%d", &data);

    firstpushop(data);

    counting++;

}

 

/*delete element*/

 

void DeQ()

{

    int i;

 

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

    {

        secondpushop(firstpopop());

    }

    secondpopop();

    counting--;

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

    {

        firstpushop(secondpopop());

    }

}

 /*Function to Show the elements in the stack*/

 void Show()

{

    int i;

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

    {

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

    }

}



   Stack using LinkedList in C                             

#include <stdio.h>

#include <stdlib.h>

  struct node {

        int data;

        struct node *nextptr;

  };

  struct node *topofstack = NULL;

  struct node* newNode(int data) {

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

        ptr->data = data;

        ptr->nextptr = NULL;

  }

  void push(int data) {

        struct node *ptr = newNode(data);

        if (!topofstack) {

                topofstack = ptr;

                return;

        }

        ptr->nextptr = topofstack;

        topofstack = ptr;

  }

  void pop() {

        struct node *ptr;

        if (!topofstack) {

                printf("Underflow Stack\n");

                return;

        }

        ptr = topofstack;

        ptr = ptr->nextptr;

        free(topofstack);

        topofstack = ptr;

  }

 

 

  void Show() {

        struct node *ptr = topofstack;

        if (!ptr) {

                printf("Empty Stack\n");

                return;

        }

        while (ptr) {

                printf("%-2d", ptr->data);

                ptr = ptr->nextptr;

        }

        printf("\n");

        return;

  }

 

 

  int main() {

        int data, ch;

        printf("STACK using linkelist\n");

        while (1) {

                                               

                printf("1. Push\n2. Pop\n3. Show\n");

                printf("4. Exit\nEnter ur choice:");

                scanf("%d", &ch);

                switch (ch) {

                        case 1:

                                printf("Enter the element:");

                                scanf("%d", &data);

                                push(data);

                                break;

                        case 2:

                                pop();

                                break;

                        case 3:

                                Show();

                                break;

                        case 4:

                                exit(0);

                        default:

                                printf("Enter correct option \n");

                                break;

                }

        }

        return 0;

  }



          Queue using LinkedList in C                                 

#include <stdio.h>

#include <stdlib.h>

  struct node {

        int data;

        struct node *nextptr;

  };

  struct node *frontptr = NULL, *rearptr = NULL;

  struct node * MakeNewNode(int data) {

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

        newnode->data = data;

        newnode->nextptr = NULL;

        return (newnode);

  }

 

  void enqueue(int data) {

        struct node *ptr = MakeNewNode(data);

        if (!rearptr) {

                frontptr = rearptr = ptr;

                return;

        }

        rearptr->nextptr = ptr;

        rearptr = ptr;

  }

 void dequeue() {

        struct node *tmp;

        if (!frontptr) {

                printf("Empty Queue\n");

                return;

        } else if (frontptr == rearptr) {

                tmp = frontptr;

                frontptr = rearptr = NULL;

        } else {

                tmp = frontptr;

                frontptr = frontptr->nextptr;

        }

        free(tmp);

        return;

  }

 

  void show() {

        struct node *tmp;

        if (!frontptr) {

                printf("Empty queue\n");

        } else {

                tmp = frontptr;

                while (tmp) {

                        printf("%-3d", tmp->data);

                        tmp = tmp->nextptr;

                }

                printf("\n");

        }

        return;

  }

 

  int main() {

        int data, ch;

        printf("Queue using LinkedList\n");

        while (1) {

                printf("1. Enqueue\n2. Dequeue\n");

                printf("3. show\n4. Exit\n");

                printf("What is your choice:");

                scanf("%d", &ch);

                switch (ch) {

                        case 1:

                                printf("Enter values:");

                                scanf("%d", &data);

                                enqueue(data);

                                break;

                        case 2:

                                dequeue();

                                break;

                        case 3:

                                show();

                                break;

                        case 4:

                                exit(0);

                        default:

                                printf("Not correct option,enter again\n");

                                break;

                }

        }

        return 0;

  }




Circular LinkedList in C

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

  struct node {
        int data;
        struct node *nextptr, *prevptr;
  };

  struct node *headprt = NULL, *tailprt = NULL;
  int Count = 0;

  struct node * makeNode(int data) {
        struct node *newnode;
        newnode = (struct node *)malloc(sizeof (struct node));
        newnode->data = data;
        newnode->nextptr = NULL;
        newnode->prevptr = NULL;
        return (newnode);
  }

  void newNodes() {
        headprt = (struct node *)malloc(sizeof (struct node));
        tailprt = (struct node *)malloc(sizeof (struct node));
        headprt->data = tailprt->data = 0;
        headprt->nextptr = tailprt;
        tailprt->prevptr = headprt;
        headprt->prevptr = tailprt->nextptr = NULL;
  }

  void insert(int data, int pos) {
        struct node *newnode, *temp1;
        int count = 1;
        newnode = makeNode(data);
        temp1 =  headprt;
        while (temp1) {
                if (count == pos) {
                        newnode->nextptr = temp1->nextptr;
                        newnode->prevptr = temp1;
                        temp1->nextptr->prevptr = newnode;
                        temp1->nextptr = newnode;
                        Count++;
                        break;
                }
                temp1 = temp1->nextptr;
                count++;
        }
        return;
  }

  void insertatBeg(int data) {
        struct node *newnode = makeNode(data);
        newnode->nextptr = headprt->nextptr;
        newnode->prevptr = headprt;
        headprt->nextptr->prevptr = newnode;
        headprt->nextptr = newnode;
        Count++;
  }

  void insertatLast(int data) {
        struct node *newnode = makeNode(data);
        newnode->nextptr = tailprt;
        newnode->prevptr = tailprt->prevptr;
        tailprt->prevptr->nextptr = newnode;
        tailprt->prevptr = newnode;
        Count++;
  }

  void delete(int data) {
        struct node *temp1, *temp2;
        temp1 = headprt->nextptr;
        while (temp1 != tailprt) {
                if (temp1->data == data) {
                        temp2 = temp1;
                        temp1->prevptr->nextptr = temp1->nextptr;
                        temp1->nextptr->prevptr = temp1->prevptr;
                        free(temp2);
                        Count--;
                        return;
                }
                temp1 = temp1->nextptr;
        }
        printf("Given node is not present in the List\n");
        return;
  }

  void deleteList() {
        struct node *temp2, *temp1 = headprt->nextptr;
        while (temp1 != tailprt) {
                temp1->prevptr->nextptr = temp1->nextptr;
                temp1->nextptr->prevptr = temp1->prevptr;
                temp2 = temp1;
                free(temp1);
                temp1 = temp2->nextptr;
        }
        Count = 0;
        return;
  }

  void Find(int data) {
        struct node *temp = headprt->nextptr;
        while (temp != tailprt) {
                if (temp->data == data) {
                        printf("Given node is present\n");
                        return;
                }
                temp = temp->nextptr;
        }
        printf("Given node is not present in the list\n");
        return;
  }

  void TraverseList() {
        struct node *ptr = headprt->nextptr;
        int flag = 1, i = 1;
        if (headprt->nextptr == tailprt) {
                printf("Empty\n");
                return;
        }
        printf("Element in List:\n");
        while (ptr != tailprt) {
                if (ptr->prevptr != headprt) {
                        printf("           /\\ \n");
                        printf("           ||\n");
                        flag = 0;
                }
                printf("+------------------------+\n");
                printf("|node%2d addr:0x%08x  |\n", i, (unsigned int)ptr);
                printf("+------------------------+\n");
                printf("|data: %4d              |\n", ptr->data);
                printf("-------------------------+\n");
                printf("|prevptr: 0x%08x        |\n",
                        ptr->prevptr == headprt ? 0 : (unsigned int)ptr->prevptr);
                printf("+------------------------+\n");
                printf("|nextptr: 0x%08x        |\n",
                        ptr->nextptr == tailprt ? 0 : (unsigned int)ptr->nextptr);
                printf("+------------------------+\n");
                ptr = ptr->nextptr;

                if (ptr != tailprt) {
                        printf("    ||      \n");
                        printf("    \\/     \n");
                }
                i++;
        }
  }

  int main() {
        int data, ch, pos;
        newNodes();
        while (1) {
                printf("1. Insert\n2. Delete\n");
                printf("3. Traverse the List\n4. Search \n");
                printf("5. Delete List\n6. Insert at Beg\n");
                printf("7. Insert in the last\n8. Exit\n");
                printf("What is your choice:");
                scanf("%d", &ch);
                switch (ch) {
                        case 1:
                                printf("Enter the node position(1 - %d)", Count+1);
                                scanf("%d", &pos);
                                if (pos <= 0 || pos > Count+1) {
                                        printf("Enter correct position\n");
                                } else {
                                        printf("Enter data :");
                                        scanf("%d", &data);
                                        insert(data, pos);
                                }
                                break;
                        case 2:
                                printf("Enter the elemenet to be deleted:");
                                scanf("%d", &data);
                                delete(data);
                                break;
                        case 3:
                                TraverseList();
                                break;
                        case 4:
                                printf("What to be searched");
                                scanf("%d", &data);
                                Find(data);
                                break;
                        case 5:
                                deleteList();
                                break;
                        case 6:
                                printf("Enter ur data to  insert at start:");
                                scanf("%d", &data);
                                insertatBeg(data);
                                break;
                        case 7:
                                printf("Enter ur data to insert:");
                                scanf("%d", &data);
                                insertatLast(data);
                                break;
                        case 8:
                                exit(0);
                        default:
                                printf("Entered wrong option\n");
                                break;
                }
        }
        return 0;
  }








No comments:

Post a Comment

GRAPH ALGORITHMS : FLOYD-WARSHAL -Displaying DISTANCE MATRIX and PI MATRIX using C PROGRAM

Modify the following Floyd-Warshall algorithm by introducing a π( pi ) table as per the class’s discussion. Here you can write one function...