0% found this document useful (0 votes)
43 views4 pages

Struct Node

The document defines a linked list data structure with functions for initialization, node creation, and various operations such as adding nodes to the front or back, adding a node after a specific node, and deleting nodes from the list. It includes functions to handle edge cases, such as deleting the head or the last node. Overall, it provides a basic implementation of a singly linked list in C++.

Uploaded by

LÊ XUÂN LINH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Struct Node

The document defines a linked list data structure with functions for initialization, node creation, and various operations such as adding nodes to the front or back, adding a node after a specific node, and deleting nodes from the list. It includes functions to handle edge cases, such as deleting the head or the last node. Overall, it provides a basic implementation of a singly linked list in C++.

Uploaded by

LÊ XUÂN LINH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

struct node

{
int value ;
node* next ;
};

struct list
{
node *head ;
node *tail ;
};
void initialize_list( list &a)
{
a.head = NULL;
a.tail = NULL;
}
node* CreateNode( int x)
{
node* p ;
p = new node;
p->value = x;
p->next = NULL;
return p;
}
void add_front( list &a, int b)
{
node* C;
C = CreateNode(b);
if ( a.head == NULL )
{
a.head = C ;
a.tail = C ;
}
else
{
C->next = a.head ;
a.head = C ;
}
}
void add_back( list &a, int b)
{
node* C;
C = CreateNode(b);
if ( a.head == nullptr)
{
a.head = C ;
a.tail = C ;
}
else
{
a.tail->next = C ;
C->next = NULL;
a.tail = C ;
}
}
void Add_after( list &a , node* p, node* q)
{
if( q == a.tail )
{
a.tail->next = p;
a.tail = p;
}
else
{
p->next = q->next;
q->next = p;
}
}
void delete_head ( list &l )
{
if ( l.head == NULL )
{
return ;
}
else if ( l.head == l.tail )
{
delete l.head ;
l.head = l.tail = NULL ;
}
else
{
auto p = l.head ;
l.head = l.head->next ;
delete p ;
}
}
void delete_after_p ( list &l , node* p )
{
if ( p->next == l.tail ) // Node muố n xóa nằ m đằ ng sau node p và nó là node cuối cùng của
danh sách
{
delete l.tail ;
l.tail = p ;
l.tail->next = NULL ;
}
else
{
auto q = p->next ; // q là node muốn xóa
p->next = q->next ;
delete q ;
}
}

You might also like