Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- typedef struct Node
- {
- int number;
- struct Node *next;
- } Node;
- Node *makeNode(int number)
- {
- Node *temp = new Node;
- temp -> number = number ;
- temp -> next = NULL;
- return temp;
- }
- void addLast(Node *head, Node *node)
- {
- Node *current = head;
- while (current->next != NULL)
- {
- // Run until the last node in the list
- current = current->next;
- }
- current->next = node;
- }
- void printList(Node *head)
- {
- Node *current = head;
- while (current != NULL)
- {
- cout << current->number << "->";
- current = current-> next;
- }
- cout << "NULL" << endl;
- }
- Node *function(Node *head)
- {
- Node *head2 = NULL, *current2 = NULL;
- Node *current = head;
- while (current != NULL)
- {
- if (head2 == NULL)
- {
- head2 = current->next;
- current2 = head2;
- }
- else
- {
- current2->next = current->next;
- current2 = current2->next;
- }
- if (current->next != NULL)
- {
- current->next = current->next->next;
- }
- current = current->next;
- }
- return head2;
- }
- int main()
- {
- Node *list1 = makeNode(2), *list2;
- addLast(list1, makeNode(4));
- addLast(list1, makeNode(8));
- addLast(list1, makeNode(16));
- addLast(list1, makeNode(24));
- addLast(list1, makeNode(46));
- addLast(list1, makeNode(50));
- addLast(list1, makeNode(7));
- cout << "List 1:" << endl;
- printList(list1);
- cout << endl;
- list2 = function(list1);
- cout << "List 1 modified:" << endl;
- printList(list1);
- cout << endl;
- cout << "List 2:" << endl;
- printList(list2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement