QUEUE USING LINKED LIST

For Algorithm and Program related to queue using integer array follow this link :
https://codstudio.wordpress.com/2014/02/23/queue-using-integer-array/

Program using linked list :

/***********************************
		AUTHOR- TANMAY BARANWAL
		    C#ODE STUDIO
***********************************/
#include"iostream"
#include"conio.h"
using namespace std;
class linkedq
{
	struct queue
	{
		int info;
		queue *link;
	}*front,*rear;
	public:
		linkedq()
		{
			front=NULL;
			rear=NULL;
		}
		int push(int);
		int pop();
		int disp();
};
int linkedq::push(int x)
{
	queue *ptr;
	ptr=new queue;
	ptr->info=x;
	ptr->link=NULL;
	if(front==NULL)
		front=ptr;
	else
		rear->link=ptr;
	rear=ptr;
	disp();
}
int linkedq::pop()
{
	queue *ptr;
	if(front==NULL)
		cout<<"\nUNDERFLOW ";
	else
	{
		ptr=front;
		front=front->link;
	}
	cout<<"\nELEMENT DELETED IS : "<<ptr->info;
	disp();
}
int linkedq::disp()
{
	queue *ptr;
	if(front==NULL)
	{
		cout<<"\nUNDERFLOW ";
	}
	else
	{
		ptr=front;
		cout<<"\nQUEUE => ";
		while(ptr!=NULL)
		{
			cout<<ptr->info<<"=> ";
			ptr=ptr->link;
		}
		cout<<" NULL"<<endl;
	}
}
main()
{
	linkedq q;
	int num,choice;
	do
	{
		cout<<"\n1:PUSH"
			<<"\t2:POP"
			<<"\t3:DISPLAY"
			<<"\t4:EXIT"<<endl;
		cout<<"\nMAKE CHOICE : ";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nINSERT VALUE : ";
					cin>>num;
					q.push(num);
					break;
			case 2: q.pop();
					break;
			case 3: q.disp();
					break;
			case 4: cout<<"\nEXITING PROGRAM "<<endl;
					break;
			default: cout<<"\nENTER CORRECT CHOICE";
		}
		cout<<"________________________________________"<<endl;
	}while(choice!=4);
	cout<<"\n\n\tPROGRAMMING @ C#ODE STUDIO";
	getch();
}

OUTPUT – :

queue_ll

STACK LINKED LIST

A stack is a data holding structure represented whether as linked list or array, for stack using array click here.

A stack follows LAST IN FIRST OUT for which we have to follow insertion at end and deletion in end .
Same in queue : we have to inbuilt two functions INSERTION AT END and DELETION AT BEGINING as it follows FIRST IN FIRST OUT : NEXT PROGRAM WILL BE ON QUEUES.

Program:

#include"conio.h"
#include"iostream"
/*STACK USING LINKED LIST BY C#ODE STUDIO*/
using namespace std;
class list
{
	struct node
	{
		int info;
		node *link;
	}*p,*start; /*STRUCTURE CONTAINING INFO AND LINK BY C#ODE STUDIO*/
	int size,m;
	public:
		list(int b)
		{
			p=NULL;
			size=b; /*SIZE OF STACK IS INITIALIZED IN CONSTRUCTOR*/
			m=0;
		}
		int push(int x);
		int pop();
		int disp();
};
/*PUSH FUNCTION BY C#ODE STUDIO*/
int list::push(int x)
{
	if(m<size)
	{
		if(p==NULL)
		{
			node *ptr;
			ptr=p;
			p=new node;
			p->info=x;
			p->link=ptr;
			cout<<"\nITEM INSERTED AT BEGINING"<<endl;
		}
		else
		{
			node *ptr,*t;
			ptr=p;
			while(ptr->link!=NULL)
				ptr=ptr->link;
			t=new node;
			t->info=x;
			t->link=ptr->link;
			ptr->link=t;
			cout<<"\nITEM INSERTED AT LIST : "<<x<<endl;
		}
		m+=1;
	}
	else
	{
		cout<<"\nOVERFLOW OCCURS : ";
	}
	disp();	
}
/*POP FUNCTION BY C#ODE STUDIO*/
int list::pop()
{
	node *ptr,*temp;
	int inf;
	ptr=p;
	if(ptr==NULL)
		cout<<"\nUNDERFLOW, NO NODES ARE AVAILABLE"<<endl;
	else
	{
		while(ptr->link!=NULL)
		{
			temp=ptr;
			ptr=ptr->link;
		}
		inf=temp->info;
		temp->link=NULL;
		cout<<"\nELEMENT POPPED : "<<inf<<endl;
	}
	disp();
}
/*DISPLAY FUNCTION BY C#ODE STUDIO*/
int list::disp()
{
	node *ptr;
	ptr=p;
	cout<<endl<<"STACK => Start => ";
	while(ptr!=NULL)
	{
		cout<<ptr->info<<" => ";
		ptr=ptr->link;
	}
	cout<<" NULL"<<endl<<endl;
}
int main()
{
	int siz;
	cout<<"\nENTER SIZE OF STACK : ";
	cin>>siz;
	list l(siz); /*CONSTRUCTOR OF LIST L BY C#ODE STUDIO*/
	int i=1,num,choice;
	while(i!=0)
	{
		cout<<"\n1:PUSH A ELEMENT"
			<<"\n2:POP A ELEMENT"
			<<"\n3:DISP"<<endl;
		cout<<"\nENTER CHOICE : ";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nENTER NUMBER : ";
					cin>>num;
					l.push(num);
					break;
			case 2: l.pop();
					break;
			case 3: l.disp();
					break;
			default: cout<<"\nWRONG CHOICE";
		}
		cout<<"\n1:CONTINUE OR 0:EXIT :: MAKE CHOICE : ";
		cin>>i;
	}
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
}

Output :

stack_ll

Continue reading

TWO WAY LINKED LIST

A Two Way linked list is a type of link list which contains two part as first link part pointing towards next node while second link part is pointing toward previous node. As shown in picture –

tw5

PROGRAM TO INSERT AND DELETE AND TRAVERSING THE TWO WAY LL –  If you think it is a bit larger then go function wise. It will help you implementing program for 1 operation.

#include"iostream.h"
#include"conio.h"
using namespace std;
class list
{
	struct node
	{
		int info;
		node *flink;
		node *blink;
	}*p;
	public:
	list()
	{
		p=NULL;
	}
	int ins_beg(int x);
	int ins_end(int x);
	int del_beg();
	int del_end();
	int del_giv(int x);
	int disp();
};
/*INSERTION AT BEG FUNCTION BY C#ODE STUDIO*/
int list::ins_beg(int x)
{
	node *ptr;
	ptr=p;
	p=new node;
	p->info=x;
	p->blink=NULL;
	p->flink=ptr;
	if(ptr==NULL)
		cout<<"\nVALUE INSERTED";
	else
	{
		ptr->blink=p;
		cout<<"\nVALUE INSERTED";
	}
}
/*INSERTION AT END FUNCTION BY C#ODE STUDIO*/
int list::ins_end(int x)
{
	node *temp,*ptr;
	ptr=p;
	if(ptr==NULL)
		ins_beg(x);
	else
	{
		temp=new node;
		temp->flink=NULL;
		temp->info=x;
		while(ptr->flink!=NULL)
		{
			ptr=ptr->flink;
		}
		temp-> blink=ptr;
		ptr->flink=temp;
		cout<<"\nVALUE INSERTED";
	}
}
/*DELETION AT BEG FUNCTION BY C#ODE STUDIO*/
int list::del_beg()
{
	node *ptr,*temp;
	ptr=p;
	if(ptr==NULL)
		cout<<"\nUNDERFLOW, EXITING...";
	else
	{
		if(ptr->flink==NULL && ptr->blink==NULL)
			ptr=NULL;
		else
		{
			temp=ptr->flink;
			p=ptr->flink;
			temp->blink=NULL;
			ptr->flink=NULL;
		}
	}
	cout<<"\nVALUE DELETED";
} 
/*DELETION AT END FUNCTION BY C#ODE STUDIO*/
int list::del_end()
{
	node *ptr,*temp;
	ptr=p;
	if(ptr==NULL)
		cout<<"\nUNDERFLOW ";
	else
		if(ptr->flink==NULL && ptr->blink==NULL)
			ptr=NULL;
		else
		{
			while(ptr->flink!=NULL)
			{
				temp=ptr;
				ptr=ptr->flink;
			}
			temp->flink=NULL;
			ptr->blink=NULL;
		}
	cout<<"\nVALUE DELETED";
}
/*DELETION OF VALUE FUNCTION BY C#ODE STUDIO*/
int list::del_giv(int x)
{
	node *ptr,*temp1,*temp2;
	ptr=p;
	temp1=NULL;
	temp2=NULL;
	if(ptr==NULL)
		cout<<"\nUNDERFLOW OCCURS : NO DATA";
	else
	{
		while(ptr->flink!=NULL && ptr->info!=x)
		{
			temp1=ptr;
			ptr=ptr->flink;
			temp2=ptr->flink;
		}
		if(ptr->flink==NULL && ptr->blink==NULL)
			ptr=NULL;
		else
			if(ptr->flink==NULL && ptr->blink!=NULL)
			{
				temp1->flink=NULL;
				ptr->blink=NULL;
			}
			else
			{
				temp1->flink=temp2;
				temp2->blink=temp1;
				ptr->flink=NULL;
				ptr->blink=NULL;
			}
		cout<<"\nVALUE DELETED = "<<x;	
	}
}
/*DISPLAY FUNCTION BY C#ODE STUDIO*/
int list::disp()
{
	node *ptr;
	ptr=p;
	cout<<"START => ";
	while(ptr!=NULL)
	{
		cout<<ptr->info<<" => ";
		ptr=ptr->flink;
	}
	cout<<" NULL ";
}
/*MAIN FUNCTION BY C#ODE STUDIO*/
int main()
{
	list l;
	int num,choice,i=1;
	cout<<"\nPROGRAM OF TWO WAY LINKED LIST : READ AND MAKE CHOICE"<<endl<<endl;
	while(i!=0)
	{
		cout<<"\n\t1:INSERTION AT BEG"
			<<"\n\t2:INSERTION AT END"
			<<"\n\t3:DELETION FROM BEG"
			<<"\n\t4:DELETION FROM END"
			<<"\n\t5:DELETE AN ELEMENT"
			<<"\n\t6:DISPLAY"<<endl;
		cout<<"\nENTER YOUR CHOICE : ";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nENTER NUMBER : ";
					cin>>num;
					l.ins_beg(num);
					break;
			case 2: cout<<"\nENTER NUMBER : ";
					cin>>num;
					l.ins_end(num);
					break;
			case 3: l.del_beg();
					break;
			case 4: l.del_end();
					break;
			case 5: cout<<"\nENTER NUMBER : ";
					cin>>num;
					l.del_giv(num);
					break;
			case 6: l.disp();
					break;
			default: cout<<"\nWRONG CHOICE : MAKE CHOICE AGAIN";
		}
		cout<<"\n\n\tPRESS 1:CONTINUE AND 0:EXIT ::: ";
		cin>>i;
	}
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
} 

OUTPUT –
Will be long. So click on image to see output.

Continue reading

CIRCULAR LINKED LIST

A circular linked list is the link list in which the last node points to header node,
Manipulation of all the nodes are same as we do in the previous linked list.
Go through image to understand.

circ

#include"conio.h"
#include"iostream"
using namespace std;
class list
{
	struct node
	{
		int info;
		node *link;
	}*p;
	public:
		list()
		{
			p=new node;
			p->info=0;
			p->link=p;
		}
		int insend(int x);
		int insbeg(int x);
		int headins(int x);
		int sumofele();
		int maxofele();
		int disp();
};
int list::maxofele()
{
	node *ptr;
	ptr=p->link;
	int max_ele=ptr->info;
	while(ptr!=p)
	{
		if(ptr->info > max_ele)
			max_ele=ptr->info;
		ptr=ptr->link;
	}
	p->info=max_ele;
	disp();
}
int list::sumofele()
{
	node *ptr;
	int sum=0;
	ptr=p->link;
	while(ptr!=p)
	{
		sum=sum+ptr->info;
		ptr=ptr->link;
	}
	p->info=sum;
	disp();
}
int list::insbeg(int x)
{
	node *ptr,*temp;
	ptr=p->link;
	temp=new node;
	temp->info=x;
	temp->link=ptr;
	p->link=temp;
	cout<<"\nITEM INSERTED AT BEGINING"<<endl;
	disp();
}
int list::insend(int x)
{
	if(p->link==p)
	{
		insbeg(x);
	}
	else
	{
		node *ptr,*t;
		ptr=p->link;
		while(ptr->link!=p)
			ptr=ptr->link;
		t=new node;
		t->info=x;
		t->link=ptr->link;
		ptr->link=t;
		cout<<"\nITEM INSERTED AT LIST"<<endl;
		disp();
	}	
}
int list::disp()
{
	node *ptr;
	ptr=p;
	cout<<"Values => Start => Header = ";
	do
	{
		cout<<ptr->info<<" => ";
		ptr=ptr->link;
	}while(ptr!=p);
	cout<<"NULL"<<endl<<endl;
}
int main()
{
	list l;
	int n=1,num,choice;
	while(n!=0)
	{
		cout<<"\n1:INSERT AT END"
			<<"\n2:INSERT AT BEG"
			<<"\n3:SUM OF ELEMENTS"
			<<"\n4:MAX OF ELEMENTS"
			<<"\n5:DISPLAY"<<endl;
		cout<<"\nINSERT CHOICE :";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nINSERT VALUE : ";
					cin>>num;
					l.insend(num);
					break;
			case 2: cout<<"\nINSERT VALUE : ";
					cin>>num;
					l.insbeg(num);
					break;
			case 3: l.sumofele();
					break;
			case 4: l.maxofele();
					break;
			case 5: l.disp();
					break;
			default: cout<<"\nENTER CORRECT CHOICE ";
		}
		cout<<"\n1:CONTINUE, 0:EXIT :: ";
		cin>>n;
	}
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
	return 0;
}

Output –

circll

HEADER LINKED LIST

HEAD

Header linked list are the linked list with one node attached with start pointer. We doesnt have to consider that node in treversal. The next nodes of header node will contain elements and the header node will contain any special information related to all the element nodes.

Algorithm –

1: SET PTR=LINK [START]
2: REPEAT 3,4 WHILE PTR != NULL
3:   //HERE WE CAN APPLY OPERATIONS//
4:  PTR= LINK [PTR]
5: EXIT

Program –
Below, program contains insertion at end and beginning of header link list and display function along with two function sumofelement & maxofelement to store there corresponding data to header node.

#include"conio.h"
#include"iostream"
using namespace std;
class list
{
	struct node
	{
		int info;
		node *link;
	}*p,*start;
	public:
		list()
		{
			p=new node;
			p->info=0;
			p->link=NULL;
		}
		int insend(int x);
		int insbeg(int x);
		int headins(int x);
		int sumofele();
		int maxofele();
		int disp();
};
int list::maxofele()
{
	node *ptr;
	ptr=p->link;
	int max_ele=ptr->info;
	while(ptr!=NULL)
	{
		if(ptr->info > max_ele)
			max_ele=ptr->info;
		ptr=ptr->link;
	}
	p->info=max_ele;
	disp();
}
int list::sumofele()
{
	node *ptr;
	int sum=0;
	ptr=p->link;
	while(ptr!=NULL)
	{
		sum=sum+ptr->info;
		ptr=ptr->link;
	}
	p->info=sum;
	disp();
}
int list::insbeg(int x)
{
	node *ptr,*temp;
	ptr=p->link;
	temp=new node;
	temp->info=x;
	temp->link=ptr;
	p->link=temp;
	cout<<"\nITEM INSERTED AT BEGINING"<<endl;
	disp();
}
int list::insend(int x)
{
	if(p==NULL)
	{
		insbeg(x);
	}
	else
	{
		node *ptr,*t;
		ptr=p->link;
		while(ptr->link!=NULL)
			ptr=ptr->link;
		t=new node;
		t->info=x;
		t->link=ptr->link;
		ptr->link=t;
		cout<<"\nITEM INSERTED AT LIST"<<endl;
		disp();
	}	
}
int list::disp()
{
	node *ptr;
	ptr=p;
	cout<<"Values => Start => Header = ";
	while(ptr!=NULL)
	{
		cout<<ptr->info<<" => ";
		ptr=ptr->link;
	}
	cout<<"NULL"<<endl<<endl;
}
int main()
{
	list l;
	int n=1,num,choice;
	while(n!=0)
	{
		cout<<"\n1:INSERT AT END"
			<<"\n2:INSERT AT BEG"
			<<"\n3:SUM OF ELEMENTS"
			<<"\n4:MAX OF ELEMENTS"
			<<"\n5:DISPLAY"<<endl;
		cout<<"\nINSERT CHOICE :";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nINSERT VALUE : ";
					cin>>num;
					l.insend(num);
					break;
			case 2: cout<<"\nINSERT VALUE : ";
					cin>>num;
					l.insbeg(num);
					break;
			case 3: l.sumofele();
					break;
			case 4: l.maxofele();
					break;
			case 5: l.disp();
					break;
			default: cout<<"\nENTER CORRECT CHOICE ";
		}
		cout<<"\n1:CONTINUE, 0:EXIT :: ";
		cin>>n;
	}
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
	return 0;
}

Output –

h1 h2

DELETION : LINKED LIST

This Program contains the function related to deletion of a node from beginning, end and given location.
Algo will be followed using this program….. Program of sorted linked list.

#include"conio.h" #include"iostream" using namespace std; class list { struct node { int info; node *link; }*p; public: list() { p=NULL; } int inssll(int x); int delbeg(); int delend(); int delloc(int x); int disp(); };
/*FUNCTION TO INSERT VALUE*/

int list::inssll(int x) //function to insert values in sorted linked list { node *ptr,*loc,*temp; //we have taken two pointers to track the previous location from position ptr=p; loc=NULL; temp=NULL; while(ptr!=NULL && x>ptr->info) //loop will continue when we get item > info [ptr] and ptr!=null { temp=ptr; ptr=ptr->link; } loc=temp; temp=new node; //deriving new node temp->info=x; if(loc==NULL) { temp->link=p; p=temp; } else { temp->link=loc->link; loc->link=temp; } disp(); } /*FUNCTION TO DELETE VALUE FROM BEGINNING*/

int list::delbeg() { node *ptr; ptr=p; if(ptr==NULL) cout<<"\nUNDERFLOW, NO NODES ARE AVAILABLE"<<endl; else { p=ptr->link; ptr->link=NULL; } disp(); } /*FUNCTION TO DELETE VALUE FROM END*/

int list::delend() { node *ptr,*temp; ptr=p; if(ptr==NULL) cout<<"\nUNDERFLOW, NO NODES ARE AVAILABLE"<<endl; else { while(ptr->link!=NULL) { temp=ptr; ptr=ptr->link; } temp->link=NULL; } disp(); } /*FUNCTION TO DELETE VALUE FROM LOCATION*/

int list::delloc(int x) { node *ptr,*temp,*loc; ptr=p; loc=NULL; temp=NULL; while(ptr!=NULL && x>ptr->info) { temp=ptr; ptr=ptr->link; } loc=temp; if(ptr==NULL) cout<<"\nUNDERFLOW OR ITEM NOT FOUND"<<endl; else { if(loc==NULL) delbeg(); else { if(loc!=NULL && ptr==NULL) delend(); else { loc->link=ptr->link; ptr->link=NULL; } } } disp(); } /*FUNCTION TO DISPLAY NODES*/

int list::disp() { node *ptr; ptr=p; cout<<"Values => Start => "; while(ptr!=NULL) { cout<<ptr->info<<" => "; ptr=ptr->link; } cout<<"NULL"<<endl<<endl; } /*MAIN FUNCTION*/

int main() { list l; int n=1,num,choice; while(n!=0) { cout<<"\n1:INSERT" <<"\n2:DELETE BEG" <<"\n3:DELETE END" <<"\n4:DELETE LOC" <<"\n5:DISPLAY"<<endl; cout<<"\nINSERT CHOICE :"; cin>>choice; switch(choice) { case 1: cout<<"\nINSERT VALUE : "; cin>>num; l.inssll(num); break; case 2: cout<<"\nDELETING FROM BEGINNING: "<<endl; l.delbeg(); break; case 3: cout<<"\nDELETING FROM END: "<<endl; l.delend(); break; case 4: cout<<"\nINSERT VALUE TO DELETE: "; cin>>num; l.delloc(num); break; case 5: l.disp(); break; default: cout<<"\nENTER CORRECT CHOICE "; } cout<<"\n1:CONTINUE, 0:EXIT :: "; cin>>n; } cout<<"\nPROGRAMMING @ C#ODE STUDIO"; getch(); return 0; }

FOR OUTPUT CLICK ON BELOW IMAGE.—–

Deletion

INSERTION: SORTED LL

INSERTION IN SORTED LINKED LIST : ALGORITHM FOLLOWS

LINK_SLL

INST-SLL(START,INFO,LINK,LOC,ITEMS)
1: SET PTR=START & LOC:=NULL & TEMP:=NULL
2: WHILE PTR!=NULL && ITEM > INFO [PTR] REPEAT STEP 3.
3:TEMP = PTR & PTR = LINK[PTR]
4: SET LOC=TEMP
5: IF AVAIL==NULL
            OVERFLOW & EXIT
6: SET NEW = AVAIL & AVAIL=LINK[AVAIL]
7: SET INFO [NEW] = ITEM
8: IF LOC==NULL
           SET LINK [NEW] : =START & START= NEW
    ELSE
           LINK [NEW] = LINK[LOC] & LINK [LOC] = NEW
9: EXIT;

PROGRAM :

#include"conio.h"
#include"iostream"
using namespace std;
class list
{
	struct node
	{
		int info;
		node *link;
	}*p;
	public:
		list()
		{
			p=NULL;
		}
		int inssll(int x);
		int disp();
};
int list::inssll(int x) //function to insert values in sorted linked list
{
	node *ptr,*loc,*temp; //we have taken two pointers to track the previous location from position
	ptr=p;
	loc=NULL;
	temp=NULL;
	while(ptr!=NULL && x>ptr->info) //loop will continue when we get item > info [ptr] and ptr!=null
	{
		temp=ptr;
		ptr=ptr->link;
	}
	loc=temp;
	temp=new node; //deriing new node
	temp->info=x;
	if(loc==NULL)
	{
		temp->link=p;
		p=temp;
	}
	else
	{
		temp->link=loc->link;
		loc->link=temp;
	}
	disp();
}
int list::disp()
{
	node *ptr;
	ptr=p;
	cout<<"Values => Start => ";
	while(ptr!=NULL)
	{
		cout<<ptr->info<<" => ";
		ptr=ptr->link;
	}
	cout<<"NULL"<<endl<<endl;
}
int main()
{
	list l;
	int n=1,num,choice;
	while(n!=0)
	{
		cout<<"\n1:INSERT"
			<<"\n2:DISPLAY"<<endl;
		cout<<"\nINSERT CHOICE :";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nINSERT VALUE : ";
					cin>>num;
					l.inssll(num);
					break;
			case 2: l.disp();
					break;
			default: cout<<"\nENTER CORRECT CHOICE ";
		}
		cout<<"\n1:CONTINUE, 0:EXIT :: ";
		cin>>n;
	}
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
	return 0;
}

OUTPUT –

linked_list_sll

INSERTION IN LINKED LIST

TRAVERSE A LINKED LIST : ALGORITHMS

TRAVERSE-LL(START, INFO, LINK)
1: SET PTR:=START
2: REPEAT STEP 3 & 4 WHILE PTR!=NULL
3: //APPLY PROCESS TO INFO [PTR]
4: SET PTR:=LINK [PTR]
5: EXIT

INSERTION IN LINKED LIST :

INSERTION-BEG:LL(START,AVAIL,INFO,LINK,ITEM) 1: IF AVAIL = NULL PRINT :”OVERFLOW” & EXIT 2: SET NEW:=AVAIL 3: SET INFO [NEW] = ITEM 4: SET LINK [NEW]=START 5: SET START:=NEW 6:EXIT
---------------------------------------------
INSERTION-END:LL
1: IF AVAIL = NULL PRINT :”OVERFLOW” & EXIT
2: SET NEW:=AVAIL
3: SET INFO [NEW] = ITEM
4:WHILE(LINK [PTR] != NULL) REPEAT 5
5: PTR = LINK [PTR]
6: LINK [PTR]=NEW
7: LINK [NEW]=NEW
8: EXIT

C++ PROGRAMS : ( WE RECOMMEND C-FREE ( BEST IDE FOR C/C++ ) )

#include"conio.h"
#include"iostream"
using namespace std;
class list
{
	struct node
	{
		int info;
		node *link;
	}*p; // A NODE IS BEING INITIALIZE HERE
	public:
		list()
		{
			p=NULL;
		}
		int insend(int x);
		int insbeg(int x);
		int disp();
};
int list::insbeg(int x) // FUNCTION TO INSERT NODE AT BEGINNNING
{
	node *ptr;
	ptr=p;
	p=new node;
	p->info=x;
	p->link=ptr;
	cout<<"\nITEM INSERTED AT BEGINING"<<endl;
	disp();
}
int list::insend(int x) // A NODE IS BEING INSERTED AT END HERE 
{
	if(p==NULL)
	{
		insbeg(x);
	}
	else
	{
		node *ptr,*t;
		ptr=p;
		while(ptr->link!=NULL)
			ptr=ptr->link;
		t=new node;
		t->info=x;
		t->link=ptr->link;
		ptr->link=t;
		cout<<"\nITEM INSERTED AT LIST"<<endl;
		disp();
	}	
}
int list::disp()
{
	node *ptr;
	ptr=p;
	cout<<"Values => Start => ";
	while(ptr!=NULL)
	{
		cout<<ptr->info<<" => ";
		ptr=ptr->link;
	}
	cout<<"NULL"<<endl<<endl;
}
int main()
{
	list l;
	int n=1,num,choice;
	while(n!=0)
	{
		cout<<"\n1:INSERT AT END"
			<<"\n2:INSERT AT BEG"
			<<"\n3:DISPLAY"<<endl;
		cout<<"\nINSERT CHOICE :";
		cin>>choice;
		switch(choice)
		{
			case 1: cout<<"\nINSERT VALUE : ";
					cin>>num;
					l.insend(num);
					break;
			case 2: cout<<"\nINSERT VALUE : ";
					cin>>num;
					l.insbeg(num);
					break;
			case 3: l.disp();
					break;
			default: cout<<"\nENTER CORRECT CHOICE ";
		}
		cout<<"\n1:CONTINUE, 0:EXIT :: ";
		cin>>n;
	}
	cout<<"\nPROGRAMMING @ C#ODE STUDIO";
	getch();
	return 0;
}

OUTPUT –

linked list_insert

LINKED LIST : INFORMATION

 

LINK

Linked List : Linked List is used to define a array in a memory where defragmentation occurs. Defragment leads to the memory location having non-continous data, so that we cant initialize a large memory location.

File system fragmentation.svg

Linked List is the solution to overcome memory defragment problem in which, we have nodes which contains an info part and a link part.

LINK2 Each Node is attached to another node whose address is stored in link part. Last link should have Θ (NULL VALUE) in singly link list.

Go To Next Post For Algorithms.