---linked list operations for C + + knowledge points

Source: Internet
Author: User

1. Single Linked list
Node type node definition for single linked list:
typedef struct LINKNODE
{
int data;
struct Linknode *node;
};

<1> Create a single-linked list
void Createsinglelink ()
{
Node *head, *p, *s;
int tempvalue;
BOOL bCycle = True;//bcycle loop control variable

Head = (node *) malloc (sizeof (node));//Establish head node
p = head;

while (bCycle)
{
scanf ("%d", &tempvalue);
if (0! = tempvalue)//end with 0 sign
{
s = (node *) malloc (sizeof (node)); Establish the next node.
S->data = Tempvalue;
P->next = s; Connect the new node to the list created in the previous link.
p = s;
}
Else
{
BCycle = false;
}

}

P->next = NULL; If it is a circular linked list, then change to P->next = head;
p = head;
Head = head->next;
Free (P);//Delete head node
}

<2> single-linked list lookup node
BOOL FindNode (node *head, int ivalue)
{
bool bfind = false;
Node *p;
If ordinary linked list
p = head;
else Loop list
if (Head->data = = ivalue)
//{
Bfind = true;
return bfind;
//}
p = head->next;

while (P!=null && p->data!=ivalue)
{
p = p->next;
}

if (P! = NULL)
{
Bfind = true;
}

return bfind;
}

<3> single-linked list length
Ordinary linked list
int Lengthlink (node *head)
{
int ilength = 0;
Node *p;
p = head;

while (P! = NULL)
{
p = p->next;
ilength++;
}

return ilength;
}

Looping links
int Lengthlink (node *head)
{
int ilength = 0;
Node *p;

if (head = = null)//Empty list
{
return ilength;
}

p = head->next;
Ilength = 1;
while (P! = NULL)
{
p = p->next;
ilength++;
}

return ilength;
}

<4> single-linked list insertion node
BOOL Insertnode (node *head, int i, int ivalue)
{
Node *s, *p;
Int J;
BOOL Binsert = false;

s = (node *) malloc (sizeof (node)); Create a node to be inserted.
S->data = Ivalue;

if (i = = 0)
{
Table Header Insertion Node
S->next = head;
Head = s;
Binsert = true;
return binsert;
}

p = head;
j = 1;
while (P!=null && j<i)
{
j + +;
p = p->next;
}

if (P! = NULL)
{
Find successful, insert s into subsequent
S->next = p->next;
P->next = s;
Binsert = true;
}

return binsert;
}

<5> single-column Table delete nodes
BOOL Deletenode (node *head, int ivalue)
{
Node *p, *q;
bool bdelete = false;

if (head = = NULL)
{
The list is empty, underflow processing
return bdelete;
}

if (Head->data = = ivalue)
{
Table Header for Delete node
P =head;
Head = head->next;
Free (p);
Bdelete = true;
return bdelete;
}

Q = head;
p = head->next; Find a node with a value of ivalue starting from the second node
while (P!=null && p->data!=ivalue)
{
if (p->data! = ivalue)
{
Q = p;
p = p->next;
}
}

if (P! = NULL)
{
Find the node and make the deletion process.
Q->next = p->next;
Free (p);
Bdelete = true;
}

return bdelete;
}

---linked list operations for C + + knowledge points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.