Tuesday, January 30, 2024

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

Ordered Linked Lists

Implemented using C Language

C Language Code:

              ORDERLST.H              

// DSTC - SIMPLE LINK LIST... orderlst.h
// this is ORDERED Link List.


#include "assert.h"

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

typedef struct linklist
{
node *first;
}list;

list *makelist(void)
{
list *l;
l = (list *)malloc (sizeof(list));
// printf("\nMaking a list List Size = %d",sizeof(list));
l->first = NULL;
return (l);
}


// adding a node in the list
// depending upon its value as the list is in Ascending order ...

void addnode(int n,list *l)
{
node *nd,*t,*t1 ;
nd = (node *)malloc(sizeof(node));
nd->info = n ;
if (l->first == NULL)
{
l->first = nd;
nd->next = NULL;
}
else
{
t = l->first;
while(t !=NULL && t->info<n)
{
// loop will continue till end of the list
// or whenever any value in the nodes is less than the new value
t1 = t ;
t = t->next ;
}
if (t == NULL)
{
// printf("\nValue added in END");
t1->next = nd;
nd->next=NULL;
}
else if (t == l->first)
{
// adding as the first node... becoz it is smaller than all..
nd->next = t ;
l->first = nd ;
}
else
{
// adding in between the list....
t1->next = nd ;
nd->next = t ;
} }
}
// deletes the node added in the last,
// ie the first node from the begining of the list
// and then assigning the next poniters accordingly ...

void delnode(list *l,int v)
{
assert(l->first != NULL);
node *temp=l->first , *temp1=NULL;
if (l->first==NULL)
printf("\nList not Found");
else
{
while(temp!=NULL)
{
if(temp->info==v)
break ;
temp1 = temp;
temp = temp1->next ;
}
if (temp == l->first)
{
l->first = temp->next ;
free(temp);
}
else if (temp==NULL)
printf("\n Node not Found");
else
{
temp1->next = temp->next ;
free(temp);
}
}
}

void killlist(list *l)
{
node *save,*temp ;
save = l->first;
while(save)
{
temp = save->next ;
free(save);
save = temp;
}
free(l);
}

void printlist(list *l)
{
node *save ;
save = l->first ;
while (save != NULL)
{
printf("%d, ",save->info);
save = save->next ;
}
}


list *reverselist(list *p)
{
list *temp ;
node *save;
temp = makelist();
while (p->first)
{
save = p->first ;
p->first = p->first->next;
save->next = temp->first;
temp->first = save;
}
return (temp);
}

void splitlist(list *l,list *newl,int nd)
{
assert(l->first != NULL);
assert(nd!=0);

node *temp=l->first , *temp1=NULL;

if (l->first==NULL)
printf("\nList not Found");
else
{
while(temp!=NULL )
{ temp1=temp;
temp = temp->next ;
if(temp->info==nd)
break ;
}
newl->first=temp;
temp1->next=NULL;
}

         ORDERLST.CPP        

// DSTC - ORDERED LINK LIST ----- ORDERLST.CPP
// including a Header file ORDERLST.H


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include "orderlst.h"

main()
{
list *l,*s;
int n;
clrscr();
l=makelist();
s=makelist();

addnode(11,l) ;
addnode(2,l) ;
addnode(13,l) ;
addnode(34,l) ;
addnode(25,l) ;
addnode(-22,l) ;
printlist(l);

printf("\nEnter the NODE's Value to split from:");
scanf("%d",&n);

splitlist(l,s,n);

printf("\nAfter Splitting the list from %d",n);
printf("\n List1 >> ");
printlist(l);
printf("\n List2 >> ");
printlist(s);

l = reverselist(l);
printf("\nAfter Reversing the List\n");
printlist(l);

killlist(l);
killlist(s);
}

No comments: