Wednesday, January 31, 2024

QUEUE's (Using Linked Lists) - DSTC using C Language (Source Code Implemented)

QUEUE's (Using Linked Lists)
Implemented using C Language

C Language Code: QUEUELST.H

 QUEUELST.H

//   DSTC ---- QUEUELST.H ..queues using Linked Lists

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

typedef struct queuelstnode
{
            int info ;
            struct queuelstnode *next ;
}node ;

typedef struct queuelst
{
            node *front ;
            node *rear ;
}queuelst ;

queuelst *makequeue()
{
            queuelst *t ;
            t = (queuelst *)malloc(sizeof(queuelst)) ;
            t->front = NULL ;
            t->rear = NULL ;
            return t ;
}

node *makenode()
{
            node *n ;
            n = (node *)malloc(sizeof(node));
            return n ;
}

void insert(queuelst *q,int v)
{
            node *n;
            n = makenode();
            n->info = v;
            n->next = NULL ;
            if(q->rear == NULL)
            {
                        q->rear = n ;
                        q->front = n ;
            }
            else
            {
                        q->rear->next = n ;
                        q->rear = n ;
            }
}

int del(queuelst *q)
{
            int v ;
            node *t ;
            if(q->front == NULL)
            {
                        printf("\n Queue is Empty") ;
                        return NULL;
            }

            v = q->front->info ;
            t = q->front ;

            if(q->front == q->rear)
            {
                        q->front = NULL ;
                        q->rear = NULL ;
            }
            else
                        q->front = t->next ;

            free(t);
            return v;
}

 


QUEUELST.CPP

//   DSTC ---- QUEUELST.CPP ..queues using Linked Lists
// ... including QUEUELST.H ..header file

#include "queuelst.h"
#include <conio.h>

void main()
{
 int ch,i;
 queuelst *q;
 q=makequeue();
 clrscr();
 while(1)
 {
 printf("Enter the choice (1-insert, 2-delete, 3-quit): ");
  scanf("%d", &ch);
  if(ch==3)
    break;
  else if(ch==1)
    {
     printf("Enter information to insert: ");
     scanf("%d", &i);
             insert(q,i);
    }
  else if(ch==2)
    {
             i=del(q);
             if(i!=NULL)
               printf("Information is: %d\n", i);
            }
  else
             printf("Invalid choice.......enter a value between 1 and 3\n");
 }
}

No comments: