Anda di halaman 1dari 7

/*******************************************************/ // Auther : Muhammad Umair // Email : realumair@gmail.

com // Creation Date: 08/10/2012 // Discription : In this program all basic operations are // implimented to understand singly link list // like (add, insert, delete, search etc.) /*******************************************************/ #include<iostream> #include<conio.h> #include<string.h> #include<stdio.h> #include<cstdlib> using namespace std; /***** Globle Variables *****/ char choice; /***** Section End *****/ struct studentInfo { string name; int id; double score; studentInfo *next; }; // class to perform the operations on link list class listADT { private: // private variables // structured pointer variables studentInfo *head, *current, *tail, *temp, *temp1; int count, key, posChoice; // some variables used in program double sum; bool found; // bool true and false public: // public member functions. listADT(); // default constractor. ~listADT(); // distructor bool isEmpty(); // function to check the emptyness. void addRecord(); // function to add new node or record runtime. void insert(); // this function finds the position and then add node // before or after that position on use r choice. bool posSearch(); // function to search the position used in addAtPos(); function. void update(); // this function get id from user and if found then updat e void print(); // print all data in link list.. void search(); // search the given id or roll number. void delRecord(); // this function delete the specified record in LL. void addNode(); // extra function to make new node on required. }; /*********Code for Class Member Function*********/ listADT::listADT() { // default constractor body // initialize all necessary variables head = NULL; tail = NULL; temp = NULL; temp1 = NULL; current = NULL;

count= 1; // int sum = 0; //double found = false; // bool } void listADT::addNode() { // make memory space for program current = new studentInfo; // get informations from user cout<<"\nEnter Roll: "; cin>>current->id; cout<<"\nEnter Single Name: "; cin>>current->name; cout<<"\nEnter Your Phone No: "; // score between 0-100 cin>>current->score; }// end addNode body void listADT::addRecord() { // this function add new student record // condition check if first node is not made yet if(head==NULL) { head = new studentInfo; // here it made head node // information input by user cout<<"\nEnter Roll: "; cin>>head->id; cout<<"\nEnter First Name: "; cin>>head->name; cout<<"\nEnter Your Phone No: "; cin>>head->score; head->next = NULL; // default head next address is set to NULL tail = head; // tail points the head } else { // if head is already maked then this condition works temp = new studentInfo; // occupied space for temp variable temp->next = NULL; // information input by user cout<<"\nEnter Roll: "; cin>>temp->id; cout<<"\nEnter First Name: "; cin>>temp->name; cout<<"\nEnter Your Phone No: "; cin>>temp->score; tail->next=temp; // now tail next address points the temp tail = temp; // now tail point the temp } }// add record function body ends bool listADT::posSearch() { // this function find position and work in insert fu nciton. if(head==NULL) { // condition if head is NULL then means that list is em pty cout<<"List is empty!"; return false; //return the false } current = temp = head; // two pointer points the head key = 0; // initialise by 0 inthe function // get roll no cout<<"\nEnter Roll No to Search: "; cin>>key; // search in while loop untill temp equals to NULL while(temp) {

if(temp->id==key ) { return true; // if roll no found } /*else if(temp->next->info==key) { return; }*/ current = temp; // current points the previous position temp = temp->next; // temp move to next address }// end loop body cout<<"\nRoll No. not found!"<<endl; // incase if given input not found return false; // returns bool expression as false }// end position search function body void listADT::insert() { // function insert node on desired position in LL. if(head==NULL) { // condition check cout<<"\nList is Empty!"<<endl; getch(); return; // return from function if LL empty. } // if list is not empty temp = head; // temp points head // condition is position search return true of false if(!posSearch()) { getch(); return; // if this get false then this function terminates } // if previous condtion true then cout<<"\n\nTo Insert Before (1)/After (2): "; cin>>posChoice; // condition evaluate choice if(posChoice==1) { // to insert before if(head->id==key) { addNode(); // call adnode function current->next=head; head=current; cout<<"\n\nOperation Success!"<<endl; getch(); return; } else { temp = head; while(temp) { if(temp->id==key) { addNode(); current->next=temp1->next; temp1->next=current; cout<<"\n\nOperation Success!"<<endl; getch(); return; } temp1 = temp; temp = temp->next; } }

} else if(posChoice==2) { addNode(); current->next=temp->next; temp->next=current; cout<<"\n\nOperation Success!"<<endl; getch(); return; } else { cout<<"\nInvalid Choice!"<<endl; getch(); } return; } void listADT::update() { if(head==NULL) { cout<<"\nList is Empty!"<<endl; getch(); return; } temp = head; posSearch(); cout<<"\n\nEnter new data to update..\n"; if(temp->id==key) { cout<<"\nEnter New Roll No: "; cin>>temp->id; cout<<"\nEnter New single Name: "; cin>>temp->name; cout<<"\nEnter New Phone No: "; cin>>temp->score; cout<<"\n\nUpdate Success!"<<endl; print(); getch(); return; } } void listADT::delRecord() { if(head==NULL) { cout<<"\nList is empty!"<<endl; getch(); return; } temp = head; key = 0; cout<<"\nEnter Roll No to Delete: "; cin>>key; if(head->id==key) { temp=head->next; delete head; head = temp; print(); getch(); return; } else { while(temp) { if(temp->next->id==key) {

temp1 = temp->next; temp->next = temp1->next; delete temp1; print(); getch(); return; } temp = temp->next; } } } void listADT::search() { if(head==NULL) { cout<<"\nList is empty!"<<endl; getch(); return; } temp = head; key = 0; count = 0; cout<<"\nEnter Roll No to Search: "; cin>>key; while(temp) { if(temp->id==key) { count++; cout<<"\nRecord Found! "<<endl; cout<<"\nSn. "<<" Roll No"<<"\t\tName"<<"\t\tScore"<<e ndl; cout<<"------------------------------------------------"<<endl; cout<<count<<" "<<temp->id <<"\t\t"<<temp->name <<"\t\t"<<temp->score<<endl; getch(); return; } temp = temp->next; } cout<<"\nNot found!"<<endl; getch(); return; } void listADT::print() { if(head==NULL) { cout<<"\nThere is no data to print!"<<endl; getch(); return; } temp=head; count = 1; sum = 0; cout<<"\nSn. "<<" Id"<<"\tName"<<"\tScore"<<endl; cout<<"-------------------------------"<<endl; while(temp) { cout<<count<<" "<<temp->id <<"\t"<<temp->name <<"\t"<<temp->score<<endl; sum = sum + temp->score;

temp = temp->next; count++; } cout<<endl; cout<<"-------------------------------"<<endl; cout<<"\tTotal Students : "<<(count-1)<<endl; cout<<"\tClass Average : "<<(sum/(((count-1))*100))*100 <<endl<<endl; getch(); return; } listADT::~listADT() { delete temp; delete head; delete temp1; delete current; //cout<<"All Data Cleared!"<<endl; } /********* End Coding Section *********/ void manu() { system("CLS"); // clear screen cout<<"\n ~ Phonebook Linked List ~ \n"; cout<<"*******************************\n\n"; cout<<"(1) Add New Data \n"; cout<<"(2) Insert Record at Position\n"; cout<<"(3) Search a Record \n"; cout<<"(4) Delete Data Record \n"; cout<<"(5) Print Data Record List \n"; cout<<"(6) Updata Previous Record \n"; cout<<"(7) To Exit \n\n"; cout<<" Enter Choice ie(1-7) : "<<flush; choice = getche(); } int main() { listADT obj; while (1) { do { manu(); }while (choice < '1' || choice > '8'); system("cls"); //After main switch (choice) { case '1': obj.addRecord(); break; case '2': obj.insert(); break; case '3': obj.search(); break; case '4': obj.delRecord(); break; case '5': obj.print();

break; case '6': obj.update(); break; case '7': obj.~listADT(); //system("pause"); return 0; break; }//end switch }//end while //system("pause"); }

Anda mungkin juga menyukai