All pastes #160393 Raw Edit

Unnamed

public cpp v1 · immutable
·published 2006-09-04 06:52 UTC
rendered paste body
#include <iostream>using namespace std;struct node {public:    int d; // The data holder    node * next; // A pointer to the next obj.};void Display();void Init();void AddEnd(int);void AddBeg(int);int ViewSeek(int);void EditSeek(int, int);int Dim();void KillList();// This is a pointer to the first object in the linked list// (set to null, because there currently is none)node * root = NULL;int main() {    Init();    AddEnd(3);    AddEnd(8);    AddBeg(5);    cout << "View Seek: " << ViewSeek(1) << endl;    cout << "Counter: " << Dim() << endl;    Display();    EditSeek(3, 2);    cout << endl;    Display();    KillList();    system( "PAUSE" );    return 0;}// Displays the linked list (excluding the root)void Display() {    node * ptr = root; // Temperary pointer, for following the list until the end    while (ptr -> next != NULL) {        ptr = ptr -> next;        cout << ptr -> d << endl;    }    return;}// Initalizes the root objectvoid Init() {    root = new node;    root -> next = NULL;    root -> d = 0;    return;}// Adds a new object to the end of the list, and assigns the value of 'num'// to the 'd' variablevoid AddEnd(int num) {    node * ptr = root; // Temperary pointer, for following the list until the end    while (ptr -> next != NULL) {        ptr = ptr -> next;    }    if (ptr -> next == NULL) {        ptr -> next = new node;        ptr = ptr -> next;        ptr -> d = num;    }    return;}// Adds a node after the root, and makes 'd' have the passed value, numvoid AddBeg(int num) {    node * ptr = new node;    ptr -> next = root -> next;    ptr -> d = num;    root -> next = ptr;}// This function finds the value in the node # (nodeNum),// assuming 0 is root (nothing in root)// Returns 0, if the location doesn't existint ViewSeek(int nodeNum) {    int j = 1; // The counter, to keep track on witch node its on    node * ptr = root; // Used to go from node to node    for (j = 1;j <= nodeNum;j++) {        if (ptr -> next == NULL) {            return 0;        }        else {            ptr = ptr -> next;        }    }    return ptr -> d;}// Finds the desired node on the list, and edits its 'd' variable// nodeNum - node to edit (assume 0 is root)// num - the number to replace 'd'void EditSeek(int nodeNum,int num) {    int j = 1; // The counter, to keep track on witch node its on    node * ptr = root; // Used to go from node to node    for (j = 1;j <= nodeNum;j++) {        if (ptr -> next == NULL) {            return;        }        else {            ptr = ptr -> next;        }    }    ptr -> d = num;}// Returns the # of nodes in the linked list (excluding the root)int Dim() {    node * ptr = root;    int counter = 0; // Keeps track of the # of nodes    while(ptr -> next != NULL) {        counter++;        ptr = ptr -> next;    }    return counter;}// Deletes all the nodes in the List// Starts at the root, but doesn't delete the root, deletes the next node,// And has the root points to the one after that// At the very end, the root is deletedvoid KillList() {    node * ptr = root; // Temperary pointer    while ((ptr -> next != NULL) && (root -> next != NULL)) {        ptr = ptr -> next;        if (ptr -> next != NULL) {            root = ptr -> next; // make root point to the next node        }        else {            root -> next = NULL;        }        delete ptr; // get rid of lost node        ptr = root;    }    delete root; // finish off the list, by killing the root    return;}