Important alert: (current site time 10/21/2014 9:10:58 AM EDT)
 

VB icon

basic link list data structure the function

Email
Submitted on: 10/19/2002 11:19:43 AM
By: Sangamner college  
Level: Intermediate
User Rating: By 1 Users
Compatibility: C, C++ (general)
Views: 8666
author picture
(About the author)
 
     This is the basic link list data structure the function was add, print, search and remove integers. it was insert on insert order and remove by picking number.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: basic link list data structure the function
// Description:This is the basic link list data structure the function was add, print, search and remove integers. it was insert on insert order and remove by picking number.
// By: Sangamner college
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4993&lngWId=3//for details.//**************************************

#include <iostream.h>
struct node
{
int num;
node *next;
};
class number
{
public:
number();
~number();
int add(int num, int success);
int remove(int num, int success);
void print();
int search(int num, int success);
private:
node *head;
node *cur;
node *pre;
};
//constructor
number::number()
{
head=NULL;
cur=NULL;
pre=NULL;
}
//destructor number::~number() { node *ptr; while(head!=NULL) //delete all the numbers { ptr=head->next; delete head; head=ptr; } } 
void main()
{
char answer;
int num;
int success=0;
number link;
cout << "This is the the linked list function" << endl;
cout << "\n \n";
for(int i=5; i>3; i++) //loop is for keep the program run untill set it false
{
cout << "Enter 'A' for add a number" << endl; //main menus
cout << "Enter 'R' for remove a number" << endl;
cout << "Enter 'P' for print all the number" << endl;
cout << "Enter 'S' for search fo a number" << endl;
cout << "Enter 'Q' for quit the program" << endl;
cout << "\n\n";
cout << "Enter your choice of A, R, P, S, or Q: ";
cin >> answer;
//if choice was A or a, asked the user to enter a number then call the add
//function, the function will returen success or fail
if((answer=='A')||(answer=='a'))
{
cout << "Enter a number: ";
cin >> num;
success=link.add(num, success);
if(success)
cout << "Add success" << endl;
else
cout << "Add fail" << endl;
}
else if((answer=='R')||(answer=='r'))
{
cout << "Enter a number: ";
cin >> num;
success=link.remove(num, success);
if(success)
cout << "Remove success" << endl;
else
cout << "Remove fail" << endl;
}
else if((answer=='P')||(answer=='p'))
link.print();
else if((answer=='S')||(answer=='s'))
{
cout << "Enter a number: ";
cin >> num;
success=link.search(num, success);
if(success)
cout << "Search success" << endl;
else
cout << "Search fail" << endl;
}
//if the user enter a Q or q, set the for loop at the beginning to false and
//quit the program.
else if((answer=='Q')||(answer=='q'))
i=0;
//if any other input other then A R, P, S, or Q then print out an error message
else
cout << "Error, you choice must be A, R, P, S, or Q";
i--; //the integer i will never get any larger.
} //for loop
}//end of main function
//this is the add function, it will add in the insert order.
int number::add(int num, int success)
{
node *temp=NULL;
//if it was empty the add the first number if(head==NULL) { head=new node; head->num=num; head->next=NULL; success=1; } 
//if the insert num was less then the first num else if(head->num > num) { //then insert at the head cur=head; head=NULL; head=new node; head->num=num; //insert num head->next=cur; success=1; }
//else find the correct spot to insert else { cur=head; //finding a spot while(cur && cur->num <= num) { pre=cur; cur=cur->next; } //insert the new num temp=new node; temp->num=num; //connect the link list temp->next=cur; pre->next=temp; success=1; }
return success; //return success
}
//remove by search number. Search though the list and find a match. if a match was
//find, it will delete it. else go to the next cell. continous untill the end
//of list.
int number::remove(int num, int success)
{
if(head==NULL) //if there isn't any data return 0
{
success=0;
return 0;
}
//if delete the first node if(head->num == num) { cur=head; head=head->next; delete cur; success=1; return 1; } 
else //else delete any node other than the first node
{
cur=head;
pre=head;
cur=cur->next;
while(cur!=NULL) //search all the way to the end of the list
{
if(cur->num ==num) //if match { pre->next=cur->next; delete cur; success=1; return 1; }
pre=cur;
cur=cur->next; //goes to the next one
success=0;
} // end of while loop
} //end of else
return success;
}
//this function will print from the beginning of the list to the end of the list
void number::print()
{
//if the list was empty print out an error message
if(head==NULL)
{
cout << "There isn't any number" << endl;
return;
}
//else while not the end of the list, print out the number and go to the next one
else
{
cur=head;
while(cur!=NULL)
{
cout << "number: " << cur->num << endl;
cur=cur->next;
}
}
}
//the search function was search from the beginning of the head to the end of the
//head.
int number::search(int num, int success)
{
if(head==NULL) //if there isn't any data return 0
{
success=0;
return 0;
}
if(head->num == num) //if delete the first node
{
cout << num << " was found" << endl;
success=1;
return 1;
}
else //else delete any node other than the first node
{
cur=head;
cur=cur->next;
while(cur!=NULL) //search all the to the end of the list
{
if(cur->num ==num)
{
cout << num << " was found" << endl;
success=1;
return 1;
}
pre=cur;
cur=cur->next;
success=0;
}// while
} //esle
return success;
}


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

10/30/2003 6:07:59 PM

Thanks for submitting your code online! It helps people like myself get a better understanding of what to look for when it comes to setting up classes. If you have any other resources to help out beginners like myself to learn how to program, your help would be greatly appreciated

Thanks again,
M.Powell
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.