Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class List{
- public:
- List(){ next = NULL; data = 0;}
- int data;
- List* next;
- };
- void output (List* x){
- if(x->next != NULL) {
- cout << x->data << " ";
- x = x->next;
- output(x);
- }
- cout << endl;
- }
- int shift(List*& root, List* head){ // delete one from the end and type data ||-||-/ *5*
- if(root == NULL){
- cout << "I can't do shift. Root is NULL.";
- }else{
- int number = root->data;
- List* node = new List();
- List* x = new List();
- while(head->next->next != NULL){
- head = head->next;
- cout << "test -> " << head->data << " ";
- }
- node = root;
- delete node;
- root = head;
- root->next = NULL;
- return number;
- }
- }
- int pop(List*& root, List*& head){
- cout << head;
- if(root == NULL){
- cout << "I can't do pop. Root is NULL.";
- }else{
- List* node = new List();
- node = head;
- List* x = new List();
- x = head->next;
- delete node;
- head = x;
- delete x;
- }
- cout << head;
- output(head);
- }
- void unshift (List*& root, int value, List*& head){ // add one list from the beginning |new
- if(root == NULL){
- List* node = new List();
- node->data = value;
- node->next = root;
- root = node;
- cout << root->data << endl;
- head = root;
- //cout << "->";
- //output(head);
- delete node;
- }else{
- List* node = new List();
- node->next = head;
- node->data = value;
- root = node;
- head = node;
- //output(head);
- delete node;
- }
- cout << head->data;
- }
- void push (List*& root, int value, List*& head){ // insert line to the end ||-||-|new|
- if(root == NULL){
- List *node = new List();
- node->data = value;
- node->next = root;
- root = node;
- //cout << root->data << endl;
- head = root;
- //cout << "->" << " head - data" ;
- output(head);
- delete node;
- }else{
- List *node = new List();
- node->data = value;
- node->next = root;
- root->next = node;
- root = root->next;
- //cout << root->data;
- delete node;
- }
- }
- int main(){
- List* head = new List();
- head = NULL;
- List* root = new List();
- root = NULL;
- push(root, 10, head);
- push(root, 20, head);
- //cout << pop(root, head);
- unshift(root, 11, head);
- // output(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement