当前位置:网站首页>Linear list --- circular linked list
Linear list --- circular linked list
2022-07-07 02:27:00 【Programming a small program】
Circular linked list : The characteristic of circular linked list is that the address of the head node can be found through the pointing of the tail node ;

The structure design is the same as that of the single linked list :
typedef int ELEM_TYPE;
typedef struct Clist
{
ELEM_TYPE data; // Data fields
struct Clist* next; // Pointer to the domain
} Clist, * PClist;
The main functions are as follows :
1. Initialization operation
In the initialization operation of the circular list, the first node's next The domain points to its own address
void Init_clist(struct Clist* plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return;
}
plist->next = plist;
}2. Tail interpolation function
First apply for a new node pnewnode, And then take it. next The domain points to the address of the first node , Then apply for a temporary node p Point it to the address of the tail node , And then let p Of next Domain points to pnewnode The address of .
bool Insert_tail(PClist plist, ELEMTYPE val)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
//1. Buy new nodes
struct Clist *pnewnode = (struct Clist*)malloc(1 * sizeof(struct Clist));
assert(pnewnode != NULL);
pnewnode->data = val;// New nodes purchased Finished processing
//2. Find the insertion location ( Through... With precursors for loop )
struct Clist *p = plist;
for(p; p->next!=plist; p=p->next);
// here for Loop execution ends p Point to the tail node
//3. Insert
pnewnode->next = p->next;
p->next = pnewnode;
return true;
}Circular linked list complete code
clist.h
// Circular linked list structure design
typedef int ELEMTYPE;
typedef struct Clist
{
ELEMTYPE data; // Data fields Storing data
struct Clist* next;// Pointer to the domain The address of the next node ( At the end next Save the address of the header node )
}Clist, *PClist;
// Circular linked list owned Executable function declaration :
// initialization
void InitClist(struct Clist* plist);
// Head insertion
bool Insert_head(PClist plist, ELEMTYPE val);
// Tail insertion
bool Insert_tail(PClist plist, ELEMTYPE val);
// Insert by position
bool Insert_pos(PClist plist, int pos, ELEMTYPE val);
// Head deletion
bool Del_head(PClist plist);
// Deletion at the end
bool Del_tail(PClist plist);
// Delete by location
bool Del_pos(PClist plist, int pos);
// Delete... By value
bool Del_val(PClist plist, ELEMTYPE val);
// lookup ( If I find , You need to return the address of the found node )
struct Clist* Search(struct Clist *plist, ELEMTYPE val);
// Sentenced to empty
bool IsEmpty(PClist plist);
// Full sentence ( Circular linked list does not need this operation )
// To obtain the length of the
int Get_length(PClist plist);
// Empty
void Clear(PClist plist);
// The destruction 1
void Destroy1(PClist plist);
// The destruction 2
void Destroy2(PClist plist);
// Print
void Show(struct Clist *plist);clist.cpp
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include "clist.h"
// There are few in the circular linked list NULL, nullptr
// initialization
void InitClist(struct Clist* plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return;
}
//plist->data; // Data fields do not handle
plist->next = plist;
}
// Head insertion
bool Insert_head(PClist plist, ELEMTYPE val)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
//1. Buy new nodes
struct Clist *pnewnode = (struct Clist*)malloc(1 * sizeof(struct Clist));
assert(pnewnode != NULL);
pnewnode->data = val;// New nodes purchased Finished processing
//2. Find the insertion location ( Head insertion Don't look for )
//3. Insert
pnewnode->next = plist->next;
plist->next = pnewnode;
return true;
}
// Tail insertion
bool Insert_tail(PClist plist, ELEMTYPE val)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
//1. Buy new nodes
struct Clist *pnewnode = (struct Clist*)malloc(1 * sizeof(struct Clist));
assert(pnewnode != NULL);
pnewnode->data = val;// New nodes purchased Finished processing
//2. Find the insertion location ( Through... With precursors for loop )
struct Clist *p = plist;
for(p; p->next!=plist; p=p->next);
// here for Loop execution ends p Point to the tail node
//3. Insert
pnewnode->next = p->next;
p->next = pnewnode;
return true;
}
// Insert by position
bool Insert_pos(PClist plist, int pos, ELEMTYPE val)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
assert(pos>=0 && pos<=Get_length(plist));
//1. Buy new nodes
struct Clist *pnewnode = (struct Clist*)malloc(1 * sizeof(struct Clist));
assert(pnewnode != NULL);
pnewnode->data = val;// New nodes purchased Finished processing
//2. Find the insertion location ( Through... With precursors for loop )
struct Clist *p = plist;
for(int i=0; i<pos; i++)
{
p = p->next;
}
// here for Loop execution ends p Point to the appropriate location to be inserted
//3. Insert
pnewnode->next = p->next;
p->next = pnewnode;
return true;
}
// Head deletion
bool Del_head(PClist plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
if(IsEmpty(plist))// Not empty Then there is at least one valid value
{
return false;
}
//1. The pointer p Point to the node to be deleted
struct Clist *p = plist->next;
//2. The pointer q Point to the node before the node to be deleted
//q Namely Head node There will be no additional processing here
//3. Crossing direction
plist->next = p->next;
free(p);
return true;
}
// Deletion at the end
bool Del_tail(PClist plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
if(IsEmpty(plist))// Not empty Then there is at least one valid value
{
return false;
}
//1. The pointer p Point to the node to be deleted ( If the tail is deleted , This points to the tail node )
struct Clist *p = plist;
for(p; p->next!=plist; p=p->next);
// here for Point to the end Represents the p Point to the tail node
//2. The pointer q Point to the penultimate node
struct Clist *q = plist;
for(q; q->next!=p; q=q->next);
// here for Point to the end Represents the q Point to p The previous node of
//3. Crossing direction
q->next = p->next;
free(p);
return true;
}
// Delete by location
bool Del_pos(PClist plist, int pos)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
assert(pos>=0 && pos<Get_length(plist));
if(IsEmpty(plist))
{
return false;
}
//1. The pointer p Point to the node to be deleted
//2. The pointer q Point to the previous node of the node to be deleted
// Here we use the second scheme to find pq, Look for the q Look again p
struct Clist *q = plist;
for(int i=0; i<pos; i++)
{
q = q->next;
}
struct Clist *p = q->next;
//3. Crossing direction
q->next = p->next;
free(p);
return true;
}
// Delete... By value
bool Del_val(PClist plist, ELEMTYPE val)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
struct Clist* p = Search(plist, val);
if(p == NULL)
{
return false;
}
struct Clist *q = plist;
for(q; q->next!=p; q=q->next);
q->next = p->next;
free(p);
return true;
}
// lookup ( If I find , You need to return the address of the found node )
struct Clist* Search(struct Clist *plist, ELEMTYPE val)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
for(struct Clist *p=plist->next; p!=plist; p=p->next)
{
if(p->data == val)
{
return p;
}
}
return NULL;
}
// Sentenced to empty
bool IsEmpty(PClist plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
return plist->next == plist;
}
// Full sentence ( Circular linked list does not need this operation )
// To obtain the length of the
/* The pointer p Run backward from the next node of the head node , If p Encountered the header node again ,
prove p I walked around and came back , This is a valid node. The traversal must have ended */
int Get_length(PClist plist)
{
// Without precursors for loop Just run once
int count = 0;
for(struct Clist *p=plist->next; p!=plist; p=p->next)
{
count++;
}
return count;
}
// Empty
void Clear(PClist plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
Destroy1(plist);
}
// The destruction 1( Infinite header deletion )
void Destroy1(PClist plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
while(plist->next != plist)
{
struct Clist *p = plist->next;
plist->next = p->next;
free(p);
}
plist->next = plist;
}
// The destruction 2( You need two pointer variables )
void Destroy2(PClist plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
struct Clist *p = plist->next;
struct Clist *q = NULL;
plist->next = plist;
while (p!=plist)
{
q = p->next;
free(p);
p = q;
}
}
// Print
void Show(struct Clist *plist)
{
assert(plist != NULL);
if (plist == NULL)
{
return false;
}
for(struct Clist *p=plist->next; p!=plist; p=p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
边栏推荐
- leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
- FLIR blackfly s usb3 industrial camera: white balance setting method
- Lumion 11.0 software installation package download and installation tutorial
- leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
- How to build a 32core raspberry pie cluster from 0 to 1
- postgresql 之 数据目录内部结构 简介
- 强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
- postgresql之整体查询大致过程
- postgresql之integerset
- Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
猜你喜欢

#夏日挑战赛#数据库学霸笔记(下)~

postgresql之整體查詢大致過程
![[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files](/img/20/f7fc2204ca165dcea4af25cb054e9b.png)
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files

解密函数计算异步任务能力之「任务的状态及生命周期管理」

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

fiddler的使用

Several classes and functions that must be clarified when using Ceres to slam

The boss is quarantined

Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!

3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
随机推荐
TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
Draco - gltf model compression tool
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
解密函数计算异步任务能力之「任务的状态及生命周期管理」
Data connection mode in low code platform (Part 1)
Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
投资的再思考
[leetcode] day97 remove linked list elements
argo workflows源码解析
[C # notes] use file stream to copy files
[server data recovery] data recovery case of a Dell server crash caused by raid damage
纽约大学 CITIES 研究中心招聘理学硕士和博士后
传感器:土壤湿度传感器(XH-M214)介绍及stm32驱动代码
postgresql之integerset
SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!
Web开发小妙招:巧用ThreadLocal规避层层传值
压缩 js 代码就用 terser
【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例
3D激光SLAM:Livox激光雷达硬件时间同步