Anda di halaman 1dari 6

/* SIMPLE IMPLEMENTATION OF SINGLY LINKED LIST

 * HAS 3 FILES – a header file, an implementation file and a file containing main() function to create and use object for the
  class defined in the .h and .C file 
 */

#ifndef NUMBERLIST_H
#define NUMBERLIST_H

/* AUTHOR: baddie_hehehe
 * DATE: 05/31/2010
 * FILE: LinkedList.h
 * DESCRIPTION: a simple single linked list of numbers
 */

 #include<iostream>
 #include<stdlib.h>

 class NumberList {
 
   public:
     NumberList(); //constructor
    ~NumberList(); //destructor

             //operations done on the list
     void insertNumber(int);
     void deleteNumber(int);
             void displayList();
             void searchList(int);
  private:
    //copy constructor and assignment operator
    NumberList(const NumberList&);
    NumberList operator = (const NumberList&);

   struct Elem {
int number;
Elem* next;
           };
   Elem* head;
 };

#endif
/* AUTHOR: baddie_hehehe
 * DATE: 05/31/2010
 * FILE: LinkedList.C
 * DESCRIPTION: implementation of singly linked list
 */

#include <iostream>
#include <stdlib.h>
#include "LinkedList.h"
using namespace std;

//Default constructor

 NumberList :: NumberList()
 {  
     //makes an empty list
     head = NULL;
 }

//_____________________________________________________________________________

 //Destructor
 
 NumberList :: ~NumberList()
 {
Elem *curr, *p;
p = head;
        
      //delete each element in the list 
        while(p != NULL) {
curr = p;
p = p ­> next;
delete curr;
}
 
 }

//____________________________________________________________________________

 //insert numbers to the list

void  NumberList :: insertNumber(int num)
 {
Elem* p;
        Elem*ptr;
   
        //insertion to an empty list
//insert the elem and make it head
         if(head == NULL) {
p = new Elem;
p ­> number = num;
p ­> next = NULL;
head = p;
 }

//some elements are already there in the list
       // make the new element last in the list 
else {
p = head;

          while(p != NULL) {
//traverse to the end of the list
ptr = p;
p = p ­> next;
}
   //insert the new element ­ this is the last elt in the list
p = new Elem;
                p ­> number = num;
p ­> next = NULL;

  // link the new element to the list
ptr ­> next = p;
       }
 }

 //________________________________________________________________________

  void NumberList :: deleteNumber(int num)
  {
Elem* p = head;
Elem* ptr;
  
while(p != NULL && p­>number != num) {

ptr = p;
p = p ­> next;
}

if(p == NULL){
//while loop terminated because EOL reached
    cerr << "\n End of list reached! Element not found";
}

else {
//while loop terminated because elt is found
//and the number to be deleted is not in EOL
if(p == head) {
head = p ­> next;
delete p;
}
else {
ptr ­> next = p ­> next;
delete p;
}
cout << "\n Element deleted";

  }

//___________________________________________________________________________
 
 //search the list for specific number 

  void  NumberList :: searchList(int num)
  {
        int position = 1;
bool found = false;  
        Elem* p = head;

        while(p != NULL) {
                  
if(p ­> number != num){

//traverse the list 
p = p ­> next;
                        position++;
        }

else{ 
    cerr << "\nElement found at position "<< position;
    found = true;
    break;
        }
         } 
 
  if(found == false) {
   // this point is reached when the elt is not found and EOL reached
cout << "\n element not found";
}
  }

//____________________________________________________________________________

  //Display the list

  void NumberList :: displayList()
  {
Elem* p = head;
if(p == NULL) {
cout << "\n List Empty \n";
exit(1); // exit(0)is success others failure
}
else
     cout << "\n current list is: ";

while(p != NULL) {

cout << "\n" << p ­> number;
p = p ­> next;
}
  }
/* AUTHOR: Pari
 * DATE: 06/01/2010
 * FILE: main.C
 * program: impl for singly linked list of numbers
 */

#include <iostream>
#include "LinkedList.h"
using namespace std;

int main()
{
int choice, number;
NumberList listObj;
cout << "\n==================================================== \n";   
cout << "\n enter yout choice \n 1. insert \n 2. delete \n 3. find \n 4. display \n press ctrl+c to exit \n";  

while( cin >> choice ) {

switch(choice) {

case 1: 
cout << "\n enter the number to insert";
cin >> number;
        if(isdigit(number) != 0){
     cerr << "\n only numbers are accepted input";
  break;
}
listObj.insertNumber(number);
break;
 case 2:
cout << "\n enter the number to delete";
cin >> number;
if(isdigit(number) != 0) {
  cerr << "\n only numbers are accepted input";
  break;
}
listObj.deleteNumber(number);
break;
case 3:
cout << "\n enter the number to find its pos";
cin >> number;
if(isdigit(number) != 0) {
                                   cerr << "\n only numbers are accepted input";
                                   break;
                                 }
listObj.searchList(number);
break;
case 4:
listObj.displayList();
break;

default:
   listObj.displayList();
   break;
 } //end of switch case

cout << "\n ============================================";
cout << "\n enter yout choice \n 1. insert \n 2. delete \n 3. find \n 4. display \n press ctrl+c to exit \n";

} //end of while

return 0;
}   
 

Anda mungkin juga menyukai