1
class Nodetype
{
  int info;
  Nodetype next;

  Nodetype(int i)
  {
     info=i;
     next=null;
  }
}

My textbook has this code to create Linked List dynamically. The question is, when the programs is executed line-by line, it defines variable 'info' as type 'int' & then variable 'next' as Nodetype.

What is actually happening here?

does it mean that variable 'next' will contain -

  1. Constructor 'Nodetype'
  2. int info
  3. Nodetype "next" where "next" will again have all 1,2,3 & then 3 will again have 1,2,3...and so on....till infinity?

I'm really irritated because I'm unable to understand how it works, can someone easily explain this?

9 Answers 9

3

Your code follows very well the definition of list: a list is null or an element followed by a list.
The "element", in your case, is defined by an int value, and the "followed by" part is the next variable; in Java variables (when they are not literals, as int values are) are actually pointers, so while they are not initialized they don't store any valid value and they don't point to any memory area (i.e. their value is null), so while the next variable is kept as-is your element is not followed by any other. To dynamically add elements to your list you need a pointer to the last element you added, otherwise you would not be able to find them again:

int i = 0;
Nodetype head = new Nodetype(i++);
Nodetype last = new Nodetype(i++);
head.next = last;
while (i<5) {
    Nodetype temp = new Nodetype(i++);
    last.next = temp;
    last = temp;
}
while(head) {
    System.out.println(head.info);
    head = head.next;
}

Notice how, with the last few lines, you lose the head pointer and you have no way to get back the starting point of your list.. Keep that in mind when working with lists ;)

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

Comments

1

At first variable next doesn't point to any object(it points to null). At some time you will make it point to another node with next = new NodeType(number). The idea is that you use composition - you have one instance of class which has a reference to another instance. It is like nodeA points to nodeB, nodeB points to nodeC. Here you have three instances and the first instance has a reference to the second instance and the second instance has a reference to the third instance. The third instance is the last one and its next instance points to null.

Comments

1

the field next is a reference to an object of type Nodetype. at first it will point to nothing - since it is instantiated to null. when you assign a value to it, it will point to that value only, nothing will continue infinitely unless you create a cycle within the list.

Comments

1

You created class NodeType and inside of the class you defined object of that class. So that object (in your example next) will have int info variable NodeType next object and constructor.

Comments

1

It will contain Null, as the variable is not initialized to any value.

Comments

1

Nodetype is your class that defines the data a node instance will contain as well as the reference to the next node in the linked list. That reference to the next node will be an object of type Nodetype. Nothing too difficult here, this is the classic implementation of a Linked List.

You might want to check out this great linked list resource from Stanford.

Comments

1

The way this works is that the list is made up of single elements, each of which only has a pointer to the one that comes after it:

Nodetype next;

The information each element within the list actually holds is this:

int info;

You can think of a list like a "chain": it's not really a single object, but a compound object of a number of links. From each link, you can only see the next link (or, in case of linked lists that have references in both directions: the next and the previous link), so in order to have all elements available, you will have to keep the reference to the first element in the "chain".

Note: List objects are single objects that have a reference to the first link of the "chain".

Comments

1

next is a reference to another Nodetype instance. If next == null it means the current element is the last one in the list.

Let's see an example:

Nodetype node = new Nodetype(0);        // i = 0, next = null
Nodetype anotherNode = new Nodetype(1); // i = 1, next = null
node.next = anotherNode;                // now the first node has a ref to the second

Comments

0
#include<stdio.h>
#include<stdlib.h>

void print_list(int *arr,int *size,int *capacity)
{
     printf("capacity = %d; size = %d; elements = ",*capacity,*size);
     for(int i=0;i<(*size);i++){
        printf("%d ",arr[i]);
    }
     printf("\n");
}

int * push_back(int *arr,int data,int *size,int *capacity)
{
    int *b;
    if(*size == *capacity){

        *capacity = 2*(*capacity);
        b = (int *)malloc(sizeof(int)*(*capacity));

        for(int i=0;i<(*size);i++){
        b[i]= arr[i];
        }
        b[*size]=data;
        *size=*size+1;
        print_list(b,size,capacity);
        return b;
    }

     arr[*size]=data;
     *size=*size+1;
     print_list(arr,size,capacity);

     return arr;
}
int main()
{
     int size=0;
     int n;
     int x;
     int *arr;
     arr = (int *) malloc(sizeof(int));
     int capacity=1;

     scanf("%d",&n);

     for(int i=0;i<n;i++){
        scanf("%d",&x);
        arr=push_back(arr,x,&size,&capacity);
     }
}

its working.

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.