linked list simple program

+2 Michael De Guzman · January 6, 2016
can u give me some simple example program  thanks in advance :)

Post a Reply

Replies

Oldest  Newest  Rating
0 phapha pha · January 13, 2016
Do you mean programs for early learning for beginner?

If it is, then you could make simple programs like :
Calculator,
Thermometer Conversion,
Calculate Volume of a shape, etc.
Finding Zodiak base from the input,
0 Purewoidow Gonchigsuren · January 13, 2016
i have recommended for you SinglyLinkedList

# include <iostream>
# include <conio.h>
using namespace std;
typedef int Data;
class SLNode{
Data element;
SLNode *next;
SLNode(Data evalue, SLNode *nvalue){
element=evalue; 
next=nvalue;
}
friend class SinglyLinkedList;
};
class SinglyLinkedList{
SLNode*head;
SLNode*tail;
     public:
          SinglyLinkedList();
          ~SinglyLinkedList();
        
          bool IsEmpty();
          void MakeEmpty();
          
      void InsertFirst(Data item);
      void InsertLast(Data item);
      void DeleteFirst();
      int Count();
      void DeletePosition(int pos);
      void Print();
      void insertLast(Data item);
      bool FindItem(Data key);
      void DeleteLast();
      void DeleteItem(Data key);
      //*void Print();
  
};
SinglyLinkedList::SinglyLinkedList(){
head=tail=NULL;
}
SinglyLinkedList::~SinglyLinkedList(){
MakeEmpty();
}
void SinglyLinkedList::MakeEmpty(){ // sanah oigoos bvren cholooloh
SLNode*curr=NULL;
while(head!=NULL){
curr=head;
head=head->next;
delete curr;
}
tail=NULL;
}
bool SinglyLinkedList::IsEmpty(){  //jagsaaltiig hoosloh
if(head==NULL && tail==NULL)
return true;
else return false;
}

void SinglyLinkedList::InsertFirst(Data item){
        SLNode*curr=new SLNode(item, head);
        if(IsEmpty()==true) tail=curr;
        head=curr;
     }
 void SinglyLinkedList::InsertLast(Data item){
       SLNode*curr=new SLNode(item,NULL);
       if(IsEmpty()==true) head=curr;
       else tail->next=curr;
       tail=curr;      
      }    
 void SinglyLinkedList::DeleteFirst(){
        if(IsEmpty()==false){
            SLNode*curr=head;                 
            head=curr->next;                
             delete curr;
              }
          else cout<<"SLList is empty";
      }

int SinglyLinkedList::Count(){
      if(IsEmpty()==false){
                           SLNode*curr=head;
                           int counter=0;
                           while(curr!=NULL){
                                             counter++;
                                             curr=curr->next;
                                             }
                                             return counter;
                           }
                           else return 0;
    }
 void SinglyLinkedList::DeletePosition(int pos){
       if(IsEmpty()==false){
                            int i=1;
                            SLNode*prevCurr=NULL;
                            SLNode*curr=head;
                            while(curr!=NULL&&i!=pos){
                                                      prevCurr=curr;
                                                      curr=curr->next;
                                                      i++;
                                                      }
                                if(curr==NULL) cout<<"Position not found";
                                if(i==pos){
                                           if(curr=head) head=curr->next;
                                           else prevCurr->next=curr->next;
                                           if(curr=tail)tail=prevCurr;
                                           delete curr;
                                           }
                            }
                            
      else cout<<"SLList empty";
      }   
 void SinglyLinkedList::Print(){
      if(IsEmpty()==false){
                           SLNode*curr=head;
                           cout<<"\n"<<"SLList";
                           while(curr!=NULL){
                                             cout<<curr->element<<"->";
                                             curr=curr->next;
                                             }
                           
                           }
        else cout<<"SLList empty"<<endl;
      }     
 
 //void SinglyLinkedList::InsertLast(Data item){
  //     SLNode*curr=new SLNode(item,NULL);
   //    if(IsEmpty()==true) head=curr;
     //  else tail->next=curr;
      // tail=curr;      
    //  }    
 bool SinglyLinkedList::FindItem(Data key){
     if(IsEmpty()==false){
      SLNode*curr=head;
      while(curr!=NULL){
if(curr->element==key) return true;
      else {
curr=curr->next;
return false;
}
}
 
 
}
    else return false;
    
 }

 void SinglyLinkedList::DeleteLast(){
  if(IsEmpty()==false){
  SLNode*prevCurr=NULL;
  SLNode*curr=head;
  while(curr!=tail){
  prevCurr=curr;
  curr=curr->next;
}
if(curr=tail){
prevCurr->next=curr->next;
tail=prevCurr;
}
delete curr;
}
else cout<<"SLList empty";
 }
 void SinglyLinkedList::DeleteItem(Data key){   //element ustgah
if(IsEmpty()==true){
SLNode*prevCurr=NULL;
SLNode*curr=head;
while(curr!=NULL&&curr->element!=key){
prevCurr=curr;
curr=curr->next;
}
if(curr==NULL) cout<<"Key element not found";
if(curr==tail) tail=prevCurr;
if(curr==head) head=prevCurr;
if(curr->element==key){
prevCurr->next=curr->next;
delete curr;
}
}
else cout<<"SinglyLinkedList empty";
}
 //void SinglyLinkedList::Print(){
 // if(IsEmpty()==false){
 // SLNode*curr=head;
 // cout<<"\n"<<"SLList";
 // while(curr!=NULL){
 // cout<<curr->element<<"->";
 // curr=curr->next;
// }
// }
// else cout<<"SLList hooson"<<endl;
 //}
 
 
   int main(int argc,char** argv)
{
SinglyLinkedList slist;
int n,k,l,m,b,c;
cout<<"1.InsertFirst(Data item)"<<"\n";
cout<<"2.InsertLast(Data item)"<<"\n";
cout<<"3.DeleteFirst(Data key)"<<"\n";
cout<<"4.Count()"<<"\n";
cout<<"5.DeletePosition(int pos)"<<"\n";
cout<<"6.Print()"<<"\n";
cout<<"7.InsertLast(Data item)"<<"\n";
cout<<"8.FindItem(Data key)"<<"\n";
cout<<"9.DeleteLast(Data key)"<<"\n";
cout<<"10.DeleteItem(Data key)"<<"\n";
// cout<<"11. Print()"<<"\n";
       cout<<"Tanii songoson utga n=";
       cin>>n;
while(n!=0){
switch(n){

case 1:
    cout<<"k=";
    cin>>k;
    slist.InsertFirst(k);
    slist.Print();
    break;
case 2:
cout<<"l=";
cin>>l;
slist.InsertLast(l);
slist.Print();
break;
case 3:

   slist.DeleteFirst();
slist.Print();
break;
case 4:
slist.Count();
slist.Print();
break;
case 5:
cout<<"m=";
cin>>b;
slist.DeletePosition(m);
slist.Print();
break;
case 6:
slist.Print();
            break;
case 7:
     cout<<"l=";
     cin>>l;
 slist.InsertLast(l);
     slist.Print();
 break;
case 8:
    cout<<"b=";
    cin>>b;
slist.FindItem(b);
    slist.Print();
    break;
case 9:
   slist.DeleteLast();
   slist.Print();
   break;
case 10:
  cout<<"c=";
  cin>>c;
  slist.DeleteItem(c);
  slist.Print();
  break;
default:
 cout<<"Insert another variable=";
 break;
}
cout<<"\n";
cout<<"Insert variable n=";
            cin>>n;

}

}
0 Tushar Mudgal · January 9, 2016
go to geeksforgeeks.org thats the site for cs geeks, there u will get lot of bunch of piece of code related to data structures and algorithms
  • 1

C++

107,024 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator