Showing posts with label source code of DOUBLE LINKED LISTs. Show all posts
Showing posts with label source code of DOUBLE LINKED LISTs. Show all posts

Tuesday, January 30, 2024

DOUBLE LINKED LISTs - DSTC using C Language (Source Code Implemented)

DOUBLE LINKED LISTs 
Implemented using C Language 

Functionality Implemented in this Program:

  1. typedef struct nd

  2. typedef struct Dlinklist

  3. Dlinklist * GetDlinklist()

  4. Node * GetNode()

  5. int empty(Dlinklist *l)

  6. Node * LastNode(Dlinklist *l)

  7. int CountNodes(Dlinklist *l)

  8. void InsBeg(Dlinklist *l, int i)

  9. int DelBeg(Dlinklist *l)

  10. void InsEnd(Dlinklist *l, int i)

  11. int DelEnd(Dlinklist *l)

  12. int InsPos(Dlinklist *l, int i, int pos)

  13. int DelPos(Dlinklist *l,int pos)

  14. void Displist(Dlinklist *l)

C Language Code:

              DLIST.H          

// DSTC --- DLIST.H -- DOUBLE LINK LIST ...

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


typedef struct nd
{
int info;
struct nd *next;
struct nd *prev;
}Node;

typedef struct {
Node *start;
}Dlinklist;

Dlinklist * GetDlinklist()
{
Dlinklist *temp;
temp=(Dlinklist *)malloc(sizeof(Dlinklist));
temp->start=NULL;
return temp;
}

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

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

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

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

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

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

// Inserting a new node in beginning
void InsBeg(Dlinklist *l, int i)
{
Node *n;
n=GetNode();
n->info=i;
if(!empty(l))
{
n->next=l->start;
l->start->prev=n;
}
l->start=n;
}

// Deleting a node from beginning
int DelBeg(Dlinklist *l)
{
Node *t;
if(empty(l))
{
printf("Dlinklist already empty\n");
return 0; //unsussefull operation
}
t=l->start;
l->start=t->next;
l->start->prev=NULL;
free(t);
return 1; //successfull operation
}

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

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

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

//if linklist contains more than one node
while(t->next!=NULL)
t=t->next;

t->prev->next=NULL;
free(t);
return 1; //successfull operation
}

// Inserting a new node at specific positon

int InsPos(Dlinklist *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->prev=n;
t->next=n;
n->prev=t;
return 1;
}

 

// Deleting a node at specific position


int DelPos(Dlinklist *l,int pos)
{
Node *t;
//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 to be deleted
t=l->start;
while(pos>=2)
{
t=t->next;
pos--;
}

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

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

       DLIST.CPP    

// DSTC -- DLIST.CPP ---- DOUBLE LINK LIST ....
// Using the include file DLIST.H

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

l=GetDlinklist();
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;
}