2

Could anyone tell me if this is the basic idea of linked lists? What are the pros and cons to this method and what are best practices when implementing linked lists in C++? Im new to data structures so this is my first approach. If there is a better way to do this same thing, please let me know. Additionally, how would you create the nodes dynamically without hard coding it? Thanks.

#include <iostream>
#include <string>

using namespace std;
struct node {
    int x;
    node *next;
};

int main()
{

    node *head;
    node *traverser;


    node *n = new node;                 // Create first node
    node *t = new node;                 // create second node

    head =n;                            //set head  node as the first node in out list.
    traverser = head;                   //we will first begin at the head node.

    n->x = 12;                          //set date  of first node.
    n->next = t;                        // Create a link to the next node

    t->x = 35;                          //define date  of second node.

    t->next = 0;                        //set pointer to null if this is the last node in the list.


    if ( traverser != 0 ) {                 //Makes sure there is a place to start
        while ( traverser->next != 0 ) {
            cout<< traverser->x;            //print out first data member
            traverser = traverser->next;    //move to next node
            cout<< traverser->x;            //print out second data member

        }
    }
    traverser->next = new node;  // Creates a node at the end of the list
    traverser = traverser->next; // Points to that node
    traverser->next = 0;         // Prevents it from going any further
    traverser->x = 42;
}
3
  • 2
    Since linked list operations are tricky to get right, you'd be much better off defining them as reusable methods within your linked list class, rather than trying to do the right things manually every time you use them. That way you only have to get the operation correct once, rather than dozens/hundreds of times :) Commented Jan 2, 2015 at 2:22
  • The way you're printing out the list looks odd. Why not do while (traverser->next) { cout << traverser->x; traverser = traverser->next; }? Commented Jan 2, 2015 at 2:32
  • Voted to close as too broad. This asks for a tutorial or book. Commented Sep 13, 2018 at 0:04

4 Answers 4

2

for tutorial purpose, you can work out this example:

#include <iostream>

using namespace std;

struct myList
{
    int info;
    myList* next;
};

int main()
{
    //Creation part
    myList *start, *ptr;
    char ch = 'y';
    int number;
    start = new myList;
    ptr = start;
    while (ptr != NULL)
    {
        cout << "Enter no. ";
        cin >> ptr->info;
        cout << "Continue (y/n)? ";
        cin >> ch;
        if (ch == 'y')
        {
            ptr->next = new myList;
            ptr = ptr->next;
        }
        else
        {
            ptr->next = NULL;
            ptr = NULL;
        }
    }

    //Traversal part begins
    cout << "Let's start the list traversal!\n\n";
    ptr = start;
    while (ptr!=NULL)
    {
        cout << ptr->info << '\n';
        ptr = ptr->next;
    }
}

It allocates memory dynamically for as many elements as you want to add.

Sign up to request clarification or add additional context in comments.

Comments

0

I'd prefer to make a linked list class. This eliminates the need to call 'new' more than once. A nice implementation with examples can be found here.

2 Comments

I'd rather use std::list than make my own list class. The std::list class works and has many, many hours of testing behind it.
Obviously STL is the way to go, but if the OP is trying to learn how it works, this is the way to do it.
0

You are in fact already doing dynamic allocation. So, not sure what you are asking for. But if you want to define functions to add new nodes to your linked list (or delete a node etc.), this can be a probable solution:

The location nodes get inserted/deleted is dependent on the type of data-structure. In a queue, new nodes will get added to the end; at the top in case of a stack. A function that adds a node to the top, simulating STACK push operation:

void pushNode(node **head, int Value) {  
  node *newNode = new node;  
  newNode->x = Value;  
  newNode->next = *head;  
  *head = newNode;  
}  

It would be called like pushNode(&head, 15) where 'head' would be defined as node *head = NULL. The root head should initially be set to NULL. After this operation head will point to the newly added node (top of stack).

The approach would be very similar for other data-structures (viz. queues) and works fine. But as you are using C++, I would suggest to define a class for your linked-list and define these functions as methods. That way, it will be more convenient and less error-prone.

Even better use std::list. It's the standard thing, so much portable and robust than a custom implementation.

Comments

0

You can also do it in this way

#include <iostream>
using namespace std;

struct Node{
   int data;
   Node* next;
};

void createList(Node** head ,Node* temp){
int n;
char ch;
temp = *head;
while(temp != NULL){
    cout<<"Enter The Value  ";
    cin>>temp->data;
    cout<<"DO you want to continue(y/n)";
    cin>>ch;
    if(ch=='Y' || ch == 'y'){
        temp->next = new Node;
        temp = temp->next;
    }else{
        temp->next = NULL;
        temp = NULL;
    }
  }
}

void ShowList(Node* head){
    cout<<"your list :"<<endl;
    while(head != NULL){
    cout<<head->data<<" ";
    head = head->next;
}
}

int main()
{   
   //Creation part
   Node *head, *temp;
   createList(&head,temp);
   ShowList(head);

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.