Tuesday, January 30, 2024

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

 

Linked Lists 
Implemented using C Language

C Language Code:

        LINKLST1.H     

// DSTC - Linklst1.h - SIMPLE LINK LIST...
// list of operations are

//1. list *makelist(void)
//2. void addnodeatend(int n,list *l)
//3. void addnodeatbegin(int n,list *l)
//4. void delnode(list *l,int v)
//5. void killlist(list *l)
//6. void printlist(list *l)
//7. void appendnode(list *l,int v)
//8. list *reverselist(list *p)
//9. void splitlist(list *l,list *newl,int nd)
//10. int countlist(list *l)


#include "assert.h"

// defining one node containing the value(info) and pointer to next node
// i.e. *next

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 END of the list
void addnodeatend(int n,list *l)
{
node *nd,*t ;
nd = (node *)malloc(sizeof(node));
nd->info = n ;
nd->next = NULL;

if (l->first == NULL) // means list was empty and it is the first node
l->first = nd;
else
{
t = l->first;
while(t->next !=NULL ) // transversing the whole list to move in END
{
t = t->next ;
}
t->next = nd ; // assigning the node's address in the last node *NEXT
}
}

// adds the node in the BEGINING of the list
void addnodeatbegin(int n,list *l)
{
node *nd ;
nd = (node *)malloc(sizeof(node));
nd->info = n;
nd->next = l->first;
l->first = nd ;
}

// deletes the node with the value of "v" in the list "l",
// if not found then gives the message NODE not found
// otherwise deletes it and reassign the pointers NEXT

void delnode(list *l,int v)
{
assert(l->first != NULL);

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

if (l->first==NULL)


Get Paid by Reading Ads on your Mobiles

printf("\nList not Found"); // when list is empty
else
{
while(temp!=NULL)
{
if(temp->info==v) // if value is found then break the loop
break ;
temp1 = temp; // holds the address of the previous node
temp = temp1->next ; // moves to the next node
} if (temp == l->first) // if the found nodes value is the First Node
{
// assigning the address to the first of the next node
l->first = temp->next ;
free(temp); // deleting the found node
}
else if (temp==NULL)
printf("\n Node not Found");
else
{
// if node is existing in between the list....
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 ;
}
}

/*
void appendnode(list *l,int v)
{
node *n,*k ;
n=(node *)malloc(sizeof(node));
n->info = v ;
n->next = NULL ;
if (l->first == NULL)
l->first = n ;
else
{
k = l->first ;
while (k->next != NULL)
{
k = k->next ;
}
k->next = n ;
}
}
*/

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);


Get Paid by Reading Ads on your Mobiles

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;
}
}

int countlist(list *l)
{
node *t = l->first ;
int c=0;

while (t != NULL)
{
t = t->next ;
c++;
}
return (c);
}

          LINKLST1.CPP         

// DSTC - LINKLST1.CPP - SIMPLE/LINEAR Link List
// including the file LINKLST1.H


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

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

addnodeatend(11,l) ;
addnodeatend(22,l) ;
addnodeatend(33,l) ;
addnodeatend(44,l) ;
addnodeatend(55,l) ;
printlist(l);
printf("\n No. of Nodes in the List are : %d \n",countlist(l));
printf("\nEnter the value you want to Delete:");
scanf("%d",&n);
printf("\nDeleteing %d >> enter to BEgin...",n);
getchar();
getchar();

delnode(l,n);
printf("\nAfter Deleting a Node \n");
printlist(l);
printf("\n No. of Nodes in the List are : %d \n",countlist(l));

printf("\nEnter Value to be Added in the BEGINING:");
scanf("%d",&n);
addnodeatbegin(n,l);
printf("\nAfter Adding a Node in the Begining \n");
printlist(l);
printf("\n No. of Nodes in the List are : %d \n",countlist(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);

printf("\n No. of Nodes in the First List are : %d \n",countlist(l));
printf("\n No. of Nodes in the Second List are : %d \n",countlist(s));

/* l = reverselist(l);
printf("\nAfter Reversing the List\n");
printlist(l);
*/
killlist(l);
}

No comments: