Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- struct node {
- node* next;
- int data;
- };
- class slist {
- private:
- node* head, *tail;
- public:
- slist(void):head(NULL), tail(NULL){}
- ~slist(){ clear(); }
- public:
- node* add(int data){
- node* p = new node();
- if(p != NULL){
- p->data = data;
- p->next = NULL;
- if(head == NULL)
- head = tail = p;
- else
- tail = tail->next = p;
- }
- return p;
- }
- //удаление всех
- void clear(void){
- node* t;
- while(head != NULL){
- t = head;
- head = head->next;
- delete t;
- }
- tail = NULL;
- }
- node* begin(void) { return head; }
- node* begin(void) const { return head; }
- bool empty(void) const { return (head == NULL); }
- };
- //печать
- void slist_print(std::ostream& _out, const slist& lst){
- for(const node* p = lst.begin(); p != NULL; p = p->next)
- _out << p->data <<" ";
- _out << std::endl;
- }
- void add_last(slist& lst){
- int counter=0;
- for(const node* p = lst.begin();p!=NULL;p = p->next)
- if(p->data%2!=0)
- counter++;
- lst.add(counter);
- }
- int main(){
- slist lst;
- for(int i=0;i<15;i++){
- lst.add(i);
- }
- slist_print(std::cout,lst);
- add_last(lst);
- slist_print(std::cout,lst);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement