Singly linked list is an art. I really like the way it designed. So efficient in terms of memory and so easy to get append and everything is dynamic.
Here is a code example for how it should work. Kind of weird when I first wrote this code. =============================================================================== #include <iostream> using namespace std; struct List { int num; List *next; }; int i; List* Create() { List *p = NULL; List *q = NULL; List *head = NULL; head = NULL; for (i=0; i<3; i++) { p = new List; cin >> p->num; if (head == NULL) { head = p; } else { q->next = p; } q=p; }; if (head != NULL) { q->next = NULL; } return head; }; void displayList(List *ll) { while (ll != NULL) { cout << ll->num << endl; ll = ll->next; } } int main() { List *head; head = Create(); displayList(head); return 0; } |
AuthorShaowu Pan Archives
December 2017
Categories
All
|