[cpp] LAB5fin
Viewer
- #include <iostream>
- using namespace std;
- struct Node
- {
- int data;
- Node *next;
- };
- void insertNodeBeg(Node* head, int data);
- void getOddNumber(Node* odd, Node* head);
- void displayList(Node* head);
- void insertNodeEnd(Node* head, int data);
- void sortList(Node* head);
- void change(Node* head, int data, int x);
- int main()
- {
- Node* head;
- head = new Node;
- head ->data = 3;
- head->next = NULL;
- insertNodeEnd(head, 7);
- insertNodeEnd(head, 11);
- insertNodeEnd(head, 9);
- insertNodeEnd(head, 1);
- insertNodeEnd(head, 2);
- insertNodeEnd(head, 3);
- insertNodeEnd(head, 7);
- insertNodeEnd(head, 45);
- insertNodeEnd(head, 46);
- cout << "----All List----" << endl;
- displayList(head);
- Node* odd;
- odd = new Node;
- odd->data = 3;
- odd->next = NULL;
- cout << "----Odd List----" << endl;
- getOddNumber(odd, head);
- displayList(odd);
- cout << "----Add Odd Number to the End of the Odd List----" << endl;
- insertNodeEnd(odd, 101);
- displayList(odd);
- cout << "----Add Odd Number to the Top of the Odd List----" << endl;
- insertNodeBeg(odd, 101);
- displayList(odd);
- cout << "----Sort Head List----" << endl;
- sortList(head);
- displayList(head);
- cout << "----Change a Numberr----" << endl;
- change(head, 45, 13);
- displayList(head);
- }
- void getOddNumber(Node* odd, Node* head)
- {
- Node* temp;
- temp = head;
- while (temp != NULL)
- {
- if (temp->data % 2 == 1 && temp->data != 3)
- {
- insertNodeEnd(odd, temp->data);
- temp = temp->next;
- }
- else
- {
- temp = temp->next;
- }
- }
- }
- void displayList(Node* head)
- {
- Node* temp;
- temp = head;
- while (temp != NULL)
- {
- cout << temp->data << " -> ";
- temp = temp->next;
- }
- cout << "NULL" << endl<<endl;
- }
- void insertNodeEnd(Node* head, int data)
- {
- if (head == NULL)
- {
- head = new Node;
- head->data = data;
- head->next = NULL;
- }
- else
- {
- Node* tempPtr;
- tempPtr = new Node;
- tempPtr->data = data;
- tempPtr->next = NULL;
- Node* temp;
- temp = head;
- while (temp->next != NULL)
- {
- temp = temp->next;
- }
- temp->next = tempPtr;
- }
- }
- void insertNodeBeg(Node* head, int data)
- {
- head->data = data;
- }
- void sortList(Node* head)
- {
- Node* temp, *i, *j;
- temp = head;
- int tmp;
- for (i = head; i->next != NULL; i = i->next)
- {
- for (j = i; j->next != NULL; j = j->next)
- {
- if (i->data > j->data)
- {
- tmp = i->data;
- i->data = j->data;
- j->data = tmp;
- }
- }
- }
- }
- void replaceDataWithX(Node* head, int data, int x)
- {
- Node* temp;
- temp = head;
- while (temp->next != NULL)
- {
- if (temp->data == data)
- {
- temp->data = x;
- temp = temp->next;
- }
- else
- {
- temp = temp->next;
- }
- }
- }
Editor
You can edit this paste and save as new: