0% found this document useful (0 votes)
7 views

Chapter 3 linked list

Chapter Three discusses linked lists and their advantages over static arrays, emphasizing dynamic memory allocation. It details the structure and implementation of singly and doubly linked lists, including operations such as adding, deleting, and displaying nodes. The chapter also provides C++ code examples for creating and manipulating linked lists.

Uploaded by

nafyjabesa1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Chapter 3 linked list

Chapter Three discusses linked lists and their advantages over static arrays, emphasizing dynamic memory allocation. It details the structure and implementation of singly and doubly linked lists, including operations such as adding, deleting, and displaying nodes. The chapter also provides C++ code examples for creating and manipulating linked lists.

Uploaded by

nafyjabesa1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter Three: Linked Lists

3.1 Review of Dynamic memory allocation and de-allocation

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

Dynamic memory allocation:-the memory is allocated dynamically to the entities of the


program. The program has freedom to allocate and free the memory for the program entities.
E.g. pointers

De-allocation (destroy operation-destructor):- destroys the data structure or de allocates the


memory which have been created earlier in the program.

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())

Arrays: is a very useful data structure but it has two limitations:

1. Its size has to be known at compilation time

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:

 overcomes limitations of the array

 A collection of nodes storing data and links to the other nodes.

 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.

 Mostly implemented using pointers


1
Pointers: any variable its content is the location (hold) of another variable (address)

Like a road sign that lead as a certain location

Structure:- holds d/t data type together

3.2. Singly Linked Lists

Linked lists are the most basic self-referential structures.

Linked lists allow you to have a chain of structs with related data.

A linked list is made up of a chain of nodes. Each node contains:

• the data item, and

• a pointer to the next node

Creating Linked Lists in C++


A linked list is a data structure that is built from structures and pointers. It forms a chain of
"nodes" with pointers representing the links of the chain and holding the entire thing together. A
linked list can be represented by a diagram like this one:

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. adding at the end


struct node

{ // adding at the end


char name[20]; // Name of up to 20 if(start_ptr==NULL)
letters
{
int age ;
temp=new node;
node *next;// Pointer to next node
cout << "Please enter the name of the
}; person: ";
struct node *start_ptr = NULL; cin >> temp->name;
node *temp; cout << "Please enter the age of the
person : ";
temp = new node;
cin >> temp->age;
cout << "Please enter the name of the
person: "; temp->next= NULL;
cin >> temp->name; }
cout << "Please enter the age of the else
person : ";
{ node *temp2;
cin >> temp->age;
temp= start_ptr;
temp->next = NULL;
while(temp->nxt!= NULL)
if (start_ptr == NULL)
temp=temp->next;
start_ptr = temp;
temp2=new node;
2. adding at the beginning
cout << "Please enter the name of the
person: ";
temp=new node;
cin >> temp2->name;
cout << "Please enter the name of the
person: "; cout << "Please enter the age of the
person : ";
cin >> temp->name;
cin >> temp2->age;
cout << "Please enter the age of the
person : "; temp2->nxt =NULL;
cin >> temp->age; temp->nxt=temp2;
temp->nxt= start_ptr; }

3
4. adding somewhere in the list 6. deleting from the end

//adding at somewhere if(start_ptr==NULL)

int loc; cout<<"the list is empty";

cout<<"enter the loc"; else

cin>>loc; {

node *temp2; temp=start_ptr;

temp= start_ptr; if(temp->nxt==NULL)

for(int i=0;i<loc;i++) {delete temp;

{ start_ptr=NULL;}

temp=temp->nxt; else

if(temp==NULL) {

cout<<"there are less than the element u while(temp->nxt!=NULL)


specified";
{temp2=temp;
}
temp=temp->nxt;}
temp2=new node;
delete temp;
cout << "Please enter the name of the
person: "; temp2->nxt=NULL;

cin >> temp2->name; }

cout << "Please enter the age of the }


person : ";
7. display
cin >> temp2->age;
temp = start_ptr;
temp2->nxt =temp->nxt;
while (temp != NULL)
temp->nxt =temp2;
{ // Display details for what temp points
5. deleting from beginning to

//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;

3.3 Doubly Linked Lists

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)

//create dll insert data items

struct node temp->nxt= NULL;

{ start_ptr=temp;

char name[20]; // Name of up to }


20 letters
else
int age ;
{ node *temp2;
node *nxt;// Pointer to next
node temp= start_ptr;

node *prev; while(temp->nxt!= NULL)

}; temp=temp->nxt;

void main()

{ temp2=new node;

struct node *start_ptr = NULL; insert data items

node *temp; temp2->nxt =NULL;

temp = new node; temp2->prev=temp;

insert data items temp->nxt=temp2;

temp->nxt = NULL; }

temp->prev = NULL; //add at the begining

if (start_ptr == NULL) while (temp->prv != NULL)


temp = temp->prv;
start_ptr = temp;

// adding at the end temp2=new node;


if(start_ptr==NULL) temp2->prev=NULL;
{ insert data items
temp=new node; temp2->nxt= temp
temp->prev=NULL; temp->prev=temp2;
6
{delete temp;

//display start_ptr=NULL;

//delete from start node start_ptr->prev=NULL;

temp=start_ptr; }

start_ptr=start_ptr->nxt ; else

start_ptr->prev=NULL; {

delete temp; while(temp->nxt!=NULL)

//delete from the end {temp2=temp;

node *temp2; temp=temp->nxt;}

if(start_ptr==NULL) delete temp;

cout<<"the list is empty"; temp2->nxt=NULL;

else temp2->prev=temp->prev;

{ }

temp=start_ptr; }

if(temp->nxt==NULL)

3.4. Implementation of Linked Lists

Singly linked lists

#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++)

cin >> temp->name; {

cout << "Please enter the age of the person : temp=temp->nxt;


";
if(temp==NULL)
cin >> temp->age;
8
cout<<"there are less than the element u }
specified";
char a[2];
}
cout<<"are you sure to delete?(Y/N)";
temp2=new node;
cin>>a;
cout << "Please enter the name of the
person: "; //delete from start node

cin >> temp2->name; temp=start_ptr;

cout << "Please enter the age of the person : start_ptr=start_ptr->nxt ;


";
delete temp;
cin >> temp2->age;
//display after delete
temp2->nxt =temp->nxt;
temp = start_ptr;
temp->nxt =temp2;
while (temp != NULL)
//count the number of nodes
{ // Display details for what temp points to
int c=0;
cout << "Name : " << temp->name << endl;
temp=start_ptr;
cout << "Age : " << temp->age << endl;
while(temp!=NULL)
cout << endl; // Blank line
{temp=temp->nxt;
// Move to next node (if present)
c++;}
temp = temp->nxt;
cout<<"there are:"<<c<<"-nodes"<<endl;
}
//display
//.......................
temp = start_ptr;
//delete specified node
while (temp != NULL)
int key;
{ // Display details for what temp points to
cout<<"which element u want to delet?";
cout << "Name : " << temp->name << endl;
cin>>key;
cout << "Age : " << temp->age << endl;
temp=start_ptr;
cout << endl; // Blank line
while(temp!=NULL)
// Move to next node (if present)
{
temp = temp->nxt;
if(temp->age==key)
9
{ cout<<"are you sure to delete?(Y/N)";
if(temp==start_ptr) cin>>a;
start_ptr=temp->nxt; //delete
//node *temp2;
else if(start_ptr==NULL)
temp2->nxt=temp->nxt; cout<<"the list is empty";
else
delete temp; {
} temp=start_ptr;
else if(temp->nxt==NULL)
{temp2=temp; {delete temp;
temp=temp->nxt;} start_ptr=NULL;}
temp = temp->nxt; else
} {
cout<<"the element is not found"; while(temp->nxt!=NULL)
//display after delete {temp2=temp;
temp = start_ptr; temp=temp->nxt;}
while (temp != NULL) delete temp;
{ // Display details for what temp points to temp2->nxt=NULL;
cout << "Name : " << temp->name << endl; }
cout << "Age : " << temp->age << endl; }
cout << endl; // Blank line //display after delete
// Move to next node (if present) temp = start_ptr;
temp = temp->nxt; while (temp != NULL)
} { // Display details for what temp points to
//.................... cout << "Name : " << temp->name << endl;
//delete from the end cout << "Age : " << temp->age << endl;
10
cout << endl; // Blank line temp->prev = NULL;

// Move to next node (if present) if (start_ptr == NULL)

temp = temp->nxt; start_ptr = temp;

}
} // adding at the end

if(start_ptr==NULL)

Doubly linked list {

#include<iostream.h> temp=new node;

struct node temp->prev=NULL;

{ cout << "Please enter the name of the person:


";
char name[20]; // Name of up to 20
letters cin >> temp->name;

int age ; cout << "Please enter the age of the person : ";

node *nxt;// Pointer to next node cin >> temp->age;

node *prev; temp->nxt= NULL;

}; start_ptr=temp;

void main() }

{ else

struct node *start_ptr = NULL; { node *temp2;

node *temp; temp= start_ptr;

temp = new node; while(temp->nxt!= NULL)

temp=temp->nxt;

cout << "Please enter the name of the person:


";
temp2=new node;
cin >> temp->name;
cout << "Please enter the name of the person:
cout << "Please enter the age of the person : "; ";

cin >> temp->age; cin >> temp2->name;

temp->nxt = NULL; cout << "Please enter the age of the person : ";
11
cin >> temp2->age; }

temp2->nxt =NULL;

temp2->prev=temp; //delete from start node

temp->nxt=temp2; char a[2];

} cout<<"are you sure to delete?(Y/N)";

//add at the begining cin>>a;

while (temp->prv != NULL)


temp = temp->prv;
temp=start_ptr;

temp2=new node; start_ptr=start_ptr->nxt ;

temp2->prev=NULL; start_ptr->prev=NULL;

cout << "Please enter the name of the person: delete temp;
";

cin >> temp2->name;

cout << "Please enter the age of the person : ";


//display after delete
cin >> temp2->age;
temp = start_ptr;
temp2->nxt= temp;
while (temp != NULL)
temp->prev=temp2;
{ // Display details for what temp points to

cout << "Name : " << temp->name << endl;


//display
cout << "Age : " << temp->age << endl;
temp = start_ptr;
cout << endl; // Blank line
while(temp != NULL)
// Move to next node (if present)
{ // Display details for what temp points to
temp = temp->nxt;
cout << "Name : " << temp->name << endl;
}
cout << "Age : " << temp->age << endl;

cout << endl; // Blank line


//....................
// Move to next node (if present)
//delete from the end
temp = temp->nxt;
cout<<"are you sure to delete?(Y/N)";

12
cin>>a; cout << "Name : " << temp->name << endl;

//delete cout << "Age : " << temp->age << endl;

node *temp2; cout << endl; // Blank line

if(start_ptr==NULL) // Move to next node (if present)

cout<<"the list is empty"; temp = temp->nxt;

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;

//display after delete

temp = start_ptr;

while (temp != NULL)

{ // Display details for what temp points to

13
14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy