0% found this document useful (0 votes)
15 views24 pages

Lecture#7

The document outlines the implementation of a singly linked list in C++, detailing the structure of nodes and the various operations that can be performed on the list, such as adding, inserting, deleting, and displaying nodes. It also discusses applications of linked lists, including dynamic memory management, representation of graphs, and polynomial operations. Additionally, an assignment is provided to write a program for adding two polynomials using linked lists.

Uploaded by

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

Lecture#7

The document outlines the implementation of a singly linked list in C++, detailing the structure of nodes and the various operations that can be performed on the list, such as adding, inserting, deleting, and displaying nodes. It also discusses applications of linked lists, including dynamic memory management, representation of graphs, and polynomial operations. Additionally, an assignment is provided to write a program for adding two polynomials using linked lists.

Uploaded by

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

SINGLY LINKED LIST:

implementation of a node
struct node
{
5 0x80010
int data;
node *next;
};
Creation of Linked List
class linked_list
{
private:
node *start; node * last;
public:
linked_list()
{ start=NULL; last= NULL;}

void add_node(int value);


void insert_first(int value) ;
void insert_at_position(int pos,int val);
void delete_first();
void delete_last();
void delete_at_position(int pos);
void display();
};
Linked List:add nodes forward
void linked_list:: add_node(intvalue)
{
node *temp=new node;
temp->data=value;
temp->next=NULL;
if (start == NULL)
{ 12 NULL
start=temp;
last=temp; start last
}
else
{ 12 NULL
last->next = temp;

last = temp; last


start
temp
}
} 15 12 NULL
Linked List:insert_first
void linked_list::insert_first(int value)
{
node *temp=new node;
12 NULL
temp->data=value;
temp->next=NULL; start last

if (start == NULL)
{ 12 NULL
start=temp;
last=temp;
} last
start
else{ temp
temp->next=start;
12 NULL 15
start=temp; }
}
insert a node in between a linked
list
 The steps for inserting a node between two nodes a and b:
1. Make a new node
node *tmp=new node
2. Point the ‘next’ of the new node to the node ‘b’ (the node
after which we have to insert the new node). Till now, two
nodes are pointing the same node ‘b’, the node ‘a’ and the
new node.
temp->next=b

3. Point the ‘next’ of ‘a’ to the new node.


a->next=temp
Linked List; insert_position
void linked_list::insert_at_position(int pos, int value)
{node *temp=new node; temp last
temp->data=value;
start current 30
temp->next=NULL;
node *current=start;
10 20 40 NULL
for(int i=1;i<pos-1;i++)
{
current=current->next;
if(current==NULL)
{cout<<" the list has element lessthan
"<<pos<< "element ";return;}
}
temp->next=current->next;
current->next=temp;
}
Linked List:display

void linked_list:: display()


{
if(start==NULL) {cout<<"list is empty ";return;}
node *temp=start;

cout<<" the elements of thelist are "<<endl;

while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}

}
Delete last element in linked list
 Check if the list isempty
if(start==NULL)
 If it not empty check if it has one element only
if (start->next==NULL)
 If it has more than onelement
Delete first element in linked list
 Copy the address of first node i.e. start node to some temp
variable say temp start
node *temp= start; 10 20 30 NULL

temp

 Move the start to the second node of the linked list i.e.
start
start=start->next;
10 20 30 NULL
temp
 Disconnect the connection of firstnode to second node.
start
10 20 30 NULL

temp

 Free the memory occupied by thefsitrasrttnode.


delete temp
20 30 NULL
Linked List :delete_first
void linked_list::delete_first()
{//list is empty
if(start==NULL){ cout<<"the list isempty"
; return;} start last
//only one node
if (start->next==NULL) 12 NULL
{ delete start; delete last; return;}
//more than one node
start temp last
node *temp= start;
start=start->next; 10 20 30 NULL
delete temp;
}
Delete last node from linked list
 Traverse to the last node of the linked list keeping
track of the second last node in some temp variable say
current .
 disconnect the second last node with the last node
i.e. current->next = NULL.
 Free the memory occupied by the last node.
Linked List: delete_last
void linked_list::delete_last()
{// list is empty
if(start==NULL) { cout<<"the list is empty" ; return;}
//only one node
if (start->next==NULL)
{ delete start; delete last;return;}

//more than one node


node *current=start;
node *temp=last;
while(current->next!=last)
{ current=current->next; }
current->next=NULL;
last=current;
delete temp;
}
Delete element at specific position
 Traverse to the nth node of the singly linked list and
also keep reference of n-1th node in some tempvariable
pre current
say pre.
10 20 30 40 NULL

n-1th nth n+1th


 Reconnect the n-1th node with the n+1th node
i.e. pre->next=current->next;
(Where pre is n-1th node and current nodeis
the nth node and current->next is the n+1th node).
pre current
10 20 30 40 NULL

n-1th nth n+1th


 free the memory occupied by the nth node i.e.current
node.
Linked List: delete_ position
void linked_list::delete_at_position(int pos)
{
node *current=start;
node *pre=start;
for(int i=1;i<pos;i++)
{ pre=current; current=current->next;
if(current==NULL) {cout<<" the list has element lessthan"
<<pos<< “element “; return;}
}
pre->next=current->next;
delete current;
}
Main function
int main()
{
int value; linked_list list;
cin>>value;
list.add_node(value);
list.add_node(10);
list.insert_first(4);
list.insert_first(5);
list.insert_first(6);
list.display();
list.insert_at_position(3,-1);
list.display();
list.delete_at_position(3);
list.display();

return 0;
}
linked list applications
 Dynamic Memory Management. In allocationand
releasing memory at runtime

 Use linked list as a stack by adding and removing


elements from the beginning of thelist.

 Use linked list as a queue by adding in the beginning,


removing from the end
linked list applications
 A great way to represent a deck of cards in a game

 Graphs? Adjacency list representation of graph.

 In hash table , each bucket of the table can itself be a


linked list (chaining)

 Use linked list for addition /subtraction /multiplication of


two polynomials. Eg:
p1=2x2 +3x+7 and p2=3x3 +5x+2
p1+p2=3x3 +2x2 +8x+9
Polynomials using Linked List
p1=2x2 +3x+7
Coefficient power

p1 2 2 3 1 7 0 NULL

p2=5x3 +5x+2

p2 5 3 5 1 2 0 NULL

P= p1+p2=5x3 +2x2 +8x+9

P 5 3 2 2 8 1 9 0 NULL
Polynomials using Linked List: node
structure
node Address of
Coefficient power next node

struct node
{
int coeff;
int pow;
node *next;

}
Assignment 1
 write a program to add two polynomials. usinglinked
list as representation of polynomials
 Deadline 2-11-2019

You might also like