sobha
sobha

Reputation: 175

Review of this linked list program

This is a program on the linked list which prints the given input. But is it possible to do it Without using the if(start==NULL) ...else .. statement?

#include <stdio.h>
#include <malloc.h>
struct node
{
    int data;
    struct node* next;
}* start = NULL;

void main()
{
    struct node* tmp;
    struct node *ptr, *tmp1;

    int n = 0;
    int choice = 2;

    while (1)
    {
        printf("Enter a number less than 3 to continue");
        scanf("%d", &choice);
        if (choice >= 3)
            break;

        printf("Enter the input");
        scanf("%d", &n);
        // n=n+10;
        if (start == NULL)
        {
            tmp = (struct node*)malloc(sizeof(struct node));
            start = tmp;
            start->data = n;
            ptr = start;
        }
        else
        {
            tmp = (struct node*)malloc(sizeof(struct node));
            ptr->next = tmp;
            tmp->data = n;
            ptr = tmp;
        }
    }
    tmp->next = NULL;


    printf("\nThe output of the linked list is\n");
    n = 0;
    ptr = start;
    while (ptr != NULL)
    {
        printf("\n  1 ptr =           %d", ptr->data);
        ptr = ptr->next;
    }
}

Upvotes: 1

Views: 102

Answers (3)

chux
chux

Reputation: 154280

is it possible to do it Without using the if(start==NULL) ...else .. statement?

Yes

Create a head node and use only its .next member.

#include <stdio.h>
#include <malloc.h>

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

int main(void) {
  struct node head = {.next = NULL};  // only need .next 
  struct node *ptr = &head;

  while (1) {
    // ...

    int n;
    printf("Enter the input ");
    fflush(stdout);
    if (scanf("%d", &n) != 1) {
      break;
    }
    ptr->next = malloc(sizeof *ptr->next);
    if (ptr->next == NULL) {
      break;
    }
    ptr = ptr->next;
    ptr->data = n;
    ptr->next = NULL;
  }

  printf("\nThe output of the linked list is\n");
  ptr = head.next; // The start of the list is here
  while (ptr) {
    printf("  1 ptr =           %d\n", ptr->data);
    ptr = ptr->next;
  }
}

Upvotes: 1

Muhammad Ibtihaj Tahir
Muhammad Ibtihaj Tahir

Reputation: 636

Well, a lot of things are not necessary.

You can simply define a node as

struct node 
{
    int data;
    node * next; // Will hold the value of next node's address
};

Now you can simply make a Class of the data structure with methods.

class LinkedList {
  private:
    node *head, *current, *temp; // 3 Main pointers
  public:

    LinkedList() {
      head = temp = current = NULL; // Set them to NULL
    }

    void insert(int data) { // to add a new node
      if(head == NULL) { // if there is no node in the memory
        head = new node; // use head to make a new node
        head->data = data; // set the data of the node to data
        head->next = NULL; // set the next to NULL
        temp = current = head; // 3 pointers sitting at the head node
      }
      else { // If the node is not the first one in the memory
        current = new node; // use current pointer to make new node
        temp->next = current; // temp pointer one step behind the current node
        current->next = NULL; // make the next of new node NULL
        temp = current; // move the pointer to current
        current->data = data;
      }
    }
};

This function will simply add nodes on every call.

To display the list, Simply move the temp pointer to head and start the iteration

void display()
{
    temp = head; // move to head
    while(temp != NULL) // while temp doesn't reach the end
    {
        cout<<temp->data<< " "; // print the node's data
        temp = temp->next; // move the pointer to next node
    }
}

Upvotes: -1

Rishikesh Raje
Rishikesh Raje

Reputation: 8614

The start variable holds the head of the linked list.

The condition if (start == NULL) checks for an empty list.

If the list is empty, you need to create the head of the list. The second element onwards, you need to link the next element to the last element of the list. This is taken care by the else condition.

To look at it another way, if you do not have the if condition, you have the line

 ptr->next = tmp;

Since ptr is not initialized at this time, you will have undefined behaviour and most likely get a segmentation fault.

Upvotes: 1

Related Questions