Code tutorial for implementing single-chain table in C language and single-chain code tutorial

Source: Internet
Author: User

Code tutorial for implementing single-chain table in C language and single-chain code tutorial

LinkList. h // function declaration, header file, etc.

#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include
#include
#include
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node * next;

} Node;
void InitLinkList (Node ** pphead); // Initialize the linked list
Node * BuyNode (DataType x); // Create a node
void PrintList (Node * phead); // Print the linked list does not change the node pointer pointer, so you can pass a pointer
void PushBack (Node ** pphead, DataType x); // Insert a node at the end
void PopBack (Node ** phead); // delete a node at the end
void PushFront (Node ** pphead, DataType x); // plug a node into the head
void PopFront (Node ** phead); // delete a node in the header
Node * Find (Node * pphead, DataType x); // Find a node Do not change the pointer of the node, so pass a pointer
void Erase (Node ** pphead, Node * pos); // Delete the node at the specified position
void Insert (Node ** pphead, Node * pos, DataType x); // Insert a node before the specified position
#endif

 

LinkList. c // The implementation of each function

#include "LinkList.h"
void InitLinkList (Node ** pphead)
{
assert (pphead);
* pphead = NULL;
}
Node * BuyNode (DataType x)
{// Open up space
Node * node = (Node *) malloc (sizeof (Node));
// Assign
node-> next = NULL;
node-> data = x;
return node;
}
void PrintList (Node * phead)
{// 1. Empty
if (phead == NULL)
{
printf ("NULL \ n");
return;
}
// 2. Not empty
while (phead)
{
printf ("% d->", phead-> data);
phead = phead-> next;
}
printf ("NULL \ n");
}
void PushBack (Node ** pphead, DataType x)
{
assert (pphead); // Check parameters
Node * tail = * pphead;
// 1. Empty
if (* pphead == NULL)
{
* pphead = BuyNode (x);
}
// 2. Not empty
else
{
// find the tail
while (tail-> next)
{
tail = tail-> next;
}
// insert
tail-> next = BuyNode (x);

}

}
void PopBack (Node ** pphead)
{
// Check parameters
assert (pphead);
// 1. Empty
if (* pphead == NULL)
{
return;
}
// 2. There is a node
else if ((* pphead)-> next == NULL)
{
free (* pphead);
* pphead = NULL;
}
// 3. Two or more nodes
else
{
// find the previous node
Node * prev = NULL; // Used to find the previous node of the last node cannot directly release the tail, otherwise the pointer to the tail node is a wild pointer, so you must find the pointer to the tail and set it to null
Node * tmp = * pphead; // find the last node
while (tmp-> next)
{
prev = tmp;
tmp = tmp-> next;
}
prev-> next = NULL;
free (tmp); // Remember to release it, because it is malloc, that is, dynamically developed. Otherwise memory leak
tmp = NULL;

}

}
void PushFront (Node ** pphead, DataType x)
{
assert (pphead); // Check parameters
// 1. The list is empty
if (* pphead == NULL)
{
* pphead = BuyNode (x);
}
// 2. The list is not empty
else
{
Node * tmp = BuyNode (x);
tmp-> next = * pphead;
* pphead = tmp;
}
}
void PopFront (Node ** pphead)
{
assert (pphead); // Check parameters
// 1. Empty
if (* pphead == NULL)
{
return;
}
// 2. A node
else if ((* pphead)-> next == NULL)
{
free (* pphead);
* pphead = NULL;
}
// 3. Two or more nodes
else
{
Node * next = (* pphead)-> next; // The second node
free (* pphead);
* pphead = next;
}
}
Node * Find (Node * phead, DataType x)
{
assert (phead); // Check parameters
Node * cur = phead;
while (cur)
{
if (cur-> data == x)
{
return cur;
}
cur = cur-> next;
}
return NULL; // not found
}
void Insert (Node ** pphead, Node * pos, DataType x)
{
assert (pphead); // Check parameters
//1.pos is the head node
if (pos == * pphead)
{
PushFront (pphead, x);
}
// 2. Non-head node
else
{
Node * prev = * pphead; // Find the previous node of pos
Node * tmp = BuyNode (x);
while (prev-> next! = pos)
{
prev = prev-> next;
}
prev-> next = tmp;
tmp-> next = pos;
}
}
void Erase (Node ** pphead, Node * pos)
{
assert (pphead); // Check parameters
//1.pos is the head node
if (* pphead == pos)
{
PopFront (pphead);
}
//2.pos is the tail node
else if (pos-> next == NULL)
{
PopBack (pphead);
}
// 3. Non-head and tail nodes
else
{
Node * prev = * pphead;
Node * next = pos-> next;
if (prev-> next! = pos)
{
prev = prev-> next;
}
prev-> next = next;
free (pos);
pos = NULL;
}
}

Test. c // test

#include "LinkList.h"
void test1 ()
{// Test PopBack, PushBack
Node * list = NULL;
PrintList (list);
PushBack (& list, 4);
PushBack (& list, 3);
PushBack (& list, 2);
PushBack (& list, 1);
PopBack (& list);
PopBack (& list);
PrintList (list);
PopBack (& list);
PopBack (& list);
PrintList (list);
Ranch
}
void test2 ()
{// Test PopFront, PushFront
Node * list = NULL;
PushFront (& list, 1);
PushFront (& list, 2);
PushFront (& list, 3);
PushFront (& list, 4);
PrintList (list);
PopFront (& list);
PrintList (list);
PopFront (& list);
PopFront (& list);
PopFront (& list);
PrintList (list);

Ranch
}
void test3 ()
{// Test Find Insert Erase
Node * list = NULL;
PushFront (& list, 1);
PushFront (& list, 2);
PushFront (& list, 3);
PushFront (& list, 4);
PrintList (list);
Node * pos = Find (list, 4);
Insert (& list, pos, 20);
PrintList (list);
Insert (& list, pos-> next, 30);
PrintList (list);
Erase (& list, pos);
PrintList (list);


}
int main ()
{
// test1 ();
// test2 ();
test3 ();
system ("pause");
return 0;
} 


Related Article

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.