Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef STACK_LIST_H
- #define STACK_LIST_H
- #include <iostream>
- using namespace std;
- struct Stack_struct
- {
- Stack_struct *next;
- float number;
- };
- template <typename T> class Stack_list
- {
- Stack_struct *head;// the peak of our list
- int length;
- public:
- Stack_list(){ head=0; length=0;}
- Stack_list(int num);
- Stack_list(const Stack_list<T> &stL); // copy constructer
- ~Stack_list()
- {
- while (head!=NULL)
- {
- Stack_struct *temphead;
- temphead=head;
- head=head->next;
- delete temphead;
- }
- }
- void add_list(Stack_struct *&head, float number);
- float get_last(Stack_struct *&head);
- Stack_struct* get_head()const{return head;}
- void add_toliststruct(float addnum);
- float getelement();
- bool Empty_list(){return head==NULL ;}
- int get_length(){return length;}
- template<typename P> friend void operator<<(ostream &out, const Stack_list<T> &L);
- void Print_list();
- Stack_list<T> &operator=(const Stack_list<T> &stList);
- };
- template<typename T>
- Stack_list<T>:: Stack_list(const Stack_list<T> &stL)
- {
- head=new Stack_struct;
- Stack_struct *head1=stL.get_head();
- length=stL.length;
- head->number=head1->number;
- head->next=0;
- Stack_struct *a=head, *p;
- for (int i=1; i<length; ++i)
- {
- p=new Stack_struct;
- head1=head1->next;
- p->number=head1->number;
- a->next=p;
- p->next=0;
- a=p;
- }
- }
- template<typename T>
- Stack_list<T>:: Stack_list(int num)
- {
- length=num;
- head=new Stack_struct;
- cin>>head->number;
- head->next=0;
- Stack_struct *a=head,*p;
- for (int i=1; i<num; ++i)
- {
- p=new Stack_struct;
- cin>>p->number;
- a->next=p;
- p->next=0;
- a=p;
- }
- }
- template<typename T>
- void Stack_list<T>:: Print_list()
- { if (head!=NULL)
- {
- Stack_struct *p=head;
- while(p)
- {
- cout<<p->number<<" ";
- p=p->next;
- }
- }
- }
- template<typename T>
- void Stack_list<T>:: add_toliststruct(float addnum)
- {
- length=length+1;
- add_list(head,addnum);
- }
- template<typename T>
- float Stack_list<T>:: getelement()
- {
- length=length-1;
- return get_last(head);
- }
- template<typename T>
- void operator<<(ostream &out, const Stack_list<T> &L)
- {
- Stack_struct *temphead;
- temphead=L.get_head();
- while (temphead!=NULL)
- {
- out<<temphead->number;
- out<<' ';
- temphead=temphead->next;
- }
- }
- template<typename T>
- Stack_list<T> &Stack_list<T>:: operator=(const Stack_list<T> &stList)
- {
- while (head && head->next)
- {
- Stack_struct *temp=head;
- head=head->next;
- delete temp;
- }
- delete head;
- head=new Stack_struct;
- Stack_struct *head1=stList.get_head();
- length=stList.length;
- head->number=head1->number;
- head->next=0;
- Stack_struct *a=head, *p;
- for (int i=1; i<length; ++i)
- {
- p=new Stack_struct;
- head1=head1->next;
- p->number=head1->number;
- a->next=p;
- p->next=0;
- a=p;
- }
- }
- template<typename T>
- void Stack_list<T>::add_list(Stack_struct *&head, float number)
- {
- if (head==NULL)
- {
- head=new Stack_struct;
- head->next=0;
- head->number=number;
- }
- else
- {
- Stack_struct *addition=new Stack_struct;
- addition->next=head;
- addition->number=number;
- head=addition;
- }
- }
- template<typename T>
- float Stack_list<T>::get_last(Stack_struct *&head)
- {
- if (head==NULL) return 0;
- else
- {
- if (head->next!=NULL)
- {
- float number=head->number;
- Stack_struct *temphead=head;
- head=head->next;
- delete temphead;
- return number;
- }
- else
- {
- float number=head->number;
- delete head;
- head=NULL;
- return number;
- }
- }
- }
- #endif // STACK_LIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement