List using pointer

Write your question here.
Can someone please explain the working of this program in detail. Especially that strict node *next.. Explain what's a node and how does it store data in it.. Even that temp->next i don't understand anything.. Please help me..Thank you in advance
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
struct node*next;
}

class list
{
public:
struct node*head;
list ()
{head=NULL;}

insertbeg()
{
struct node*temp;
temp=new node;
cin>>temp->data;
temp->next=NULL;
if(head==NULL)
{
head=temp;
return;
}
else
{
temp->next=head;
head=temp;
}
}l;

Last edited on
a linked list is a data structure where nodes are attached . A node contains the pointer to the next node in the list . nodes are create in the heap , which means there are not alligned in memory opposed to arrays and vectors , that is why we need to attached them .
To access an element in the list , you have to iterate from the head until you find the corresponding node or reached the given (sort of) index.

your list should contain a node called head which is the first element and a node tail which is always equals to null . Make sure that this node is always attached to the last element of the list.
then for iterating through the list you simply do something like that :

1
2
3
4
5
6
7
8
9
10
11
12
bool contains(int value)
{
   Node* current = head;

   while(current != tail)
   {
     if(current->data == value)
       return true;
     current = current->next;
   }
   return false;
}

Was that clear ?
Topic archived. No new replies allowed.