Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct LIST {
- int number;
- struct LIST* next;
- };
- void insertList ( LIST* newList ) {
- int count = 0;
- int input;
- cin >> input;
- if ( input != 0 ) {
- newList -> number = input;
- newList -> next = new LIST;
- LIST* walkList = newList -> next;
- while ( 1 ) {
- cin >> input;
- if ( input != 0 ) {
- walkList -> number = input;
- walkList -> next = new LIST;
- walkList = walkList -> next;
- }
- else {
- walkList -> number = 0;
- break;
- }
- }
- walkList -> next = NULL;
- }
- }
- void printList ( LIST* newList ) {
- LIST* walkList;
- for ( walkList = newList; walkList != NULL ; walkList = walkList -> next ) {
- cout << walkList -> number << " ";
- }
- cout << endl;
- }
- int lengthList ( LIST* newList ) {
- LIST* walkList;
- int count = 0;
- for ( walkList = newList; walkList != NULL ; walkList = walkList -> next ) {
- count++;
- }
- return count;
- }
- void reverseList ( LIST* &newList ) {
- LIST* first;
- LIST* second;
- LIST* walkList;
- int length = lengthList ( newList );
- LIST* lastElem = newList;
- for ( int i = 1; i < length; i++ )
- lastElem = lastElem -> next;
- for ( int i = 0; i < length - 1; ++i ) {
- walkList = newList;
- for ( int j = 1; j < length - i; ++j ) {
- first = walkList;
- second = walkList -> next;
- walkList = walkList -> next;
- }
- second -> next = first;
- }
- LIST* temp = newList;
- newList = lastElem;
- lastElem = temp;
- lastElem -> next = NULL;
- }
- void insertAfter ( LIST* newList, int n, int itemToAdd ) {
- LIST* walkList = newList;
- LIST* before;
- LIST* after;
- for ( int i = 0; i < n; ++i ) {
- before = walkList;
- walkList = walkList -> next;
- after = walkList;
- }
- LIST* newItem;
- newItem = new LIST;
- newItem -> number = itemToAdd;
- newItem -> next = after;
- before -> next = newItem;
- }
- void insertBefore ( LIST* newList, int n, int itemToAdd ) {
- LIST* walkList = newList;
- LIST* before;
- LIST* after;
- for ( int i = 0; i < n - 1; ++i ) {
- before = walkList;
- walkList = walkList -> next;
- after = walkList;
- }
- LIST* newItem;
- newItem = new LIST;
- newItem -> number = itemToAdd;
- newItem -> next = after;
- before -> next = newItem;
- }
- int main () {
- LIST* newList;
- newList = new LIST;
- int itemToAdd;
- int where;
- cout << "Uzupelnianie listy. 0 konczy wpisywanie: ";
- insertList ( newList );
- cout << "Odwrocona lista: ";
- reverseList ( newList );
- printList ( newList );
- cout << endl;
- cout << "// insertBefore " << endl;
- cout << "Liczba do wstawienia: ";
- cin >> itemToAdd;
- cout << "Przed ktora pozycja: ";
- cin >> where;
- insertBefore ( newList, where, itemToAdd );
- cout << "Po wstawieniu: ";
- printList ( newList );
- cout << endl;
- cout << "// insertAfter" << endl;
- cout << "Liczba do wstawienia: ";
- cin >> itemToAdd;
- cout << "Po ktorej pozycji: ";
- cin >> where;
- insertAfter ( newList, where, itemToAdd );
- cout << "Po wstawieniu: ";
- printList ( newList );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement