Monday, 21 September 2015

Link list in c/c++ source code, c++, source code,

Link list in c/c++ source code, c++,
Link list in c/c++ source code,  c++,

More: > Dynamic Array 










#include<iostream>

//------define struct new type and address----------//
//--------of next node take this struct-------------//

struct node{
int data;
node *next;
node()
{
data=0;
next=NULL;
}
};

//-------------------------------------------------//

node *head=NULL, *tail=NULL;
void add_end();
void add_start();
void delet_all();
void show();
void input(int d);

//------------------------------------------------//

//-----------main function------------------------//

void main()
{
int num,value;
char option;
cout<<"enter number of elements: ";
cin>>num;
//0-----------------------------------------------//

for(int i=0; i<num; i++)
{
cout<<"enter value to element: ";
cin>>value;
input(value);
}
//------------------------------------------------//

do{
cout<<endl;
cout<<"     to delete all press ....... = d "<<endl;
cout<<"     to add at start press ..... = a "<<endl;
cout<<"     to display all nodes ...... = p "<<endl;
cout<<"     to add at end press ....... = e "<<endl;
cout<<"     enter desired option....... =   ";
cin>>option;

//-----------switch statement---------------------//

switch (option)
{
case 'A':
case 'a':
add_start();
break;

case 'd':
case 'D':
delet_all();
break;

case 'P':
case 'p':
show();
break;

case 'E':
case 'e':
add_end();
break;
}
}
//-----------------------------------------------//

while(option!='x' && option!='X');

//-----function for delete all items of link list--//

delet_all();
}

void input(int d)
{
node *ptr;
if(head==NULL)
{
tail=new node;
tail->data=d;
tail->next=NULL;
head=tail;
}
else
{
// tail=tail->next;
ptr=tail;
tail=new node;
tail->data=d;
tail->next=NULL;
ptr->next=tail;

}
}
//-------------------------------------------------//

//------function to show element of linklist-------//

void show()
{
node *ptr;
ptr=head;
if(ptr==NULL)
cout<< "empty ... !!! ";
else
{
while(ptr!=NULL)
{
cout<<" "<<ptr->data;
ptr=ptr->next;
}
}
cout<<endl;
}

//------------------------------------------------//

//-----function to delete all element of----------//
//---------------link list------------------------//

void delet_all()
{
node *ptr,*prev;
ptr=head;
while(ptr->next!=NULL)
{
prev=ptr;
ptr=ptr->next;
delete prev;
}
head=NULL;
}
//-----------------------------------------------//

//--------function for adding element to---------//
//-------------link list at start----------------//

void add_start()
{
node *ptr;
ptr=new node;
int d;
cout<<"enter data to new node: ";
cin>>d;
ptr->data=d;
ptr->next=head;
head=ptr;
}
//----------------------------------------------//

//--------function for adding element to---------//
//-------------link list at end----------------//

void add_end()
{
node *ptr;
ptr=new node;
cout<<"enter data to new node: ";
cin>>ptr->data;
ptr->next=NULL;
tail->next=ptr;
}
//---------------------------------------------//




Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com 


useful tutorial keep it up...
EmoticonEmoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)

LinkWithin