A round of circular linked list with tail pointers for postgraduate entrance examination to achieve test results (Wang Zhuo version)

This blog post is derived from the data structure of Mr. Wang Zhuo. The circular linked list with tail pointers in the book is merged. Here, it not only includes the implementation of the basic content of the circular linked list with tail pointers, but also the examples of linked list merging. The readers who like it can collect it , keep going.

1. Tail pointer circular linked list storage structure without head node

2. Tail interpolation method to create data singly linked list

The core point here is that there is no leading node, that is, the first node inserts data, and the second is the tail insertion method.

/*
 * 输入:空的循环单链表rear,数据A[],数据个数n
 * 输出:新创建循环单链表rear,表中各元素次序相同
 * */
void CreateCListR(CircListRear &rear,ElementType A[],int n){
    
    
    CircleNode *s;
    rear = new CircleNode;
    rear->next = rear;
    rear->data = A[0];
    for(int i =1;i<n;i++){
    
    
        s = new CircleNode;
        s->data = A[i];
        s->next = rear->next;
        rear->next = s;
        rear = s;
    }
}

3. Print a single circular linked list

It is no different from ordinary singly linked list printing, mainly p->next !=rear, secondly, the tail pointer stores data, and finally prints the tail pointer data

/*
 * 输入: 循环单链表尾指针 rear
 * 输出: 从首元素开始顺序输出中所有结点
 * */
void PrintCListR(CircListRear &rear){
    
    
    if(rear == NULL) return ;
    for(CircleNode* p = rear->next;p!=rear;p=p->next ){
    
    
        cout << p->data << " ";

    }
    cout << rear->data << endl;
}

4. Insert circular singly linked list

Insert the element before the head node, that is, after the tail pointer. It is divided into empty and non-empty. If it is empty, the inserted node is the first element node, and if it is not empty, it can be inserted directly.

/*功能:将包含给定值x的新结点插入尾指针为rear的不带头结点的循环单链表的首元结点前
 * 输入: 循环单链表表尾指针rear,插入值x
 * 输出: 插入后的循环单链表rear
 * */
void InsertCH(CircListRear &rear,ElementType x){
    
    
    CircleNode * s= new CircleNode;
    s->data = x;
    if(rear == NULL) {
    
    
        rear = s;
        rear->next = s;
    }else{
    
    
        s->next = rear->next;
        rear->next = s;
    }
}

5. Merge

Merge directly with the code in the book

int MergeL(CircListRear &A,CircListRear &B){
    
    
    if(!A && !B) return ERROR;
    CircListRear  p = B->next;
    B->next = A->next;
    A->next = p;
    return OK;
}

Test effect

insert image description here

Complete source code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 10
typedef int ElementType;
typedef int Status;
using namespace std;
typedef struct CircleNode{
    
    
    ElementType data; //结点数据
    struct CircleNode* next; //后继点指针
}CircleNode,*CircList,*CircListRear;
//带头指针和头结点的循环单链表的基本运算

/*
 * 输入:空的循环单链表rear,数据A[],数据个数n
 * 输出:新创建循环单链表rear,表中各元素次序相同
 * */
void CreateCListR(CircListRear &rear,ElementType A[],int n){
    
    
    CircleNode *s;
    rear = new CircleNode;
    rear->next = rear;
    rear->data = A[0];
    for(int i =1;i<n;i++){
    
    
        s = new CircleNode;
        s->data = A[i];
        s->next = rear->next;
        rear->next = s;
        rear = s;
    }
}
/*
 * 输入: 循环单链表尾指针 rear
 * 输出: 从首元素开始顺序输出中所有结点
 * */
void PrintCListR(CircListRear &rear){
    
    
    if(rear == NULL) return ;

    for(CircleNode* p = rear->next;p!=rear;p=p->next ){
    
    
        cout << p->data << " ";

    }
    cout << rear->data << endl;

}

/*功能:将包含给定值x的新结点插入尾指针为rear的不带头结点的循环单链表的首元结点前
 * 输入: 循环单链表表尾指针rear,插入值x
 * 输出: 插入后的循环单链表rear
 * */
void InsertCH(CircListRear &rear,ElementType x){
    
    
    CircleNode * s= new CircleNode;
    s->data = x;
    if(rear == NULL) {
    
    
        rear = s;
        rear->next = s;
    }else{
    
    
        s->next = rear->next;
        rear->next = s;
    }
}
int MergeL(CircListRear &A,CircListRear &B){
    
    
    if(!A && !B) return ERROR;
    CircListRear  p = B->next;
    B->next = A->next;
    A->next = p;
    return OK;
}
int main(){
    
    
    CircListRear L;//创建不带头结点仅含尾指针的单链表
    int A[5]={
    
    1,2,3,4,5};//插入数据
    CreateCListR(L,A,5);
    cout << "created L:" << endl;
    PrintCListR(L);//打印单循环单链表
    InsertCH(L,18);//插入头结点元素
    cout << "iniserted 18 L:" << endl;
    PrintCListR(L);//打印
    //演示合并循环链表,书中内容
    CircListRear L2;
    int B[5] ={
    
    7,8,9,10,11};
    CreateCListR(L2,B,5);
    MergeL(L,L2);
    cout << "Merged L:" << endl;
    PrintCListR(L);

    return 0;
}

Guess you like

Origin blog.csdn.net/m0_37149062/article/details/123590007