Singly Linked List (SLL) Program C++/C

File Name :- sll.cpp
#include<iostream.h>
#include<conio.h>
//NOde Definition
//functions definitions
struct node{
int info;
struct node *link;
};
typedef struct node NODE;
NODE *start=NULL ,*n=NULL;
//creation of SLL
void createsll(){
int item,ch=1;
while(ch==1){
clrscr();
n= new NODE;
if(n==NULL){
cout<<"Overflow Condition"<<endl;
getch();
return;
}
cout<<"Enter an item"<<endl;
cin>>item;
n->info=item;
if(start==NULL){
n->link=NULL;}
else{
n->link=start;}
start=n;
cout<<"Do You want to add another node \npress 1 if YES \npress 0 if NO"<<endl;
cin>>ch;
}
getch();
return;
}
//traversing of SLL
void traversesll(){
NODE *temp=start;
int i=0;
clrscr();
if(temp==NULL){
cout<<"Linked list is Empty"<<endl;
getch();
return;
}
cout<<"Linked list is:-"<<endl;
while(temp!=NULL){i++;
cout<<"Node No:-"<<i<<endl<<"info part="<<temp->info<<endl<<"Link Part="<<temp->link<<endl;
temp=temp->link;
}
cout<<"Linked List is Traversed"<<endl<<i<<"Nodes are Visited."<<endl<<"start="<<start;
getch();
return;
}
//Searching in SLL
void searchsll(int item){
NODE *temp=start;
int i=0;
clrscr();
if(temp==NULL){
cout<<"Linked list is not Exist"<<endl;
getch();
return;
}
while(temp!=NULL){i++;
if(item==temp->info){
cout<<item<<" is Found at Node Number="<<i<<endl<<"info part="<<temp->info<<endl<<"Link Part="<<temp->link<<endl;
getch();
return;
}
temp=temp->link;
}
cout<<item<<" is not found in SLL"<<endl;
getch();
return;
}
//Reverse SLL
void reversesll(){
NODE *p=start, *q=NULL, *r=NULL;
while(p!=NULL){
r=q;
q=p;
p=p->link;
q->link=r;
}
start=q;
cout<<"SLL is Reversed"<<endl;
getch();
return;
}
//sort SLL
void sortsll(NODE *s){
NODE *i,*j;
int swap;
if(s==NULL){
cout<<"Linked List is Not Exist"<<endl;
getch();
return;
}
for(i=s;i!=NULL;i=i->link){
for(j=i->link;j!=NULL;j=j->link){
if(i->info>j->info){
swap=i->info;
i->info=j->info;
j->info=swap;
}}}
cout<<"SLL is Sorted"<<endl;
getch();
return;
}
//Insert first node in SLL
void insertfirstsll(){
int item;
clrscr();
n= new NODE;
if(n==NULL){
cout<<"Overflow Condition"<<endl;
getch();
return;
}
cout<<"Enter an item"<<endl;
cin>>item;
n->info=item;
n->link=start;
start=n;
cout<<"First Node is inserted in SLL"<<endl;
getch();
return;
}
//insert last node in SLL
void insertlastsll(){
NODE *temp=start;
int item;
clrscr();
n= new NODE;
if(n==NULL){
cout<<"Overflow Condition"<<endl;
getch();
return;
}
cout<<"Enter an item"<<endl;
cin>>item;
n->info=item;
n->link=NULL;
if(temp==NULL){
start=n;
}
else{
while(temp->link!=NULL){
temp=temp->link;}
temp->link=n;
}
cout<<"Last Node is inserted in SLL"<<endl;
getch();
return;
}

//delete first node from SLL
void deletefirstsll(){
clrscr();
n=start;
if(n==NULL){
cout<<"Underflow Condition"<<endl;
getch();
return;
}
start=start->link;
cout<<"First Node is Deleted from SLL"<<endl;
getch();
delete n;
return;
}
//delete last node from SLL
void deletelastsll(){
NODE *temp=start;
clrscr();
if(start==NULL){
cout<<"Underflow Condition"<<endl;
getch();
return;
}
if(start->link==NULL){
n=start;
start=NULL;
}
else{
while(temp->link->link!=NULL){
temp=temp->link;
}
n=temp->link;
temp->link=NULL;
}
cout<<"last Node is Deleted from SLL"<<endl;
getch();
delete n;
return;
}

void main(){
int ch=0,item=0;
NODE *loc=NULL;
while(1){
clrscr();
cout<<"Singly Linked List Operations:-\n1.Creation\n2.Traversing\n3.Searching\n4.Insertion\n5.Deletion\n6.Sorting\n7.Reverse\n8.Exit\nEnter Your Choice = "<<endl;
cin>>ch;
switch(ch){
case 1: createsll();
break;
case 2: traversesll();
break;
case 3:
cout<<"Enter an item to search"<<endl;
cin>>item;
searchsll(item);
break;
case 4: cout<<"Insertion:-\n1.insert first node\n2.insert last node\nenter your choice"<<endl;
cin>>ch;
switch(ch){
case 1: insertfirstsll();
break;
case 2:
insertlastsll();
break;
}
break;
case 5:
cout<<"Deletion:-\n1.Delete first node\n2.Delete last node\nenter your choice"<<endl;
cin>>ch;
switch(ch){
case 1: deletefirstsll();
break;
case 2: deletelastsll();
break;
}
break;
case 6:
sortsll(start);
break;
case 7:
reversesll();
break;
case 8:
exit(0);
default:
cout<<"Wrong Choice\nTry Again!!"<<endl;
getch();
}
}
}


File Name :- sll.c

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
//Node Definition
struct node{
int info;
struct node *link;
};
typedef struct node NODE;
NODE *start=NULL ,*n=NULL;
//create SLL
void createsll(){
int item,ch=1;
while(ch==1){
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
if(start==NULL){
n->link=NULL;}
else{
n->link=start;}
start=n;
printf("Do You want to add another node \npress 1 if YES \npress 0 if NO\n");
scanf("%d",&ch);
}
getch();
return;
}
//traverse SLL
void traversesll(){
NODE *temp=start;
int i=0;
clrscr();
if(temp==NULL){
printf("Linked list is Empty\n");
getch();
return;
}
printf("Linked list is:-\n");
while(temp!=NULL){i++;
printf("Node No:-%d\n info part= %d\nLink Part=%u\n",i,temp->info,temp->link);
temp=temp->link;
}
printf("Linked List Traversed \n %d Node's are Visited.\nstart=%u",i,start);
getch();
return;
}
//Search in SLL
void searchsll(int item){
NODE *temp=start;
int i=0;
clrscr();
if(temp==NULL){
printf("Linked list is not  Exist\n");
getch();
return;
}
while(temp!=NULL){i++;
if(item==temp->info){
printf("%d is Found\nNode Number=%d\n info part= %d\nLink Part=%u\n",item,i,temp->info,temp->link);
getch();
return;
}
temp=temp->link;
}
printf("%d is not found in SLL\n",item);
getch();
return;
}
//Reverse SLL
void reversesll(){
NODE *p=start,*q=NULL,*r=NULL;
while(p!=NULL){
r=q;
q=p;
p=p->link;
q->link=r;
}
start=q;
printf("Single Linked List Reversed\n");
getch();
return;
}
//sort SLL
void sortsll(NODE *s){
NODE *i,*j;
int swap;
if(s==NULL){
printf("Linked List is Not Exist\n");
getch();
return;
}
for(i=s;i!=NULL;i=i->link){
for(j=i->link;j!=NULL;j=j->link){
if(i->info>j->info){
swap=i->info;
i->info=j->info;
j->info=swap;
}}}
printf("SLL is Sorted\n");
getch();
return;
}

//Insertion SLL
//Insert First Node in SLL
void insertfirstsll(){
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
if(start==NULL){
n->link=NULL;}
else{
n->link=start;}
start=n;
printf("First Node inserted\n");
getch();
return;
}
//Insert Last Node in SLL
void insertlastsll(){
NODE *temp=start;
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
n->link=NULL;
if(temp==NULL){
start=n;
}
else{
while(temp->link!=NULL){
temp=temp->link;}
temp->link=n;}
printf("Last Node inserted\n");
getch();
return;
}
//Insert a Node after given Location in SLL
void insertafterlocsll(NODE *loc){
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
if(start==NULL){
printf("SLL is not Exist\nChoose Option Insert First Node\n");
getch();
return;
}
else{
n->link=loc->link;
loc->link=n;
}
printf("Node inserted after given location\n");
getch();
return;
}
//Insert a Node before given Location in SLL
void insertbeforelocsll(NODE *loc){
NODE *temp=start;
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
if(loc==start){
printf("LOC is First Node\nChoose Option Insert First Node\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
n->link=loc;
while(temp->link!=loc){
temp=temp->link;}
temp->link=n;
printf("Node inserted before given Location\n");
getch();
return;
}

//Delete first Node in SLL
void deletefirstsll(){
clrscr();
n=start;
if(n==NULL){
printf("Underflow Condition\n");
getch();
return;
}
if(start->link==NULL){
start=NULL;}
else{
start=start->link;
}
printf("First Node Deleted\n");
getch();
free(n);
return;
}

//Delete Last Node in SLL
void deletelastsll(){
NODE *temp=start;
clrscr();
if(start==NULL){
printf("Underflow Condition\n");
getch();
return;
}
if(start->link==NULL){
n=start;
start=NULL;}
else{
while(temp->link->link!=NULL){
temp=temp->link;
}
n=temp->link;
temp->link=NULL;
}
printf("last Node Deleted\n");
getch();
free(n);
return;
}
//Delete a Node after given Location in SLL
void deleteafterlocsll(NODE *loc){
clrscr();
if(start==NULL){
printf("Underflow Condition\n");
getch();
return;
}
if(loc->link== NULL){
printf("Location is Last Node\ndeletion will not perform here\n");
getch();
return;
}
if(loc->link->link==NULL){
printf(" Choose option delete last node in SLL\n");
getch();
return;
}
else{
n=loc->link;
loc->link=loc->link->link;
}
printf("Node after given location is deleted\n");
getch();
free(n);
return;
}
//Delete a Node before given Location in SLL
void deletebeforelocsll(NODE *loc){
NODE *temp=start;
clrscr();
if(start==NULL){
printf("Underflow Condition\n");
getch();
return;
}
if(loc==start){
printf("Location is first Node\n Deletion will not perform here\n");
getch();
return;
}
if(start->link==loc){
printf("Location is Second Node\nChoose Option delete first Node in SLL\n");
getch();
return;
}
else{
while(temp->link->link!=loc){ temp=temp->link;}
n=temp->link;
temp->link=loc;
}
printf("Node before given location is deleted\n");
getch();
free(n);
return;
}
void main(){
int ch=0,item=0;
NODE *loc=NULL;
while(1){
clrscr();
printf("Single Linked List Operations:-\n1.Creation\n2.Traversing\n3.Searching\n4.Sorting\n5.Reverse\n6.Insertion\n7.Deletion\n8.Exit\nEnter Your Choice = ");
scanf("%d",&ch);
switch(ch){
case 1: createsll();
break;
case 2: traversesll();
break;
case 3:
printf("Enter an item to search\n");
scanf("%d",&item);
searchsll(item);
break;
case 4:
sortsll(start);
break;
case 5:
reversesll();
break;
case 6: printf("Insertion:-\n1.insert first node\n2.insert last node\n3.insert after given location\n4.insert before given location\nenter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1: insertfirstsll();
break;
case 2:
insertlastsll();
break;
case 3:
printf("Enter Location to insert Node\n");
scanf("%u",&loc);
insertafterlocsll(loc);
break;
case 4:
printf("Enter Location to insert Node\n");
scanf("%u",&loc);
insertbeforelocsll(loc);
break;
}
break;
case 7:
printf("Deletion:-\n1.Delete first node\n2.Delete last node\n3.Delete after given location\n4.Delete before given location\nenter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1: deletefirstsll();
break;
case 2:
deletelastsll();
break;
case 3:
printf("Enter Location to insert Node\n");
scanf("%u",&loc);
deleteafterlocsll(loc);
break;
case 4:
printf("Enter Location to insert Node\n");
scanf("%u",&loc);
deletebeforelocsll(loc);
break;
}
break;
case 8:
exit();
default:
printf("Wrong Choice\nTry Again!!\n");
getch();
}
}
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...