Created
April 16, 2019 21:59
-
-
Save VladRez/5081cd157ec2b75d884e026204e15cf0 to your computer and use it in GitHub Desktop.
C++ linked list with pointers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| struct CD { | |
| int trackNumber; | |
| CD * nextSong; | |
| CD(int selection, CD * track) { | |
| trackNumber = selection; | |
| nextSong = track; | |
| } | |
| }; | |
| typedef CD * playList; | |
| playList reverseTrack(playList); | |
| int main(void) { | |
| playList MixTape = new CD(1, nullptr); | |
| playList selection = MixTape; | |
| for (int i = 2; i < 4; i++) { | |
| selection = (selection->nextSong = new CD(i, MixTape)); | |
| } | |
| selection = selection->nextSong; | |
| for (int i = 0; i < 3; i++) { | |
| std::cout << selection->trackNumber << "|"; | |
| selection = selection->nextSong; | |
| } | |
| std::cout << std::endl; | |
| selection = selection->nextSong; | |
| reverseTrack(selection); | |
| for (int i = 0; i < 3; i++) { | |
| std::cout << selection->trackNumber << "|"; | |
| selection = selection->nextSong; | |
| } | |
| std::cout << std::endl; | |
| } | |
| playList reverseTrack(playList selection) { | |
| playList tempStorage; | |
| playList tailPointer = selection; | |
| playList reverseLink = NULL; | |
| while (tailPointer != NULL) { | |
| tempStorage = tailPointer->nextSong; | |
| tailPointer->nextSong = reverseLink; | |
| reverseLink = tailPointer; | |
| tailPointer = tempStorage; | |
| } | |
| return reverseLink; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment