Wednesday, January 31, 2024

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

 

STACKs (Using Linked Lists)
Implemented using C Language

C Language Code:


STACKLST.H

// DSTC - STACKLST.H .... Stacks using Linked Lised ...

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

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

typedef struct stacklist
{
            node *top ;
}stacklst ;

stacklst *makestacklst()
{
            stacklst *t ;
            t = (stacklst *)malloc(sizeof(stacklst)) ;
            t->top = NULL ;
            return t;
}

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

int empty(stacklst *s)
{
            if (s->top == NULL)
                        return 1 ;
            else
                        return 0 ;
}

void push(stacklst *s,int v)
{
            node *n ;
            n = makenode();
            n->info = v;
            n->next = s->top ;
            s->top = n ;
}

int pop(stacklst *s)

Get Paid by Reading Ads on your Mobiles

{
            int v;
            node *t;
            if (empty(s))
            {
                        printf("\nStack Empty");
                        return NULL;
            }

 

            v = s->top->info ;
            t = s->top ;
            s->top = t->next ;
            free(t) ;
            return (v) ;
}


STACKLST.CPP

//   DSTC ---- STACKLST.CPP ..stacks using Linked Lists
// ... including STACKLSt.H ..header file

#include "stacklst.h"
#include <conio.h>
void main()
{
 int ch,i;
 stacklst *s1;
 s1=makestacklst();
 clrscr();
 while(1)
 {
  printf("Enter the choice (1-push, 2-pop, 3-quit): ");
  scanf("%d", &ch);
  if(ch==3)
    break;
  else if(ch==1)
    {
     printf("Enter information to insert: ");
     scanf("%d", &i);
     push(s1,i);
    }
  else if(ch==2)
    {
     i=pop(s1);
     if(i!=NULL)
       printf("Information is: %d\n", i);

    }
  else
     printf("Invalid choice.......enter a value between 1 and 3\n");
 }
}

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

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

Generalised Linked Lists

Implemented using C Language
 

C Language Code:

              GLIST.CPP         

// Generalized Linked Lists...

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

typedef struct s
{
int type;
union u
{
int iinfo;
float finfo ;
char sinfo[20] ;
}uu ;
struct s *next ;
}node ;


typedef struct s1
{
node *first;
}glist;


node* createnode()
{
float p ;

node *n;
char flval[20] ;
n=(node*)malloc(sizeof(node)) ;
printf("\n Enter the type of value to input 1-int 2-float 3-string");
scanf("%d",&n->type) ;


fflush(stdin) ;
switch(n->type)
{
case 1:
printf("\n Enter the int value") ;
scanf("%d",&n->uu.iinfo) ;
//printf("\n %d",n->uu.iinfo);
break;
case 2:
printf("\n Enter the float value") ;
float ab ;
scanf("%f", &n->uu.finfo) ;
n->uu.finfo = ab ;
// printf("\n %f",n->uu.finfo) ;
break;
case 3:
printf("\n Enter the string value") ;
scanf("%s",n->uu.sinfo) ;
//printf("\n %s",n->uu.sinfo) ;
break;
}
n->next=NULL;
return n;
}

glist* createlist(void)
{
glist* g;
g=(glist*)malloc(sizeof(glist));
g->first=NULL;
return g;
}

Sparse Matrix - DSTC using C Language (Source Code Implemented)

Sparse Matrix 
Implemented using C Language 
 Get Paid by Reading Ads on your Mobiles
 

C Language Code:

              SPARSE.H          

// DSTC ---- SPARSE.H .... sparse matrix ...header file
// it is matrix having only few elements as values and rest are zeros ...

#include<stdio.h>
#include<alloc.h>
#include<assert.h>

typedef struct s
{
int val;
int row,col;
struct s *up, *left ;
}node ;

typedef struct sparselist
{
node *arows;
node *acols;
int trows,tcols; // total no of rows and cols..
}sp ;
// prototype of the function ...
int output(sp *s,int r,int c) ;

sp *makesparse(int r,int c)
{
int i ;
sp *s1=(sp*)malloc(sizeof(sp));
s1->trows = r ;
s1->tcols = c;
s1->arows = (node *)malloc(sizeof(node)*r);
s1->acols = (node *)malloc(sizeof(node)*c);
for(i=0;i<s1->trows ; i++)
{
s1->arows[i].row = i ;
s1->arows[i].col = -1 ;
s1->arows[i].left = &s1->arows[i] ;
s1->arows[i].up = NULL ;
s1->arows[i].val = -1 ;
}
for(i=0;i<s1->tcols ; i++)
{
s1->acols[i].col = i ;
s1->acols[i].row = -1 ;
s1->acols[i].left = NULL ;
s1->acols[i].up = &s1->acols[i] ;
s1->acols[i].val = -1 ;
}
return (s1);
}

void addsparsenode(int r, int c, int v, sp* s)
{
node *n,*start;
assert(r>=0 && r<s->trows && c>=0 && c<s->tcols);
assert(v!=0) ;
n = (node *)malloc(sizeof(node));
n->val = v ;
n->row = r ;
n->col = c ;
start = &s->arows[r] ;
while(start->left->col > c)
start = start->left ;

if (start->left->col != c)
{
n->left = start->left ;
start->left = n ;
}

start = &s->acols[c] ;
while(start->up->row > r)
start = start->up ;

if (start->up->row != r)
{
n->up = start->up ;
start->up = n ;
}
}

void printsparse(sp *s)
{
int i,j ;
for(i=0;i<s->trows;i++)
{
printf("\n");
for(j=0;j < s->tcols; j++)
{
printf("\t %d",output(s,i,j));
}
}
}
int output(sp *s,int r,int c)
{
node *start= &s->arows[r] ;
while(start->left->col > c)
start=start->left ;

if (start->left->col == c)
return (start->left->val);
else
return 0;

}

       SPARSE.CPP       

// DSTC --- SPARSE.CPP --- sparse matrix ...
// using the file ... SPARSE.H

#include "sparse.h"
#include <stdlib.h>
#include <conio.h>

main()
{
sp *s1 = makesparse(3,3) ;
// addsparsenode(int r, int c, int v, sp* s)
clrscr();
addsparsenode(2,2,22,s1);
addsparsenode(2,1,21,s1);
addsparsenode(0,2,2,s1);
addsparsenode(1,1,11,s1);
printsparse(s1);
}

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

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

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