0% found this document useful (0 votes)
12 views

Single Linked List

pdf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Single Linked List

pdf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Galaxy Computer Education, Chopda … Practical Of Single Linked List

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node * link;
};
struct Node * START=NULL;
struct Node *Searching(int);

void showList();
void addToFirst();
void addToLast();
void deleteFirstNode();
void deleteLastNode();
int countNodes();
void sum();
void addBefore(struct Node *);
void addAfter(struct Node *);
void addNodeAtIndex(int);
void deleteGiven(struct Node *);
void deleteNodeAtIndex(int);
void deleteAllNodes();

void main()
{
int ch,res,key;
struct Node *LOC=NULL;
system("cls");

REP:

printf("\n\t Working with linked list...");


printf("\n\t 1. Add to First ....");
printf("\n\t 2. Add to Last ....");
printf("\n\t 3. Search An Element ....");
printf("\n\t 4. Delete Last Node ....");
printf("\n\t 5. Delete First Node ....");
printf("\n\t 6. Count Nodes In List ....");
printf("\n\t 7. Show List ....");
printf("\n\t 8 Sum Of The List ....");
printf("\n\t 9. Add After Given Node ....");

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

printf("\n\t 10. Add Before Given Node ....");


printf("\n\t 11. Add Node At Given Index ... ");
printf("\n\t 12. Delete Given Node....");
printf("\n\t 13. Delete Node At Given Index .... ");
printf("\n\t 14. Delete All Elements .... ");

printf("\n\t 15. Exit");

printf("\n\t Enter your choice =>");


scanf("%d",&ch);

switch(ch)
{
case 1:
addToFirst();
break;

case 2:
addToLast();
break;

case 3:
printf("\n\t Enter The Search Key => ");
scanf("%d",&key);
LOC= Searching(key);
if(LOC!=NULL)
{
printf("\n\t Item Found ....");
}
else
{
printf("\n\t Item Not Found ....");
}
break;

case 4:
deleteLastNode();
break;
case 5:
deleteFirstNode();
break;
case 6:
res = countNodes();
printf("\n\tTotal Nodes In List Are : %d ",res);
break;

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

case 7:
showList();
break;

case 8:
sum();
break;

case 9:
printf("\n\t Enter The Information To Add After => ");
scanf("%d",&key);
LOC= Searching(key);
if(LOC!=NULL)
{
addAfter(LOC);
}
else
{
printf("\n\t Item Not Found ....");
}
break;

case 10:
printf("\n\t Enter The Information To Add After => ");
scanf("%d",&key);
LOC= Searching(key);
if(LOC!=NULL)
{
addBefore(LOC);
}
else
{
printf("\n\t Item Not Found ....");
}
break;

case 11:
printf("\n\t Enter The Index Number => ");
scanf("%d",&key);

res = countNodes();

if(key<res)
{

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

addNodeAtIndex(key);
}
else
{
printf("\n\t Index Number Is Out Of Range ....");
}
break;

case 12:
printf("\n\t Enter The Information To Delete => ");
scanf("%d",&key);
LOC= Searching(key);
if(LOC!=NULL)
{
deleteGiven(LOC);
}
else
{
printf("\n\t Item Not Found ....");
}
break;
case 13:
printf("\n\t Enter The Index Number => ");
scanf("%d",&key);

res = countNodes();

if(key<res)
{
deleteNodeAtIndex(key);
}
else
{
printf("\n\t Index Number Is Out Of Range ....");
}
break;
case 14:
deleteAllNodes();
break;

case 15:
printf("\n\t Thankk You .... ");
break;

default:

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

printf("\n\tInvalid Choice ...... ");


}

printf("\n\tPress Any Key To Continue ...... ");


getch();

if(ch!=15)
{
system("cls");
goto REP;
}

printf("\n\tEnd Of Program ...... ");


}

void showList()
{
if(START==NULL)
{
printf("\n\tList Is EMPTY !!! ");
}
else
{
struct Node *PTR=NULL;
PTR=START;

printf("\n\tList Of Elements Are ...... ");


while(PTR!=NULL)
{
printf("\n\t=> %d ",PTR->info);
PTR=PTR->link;
}
printf("\n\tEnd Of List ......");
}
}

int countNodes()
{
int count=0;
struct Node *PTR=NULL;
PTR=START;

while(PTR!=NULL)

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

{
count++;

PTR=PTR->link;
}
return(count);
}

void addToFirst()
{
struct Node *NewNode=NULL;
NewNode=(struct Node *)malloc(sizeof(struct Node));

printf("\n\tEnter The Information :=> ");


scanf("%d",&NewNode->info);

NewNode->link = NULL;

if(START==NULL)
{
START=NewNode;
printf("\n\tFirst Node %d Is Added To List .... ",NewNode->info);
}
else
{
NewNode->link=START;
START=NewNode;
printf("\n\tNode %d Is Added At First In To List .... ",NewNode->info);
}
}

void addToLast()
{
struct Node *NewNode=NULL;
NewNode=(struct Node *)malloc(sizeof(struct Node));

printf("\n\tEnter The Information :=> ");


scanf("%d",&NewNode->info);

NewNode->link = NULL;

if(START==NULL)

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

{
START=NewNode;
printf("\n\tFirst Node %d Is Added To List .... ",NewNode->info);
}
else
{
struct Node *PTR=NULL;
PTR=START;

while(PTR->link!=NULL)
{
PTR=PTR->link;
}

PTR->link=NewNode;

printf("\n\tNode %d Is Added At Last In To List .... ",NewNode->info);


}
}

void addBefore(struct Node *Location)


{
if(Location==START)
{
addToFirst();
}
else
{
struct Node *NewNode=NULL;
struct Node *Prev = NULL;

NewNode=(struct Node *)malloc(sizeof(struct Node));

printf("\n\tEnter The Information :=> ");


scanf("%d",&NewNode->info);

NewNode->link = NULL;

Prev = START;
while(Prev->link !=Location)
{
Prev=Prev->link;
}

NewNode->link=Location;

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

Prev->link = NewNode;

}
}
void addAfter(struct Node *Location)
{
struct Node *NewNode=NULL;

NewNode=(struct Node *)malloc(sizeof(struct Node));

printf("\n\tEnter The Information :=> ");


scanf("%d",&NewNode->info);

NewNode->link = NULL;

NewNode->link=Location->link;
Location->link = NewNode;
}
void addNodeAtIndex(int index)
{
struct Node *PTR=NULL;
int count=1;
PTR=START;
while(count<index)
{
PTR=PTR->link;
count++;
}

addBefore(PTR);
}
void deleteGiven(struct Node *Location)
{
if(Location==START)
{
START=START->link;
free(Location);
}
else
{
struct Node *PTR=NULL;
PTR=START;
while(PTR->link!=Location)
{

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

PTR=PTR->link;
}

PTR->link=Location->link;
free(Location);
}
}

void deleteAllNodes()
{
struct Node *PTR=NULL;
while(START!=NULL)
{
PTR=START;
START=START->link;
free(PTR);
}
}
void deleteNodeAtIndex(int index)
{
struct Node *Location=NULL;
int count=1;
Location=START;
while(count<index)
{
Location=Location->link;
count++;
}

deleteGiven(Location);

void deleteFirstNode()
{
if(START==NULL)
{
printf("\n\t List Is Empty ....");
printf("\n\t Not Eble To Delete ....");
}
else
{
struct Node *PTR=NULL;
PTR=START;

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

START=START->link;
free(PTR);
printf("\n\t First Node Deleted ....");
}
}
void deleteLastNode()
{
if(START==NULL)
{
printf("\n\t List Is Empty ...");
printf("\n\t Unable to delete ....");
}
else if(START->link==NULL)
{
free(START);
START=NULL;
}
else
{
struct Node *PRV=NULL;
struct Node *PTR=NULL;

PTR=START;

while(PTR->link!=NULL)
{
PRV=PTR;
PTR=PTR->link;
}
PRV->link=NULL;
free(PTR);
}
printf("\n\t Last Node Deleted Successfully ....");
}
void sum()
{
struct Node *PTR=NULL;
int total=0;

PTR=START;

while(PTR!=NULL)
{
total=total+PTR->info;
PTR=PTR->link;

Program Developed By : Bsc and Polytechnic Students, Chopda


Galaxy Computer Education, Chopda … Practical Of Single Linked List

}
printf("\n\t The Sum Of All The Nodes Is => %d",total);
}

struct Node *Searching(int item)


{
struct Node *PTR=NULL;
struct Node *LOC=NULL;

PTR=START;

while(PTR!=NULL)
{
if(item==PTR->info)
{
LOC=PTR;
break;
}
PTR=PTR->link;
}
return(LOC);
}

Program Developed By : Bsc and Polytechnic Students, Chopda

You might also like