Jump to content

Linked List

Go to solution Solved by WanderingFool,

How you make a linked list print out can change significantly from language to language (Concept is similar but the look is quite different)

 

Assuming C++

struct Node { int value; struct Node *Next;}//Assumes Next will be 0 when list has endedvoid printList(struct Node* parent) {    struct Node* current = parent;    while(current != 0) {       std::cout << current->value;       current = current->Next; //Set current to whatever is next in the list   }}

C#

void printList(LinkedList list) {    foreach(var item in list) {        Console.print(item); //Sorry can't remember the console print off hand but whatever the print function is    }}

As for double linked lists, it all depends on the language again...basically for c++ you do the same as a linked list but add an extra variable that points to the previous node (code should be similar for adding)

To create a doubly linked list, you'd just need another pointer in each node to point to the previous node in the series.

 

To print them out, you might have a function in your class that loops through all the nodes and spits the contents out sequentially (like a toString() method in Java). In order to loop through without going past the bounds, you'll probably have to store a size class variable in order to maintain bounds-checking and avoid dereferencing potentially null pointers.

Link to comment
https://linustechtips.com/topic/45199-linked-list/#findComment-593791
Share on other sites

Link to post
Share on other sites

How you make a linked list print out can change significantly from language to language (Concept is similar but the look is quite different)

 

Assuming C++

struct Node { int value; struct Node *Next;}//Assumes Next will be 0 when list has endedvoid printList(struct Node* parent) {    struct Node* current = parent;    while(current != 0) {       std::cout << current->value;       current = current->Next; //Set current to whatever is next in the list   }}

C#

void printList(LinkedList list) {    foreach(var item in list) {        Console.print(item); //Sorry can't remember the console print off hand but whatever the print function is    }}

As for double linked lists, it all depends on the language again...basically for c++ you do the same as a linked list but add an extra variable that points to the previous node (code should be similar for adding)

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/45199-linked-list/#findComment-594992
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×