Important alert: (current site time 10/23/2014 4:17:44 AM EDT)
 

VB icon

The stack link list data structure

Email
Submitted on: 10/19/2002 11:23:07 AM
By: Sangamner college  
Level: Advanced
User Rating: By 1 Users
Compatibility: C, C++ (general)
Views: 9685
author picture
(About the author)
 
     This is the stack link list data structure it have the structure of stack it have the function of add, remove, print, and search.
 
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: The stack link list data structure
// Description:This is the stack link list data structure it have the structure of stack it have the function of add, remove, print, and search.
// By: Sangamner college
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4995&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 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 return success or fail
if((answer=='A')||(answer=='a'))
{
cout << "Enter a number: ";
cin >> num;
success=link.add(num, success);
if(success==1)
cout << "Add success" << endl;
else
cout << "Add fail" << endl;
}
else if((answer=='R')||(answer=='r'))
{
success=link.remove(num, success);
if(success==1)
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==1)
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
}// main
//this is the link list add function by stack. If the it was empty, make a new cell
//copy the number to the new cell, else current equal to head, head equal to new cell
//copy the number to the head-> new cell. and new cell ->next equal to current. this
//will connect the list together.
int number::add(int num, int success)
{
if(head==NULL)
{
head=new node;
head->num=num;
head->next=NULL;
success=1;
}
else
{
cur=head;
head=NULL;
head=new node;
head->num=num;
head->next=cur;
success=1;
}
return success;
}
//****remove at the head, this is stack, add at the head remove at the head.
int number::remove(int num, int success)
{
node *temp; //make a temp pointer
if(head!=NULL) //if the list is not empty
{
temp=head->next; //temp equal to head ->next
delete head; //delete head, remove the first data of the list
head=temp; //now the second data of the list become the first data of the list
return success=1; //return success
}
else
return success=0; //if the list empty return fail
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
} //else
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 Advanced 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


 There are no comments on this submission.
 

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.