C++ example code of linked list


C++ Structure example: building a singly linked list

Step 7: Put the C++ code together

This is the complete C++ code of a singly linked list data structure:


#include <iostream.h>
#include <conio.h>

using namespace std;


struct ListElem{
int data; // the element data
ListElem *next; // next link
};

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


void Insert (int,int);
void Delete(int);//delete item from the list
void printall();//print out all items on the screen
int countitem();//return the number of items in the list

ListElem *pfirst;
ListElem *plast;



//insert item
void Insert (int val,int pos)
{

int t;
ListElem *item;//new element to be inserted
item=(ListElem *)malloc(sizeof(ListElem)); //allocate space
if(!item) {cout<<"Memory problem..."; exit(100);}

item->data=val;

//insert a new item to the empty list

if(pfirst==NULL && plast==NULL){
//The first and last item point to the new item when they are null--empty list.
item->next=NULL;
pfirst=item;
plast=item;

cout<<"Inserted:"<<item->data<<endl;

}

//insert a new item at the beginning of the list
else if(pos==1)
{

item->next=pfirst;
pfirst=item;

}
//insert a new item between items
else if(pos>1 && pos<=countitem()){

ListElem *ta;
ta=pfirst;
for(t=1;t<pos-1;t=t+1){ta=ta->next;} //move to the insertion point
item->next=ta->next;
ta->next=item;

}

//insert a new item at the end of the list
else if(pos==countitem()+1){

item->next=NULL; //The next link of the item is null.
plast->next=item;
plast=item;
cout<<"Inserted:"<<item->data<<endl;

}
//show message if position is not valid.
else cout<<"Invalid position! Position must be between 1 and "<<countitem()+2<<endl;


}

//Print out all items on the screen

//show all items of the list
void printall()
{

ListElem *i;
i=pfirst;
if(countitem()>0){
while(i!=NULL){
cout<<i->data<<endl;
i=i->next;
}
}
else cout<<"This is no item.\n";


}

//count the number of items in the list
int countitem()
{
ListElem *i;
int t=0;
i=pfirst;
while(i!=NULL){
t=t+1;
i=i->next;

}

return t;

}

//delete item
void Delete(int pos){
int i;
if(countitem()>0){ //make sure the list is not empty.
ListElem *temp,*del;

if(pos==1){//delete the first item
if(countitem()==1){ //The list contains only one item
pfirst=NULL;
plast=NULL;

}
else{ //The list contains more than one item
temp=pfirst;
pfirst=pfirst->next;
temp=NULL;
}

}

else if(pos>1 && pos<=countitem()){//delete middle item
temp=pfirst;
for(i=1;i<pos-1;i=i+1){temp=temp->next;} //move to the item staying before the target item to be deleted
del=temp->next; //target item to be deleted
temp->next=del->next;
if(del->next==NULL)plast=temp; //delete last item
del=NULL;

}


else cout<<"Invalid position!\n";

}

else cout<<"No item found\n";

}

void showmenu(){

cout<<"=================================\n";
cout<<"Linked List Operations Menu\n";
cout<<"=================================\n";
cout<<"1.Add a new item\n";
cout<<"2.Delete an item\n";
cout<<"3.Show number of items\n";
cout<<"4.Show all items\n";
cout<<"5.Exit\n";


}

void select(){
int val, pos, ch;
char yes='y';
while(yes=='y'){
cout<<"Enter your choice:";cin>>ch;
switch(ch){
case 1:
cout<<"Value:";cin>>val;
cout<<"Position:";cin>>pos;
Insert(val,pos);
break;
case 2:
cout<<"Position:";cin>>pos;
Delete(pos);
break;
case 3:
cout<<"Number of items:";cout<<countitem();
break;
case 4:
cout<<"All items:\n";
printall();
break;
case 5: exit(0);

default: cout<<"Invalid choice\nt";

}
cout<<"Continue?y/n:";cin>>yes;
}

}

int main(){

showmenu();
select();
getch();
return 0;
}




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.