Tuesday, January 30, 2024

Circular Linked Lists (CLL) - DSTC using C Language (Source Code Implemented)

Circular Linked Lists (CLL) 
Implemented using C Language 

C Language Code:

              CLIST.H          

// DSTC ---- CLIST.H Circular Link List .... Header File

#include <stdio.h>
#include <stdlib.h>
typedef struct nd
{
int info;
struct nd *next;
}Node;

typedef struct {
Node *start;
}Clinklist;

Clinklist * GetClinklist()
{
Clinklist *temp;
temp=(Clinklist *)malloc(sizeof(Clinklist));
temp->start=NULL;
return temp;
}

Node * GetNode()
{
Node *n;
n=(Node *)malloc(sizeof(Node));
n->next=NULL;
return n;
}

int empty(Clinklist *l)
{
if(l->start==NULL)
return 1;
else
return 0;
}

//Obtaining address of last node
Node * LastNode(Clinklist *l)
{
Node *t;
if(empty(l))
return NULL;

t=l->start;
while(t->next!=l->start)
t=t->next;
return t;
}

//Count number of nodes
int CountNodes(Clinklist *l)
{
int c;
Node *t;

if(empty(l))
return 0;

c=1;
t=l->start;
while(t->next!=l->start)
{
c++;
t=t->next;
}
return c;
}

// Inserting a new node in beginning
void InsBeg(Clinklist *l, int i)
{
Node *n,*t;
n=GetNode();
n->info=i;
if(!empty(l))
{
t=LastNode(l);
n->next=l->start;
t->next=n;
l->start=n;
}
else
{
l->start=n;
n->next=n;
}
}
// Deleting a node from beginning
int DelBeg(Clinklist *l)
{
Node *t,*p;
if(empty(l))
{
printf("Clinklist already empty\n");
return 0; //unsussefull operation
}
t=l->start;
if(CountNodes(l)==1)
l->start=NULL;
else
{
p=LastNode(l);
l->start=t->next;
p->next=l->start;
}
free(t);
return 1; //successfull operation
}

// Inserting a new node at end
void InsEnd(Clinklist *l, int i)
{
Node *n;
Node *t;
n=GetNode();
n->info=i;
if(empty(l))
{
l->start=n;
n->next=n;
}
else
{
//first of all we will go to last node
t=LastNode(l);
t->next=n;
n->next=l->start;
}
}

// Deleting a node from end
int DelEnd(Clinklist *l)
{
Node *t,*p;
//if linklist is empty
if(empty(l))
{
printf("Clinklist already empty\n");
return 0; //unsussefull operation
}

t=l->start;
//if linklist contain only one node
if(l->start->next==l->start)
{
l->start=NULL;
free(t);
return 1;
}

//if linklist contains more than one node
p=NULL;
while(t->next!=l->start)
{
p=t;
t=t->next;
}

p->next=l->start;
free(t);
return 1; //successfull operation
}

// Inserting a new node at specific positon
int InsPos(Clinklist *l, int i, int pos)
{
Node *n;
Node *t;

if(pos>CountNodes(l))
{
printf("Syntax error ... this many nodes does not exist");
return 0; //unsuccessull operation
}

//if node is to be inserted at first position
if(pos==1)
{
InsBeg(l,i);
return 1;
}

n=GetNode();
n->info=i;

//obtain a node just before the inserted position
t=l->start;
while(pos>2)
{
t=t->next;
pos--;
}
n->next=t->next;
t->next=n;
return 1;
}

// Deleting a node at specific position
int DelPos(Clinklist *l,int pos)
{
Node *t,*p;
//check number of nodes
if(pos>CountNodes(l))
{
printf("Syntax error ... this many nodes does not exist");
return 0; //unsuccessfull operation
}

//if node is to be deleted from first position
if(pos==1)
{
DelBeg(l);
return 1;
}

//obtain a node just before the deleted position
t=l->start;
while(pos>2)
{
t=t->next;
pos--;
}

p=t->next;
t->next=p->next;
free(p);
return 1;
}

void Displist(Clinklist *l)
{
Node *t;
int i=1;
int c;
t=l->start;
printf("\n----Link list details----\n");
c=CountNodes(l);
printf("Total nodes are: %d \n\n", c);
while(c>0)
{
printf("Node number %d is: %d\n", i, t->info);
i++;
t=t->next;
c--;
}
printf("\n--------------------------------\n");
}
 

       CLIST.CPP    

// DSTC -- CLIST.CPP Circular Link List .cpp
// using CLIST.H header file ... .

#include "clist.h"
#include <conio.h>
void main()
{
int ch;
int menu(void);
int GetInfo(void);
int GetPos(void);
Clinklist *l;

l=GetClinklist();
clrscr();

while(1)
{
ch=menu();
if(ch==8)
break;
switch(ch) {
case 1:
InsBeg(l,GetInfo());
break;
case 2:
InsEnd(l,GetInfo());
break;
case 3:
InsPos(l,GetInfo(),GetPos());
break;
case 4:
DelBeg(l);
break;
case 5:
DelEnd(l);
break;
case 6:
DelPos(l,GetPos());
break;
case 7:
Displist(l);
break;
}
}
}

int menu(void)
{
int ch;
while(1)
{
printf("\n Main Menu\n");
printf(" ---------\n");
printf(" 1. Insert Node in beginning\n");
printf(" 2. Insert Node at end\n");
printf(" 3. Insert Node at specific position\n");
printf(" 4. Deleting Node from beginning\n");
printf(" 5. Deleting Node from end\n");
printf(" 6. Deleting Node from particular position\n");
printf(" 7. Displaying a linklist\n");
printf(" 8. Quit\n");
printf("\n Enter a Choice: ");
scanf("%d", &ch);
if(ch < 1 || ch > 8)
printf("Invalid choice.... try again\n");
else
return ch;
}
}

int GetInfo(void)
{
int i;
printf("Enter Information: ");
scanf("%d", &i);
return i;
}

int GetPos(void)
{
int i;
printf("Enter Position: ");
scanf("%d", &i);
return i;
}

No comments: