Chapter 3 linked list
Chapter 3 linked list
Static memory allocation:-the memory is allocated to the entities by the operating system. Once
the memory blocks allocated to the entities, they remain reserved till the end of program.
E.g:- arrays
E.g. C,C++, Pascal does not have garbage collector so they allow programmer to destroy the data
structure., but Java has garbage collector so it does not use destroy operator.(automatic de
allocation/release of memory using finalize())
2. The data in the array are separated in the computer memory by the same distance. i.e.
inserting an item inside the array requires shifting other data in the array
Linked lists:
Nodes can be located anywhere in the memory and passing from one node of the linked
structure is to another is accomplished by storing the address of other nodes in the linked
structure.
Linked lists allow you to have a chain of structs with related data.
The key part of a linked list is a structure, which holds the data for each node (the name, address,
age or whatever for the items in the list), and, most importantly, a pointer to the next node. Here we
have given the structure of a typical node:
struct node
{
char name[20]; // Name of up to 20 letters
int age;
float height; // In metres
node *next;// Pointer to next node
};
struct node *start_ptr = NULL;
2
1. creating a linked list start_ptr=temp;
3
4. adding somewhere in the list 6. deleting from the end
cin>>loc; {
{ start_ptr=NULL;}
temp=temp->nxt; else
if(temp==NULL) {
//delete from start node cout << "Name : " << temp->name <<
endl;
temp=start_ptr;
cout << "Age : " << temp->age << endl;
start_ptr=start_ptr->nxt ;
cout << endl; // Blank line
delete temp;
// Move to next node (if present)
4
temp = temp->nxt;
A doubly linked list is one where there are links from each node in both directions:
The reason we needed a start pointer in the ordinary linked list is because, having moved on
from one node to another, we can't easily move back, so without the start pointer, we would lose
track of all the nodes in the list that we have already passed. With the doubly linked list, we can
move the current pointer backwards and forwards at will.
Eg:- if we are at 15th node, to reach the 14th node we have to traverse the list right from the 1st
node (in singly linked list). But in doubly linked list there is a ptr called prev.
5
1. adding doubly linked list (end)
{ start_ptr=temp;
}; temp=temp->nxt;
void main()
{ temp2=new node;
temp->nxt = NULL; }
//display start_ptr=NULL;
temp=start_ptr; }
start_ptr=start_ptr->nxt ; else
start_ptr->prev=NULL; {
else temp2->prev=temp->prev;
{ }
temp=start_ptr; }
if(temp->nxt==NULL)
#include<iostream.h> {
struct node struct node *start_ptr = NULL;
{ node *temp;
char name[20]; // Name of up to 20 letters temp = new node;
int age ;
node *nxt;// Pointer to next node cout << "Please enter the name of the
person: ";
};
cin >> temp->name;
void main()
7
cout << "Please enter the age of the person : temp->nxt= NULL;
";
}
cin >> temp->age;
else
temp->nxt = NULL;
{ node *temp2;
if (start_ptr == NULL)
temp= start_ptr;
start_ptr = temp;
while(temp->nxt!= NULL)
temp=temp->nxt;
//add at the begining
temp2=new node;
//char a[5]="duga"
cout << "Please enter the name of the
temp=new node; person: ";
cout << "Please enter the name of the cin >> temp2->name;
person: ";
cout << "Please enter the age of the person :
cin >> temp->name; ";
cout << "Please enter the age of the person : cin >> temp2->age;
";
temp2->nxt =NULL;
cin >> temp->age;
temp->nxt=temp2;
temp->nxt= start_ptr;
}
start_ptr=temp;
//adding at somewhere
int loc;
// adding at the end
cout<<"enter the loc";
if(start_ptr==NULL)
cin>>loc;
{
node *temp2;
temp=new node;
temp= start_ptr;
cout << "Please enter the name of the
person: "; for(int i=0;i<loc;i++)
}
} // adding at the end
if(start_ptr==NULL)
int age ; cout << "Please enter the age of the person : ";
}; start_ptr=temp;
void main() }
{ else
temp=temp->nxt;
temp->nxt = NULL; cout << "Please enter the age of the person : ";
11
cin >> temp2->age; }
temp2->nxt =NULL;
temp2->prev=NULL; start_ptr->prev=NULL;
cout << "Please enter the name of the person: delete temp;
";
12
cin>>a; cout << "Name : " << temp->name << endl;
else }}
temp=start_ptr;
if(temp->nxt==NULL)
{delete temp;
start_ptr=NULL;
start_ptr->prev=NULL;
else
while(temp->nxt!=NULL)
{temp2=temp;
temp=temp->nxt;}
delete temp;
temp2->nxt=NULL;
temp2->prev=temp->prev;
temp = start_ptr;
13
14