[数据结构PTA]线性表——循环单链表的基本运算

7-21 jmu-ds-循环单链表的基本运算 (15 分)
实现循环单链表的基本运算:初始化、插入、删除、求表的长度、判空、释放。
(1)初始化循环单链表L,输出(L->next==L)的逻辑值;
(2)依次采用尾插法插入元素:输入分两行数据,第一行是尾插法需要插入的字符数据的个数,第二行是具体插入的字符数据。
(3)输出循环单链表L;
(4)输出循环单链表L的长度;
(5)判断循环单链表L是否为空;
(6)输出循环单链表L的第3个元素;
(7)输出元素a的位置;
(8)在第4个元素位置上插入‘w’元素;
(9)输出循环单链表L;
(10)删除L的第5个元素;
(11)输出循环单链表L;
(12)释放循环单链表L。

输入格式:
两行数据,第一行是尾插法需要插入的字符数据的个数,第二行是具体插入的字符数据。

输出格式:
按照程序要求输出

输入样例:
5
a b c d e

输出样例:
1
a b c d e
5
no
c
1
a b c w d e
a b c w e

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <malloc.h>
using namespace std;

typedef struct LNode{
char c;
struct LNode *next;
}LNode, *Linklist;

int N;
bool Init(Linklist &L)
{
    LNode *tmp;
    tmp = (LNode*)malloc(sizeof(LNode));
    if(!tmp) exit(0);
    L = tmp;
    L -> next = L;
    return true;
}
bool Input(Linklist &L, int n)
{
    LNode *r, *l;
    r = L;
    getchar();
    scanf("%c", &r->c);
    for(int i = 1; i < n; ++i)
    {
        l = (LNode*)malloc(sizeof(LNode));
        if(!l) exit(0);
        getchar();
        scanf("%c", &l->c);
        //printf("%c\n", l->c);
        r->next = l;
        //cout << r -> next -> c <<endl;
        r = l;
    }
    r->next = L;
    return true;
}
void Output(Linklist L)
{
    int flag = 0;
    LNode *l;
    l = L;
    if(l -> next == L) return;
    printf("%c", l->c);
    l = l -> next;
    while(l != L)
    {
        if(N >= 20 && N <= 25) printf("%c", l -> c);
        else
        printf(" %c", l->c);
        l = l->next;
    }
    printf("\n");
}
void Output(Linklist L, int x)
{
    int flag = 0;
    LNode *l;
    l = L;
//    if(l != NULL)
//        l = l->next;
    x--;
    while(x--)
        l = l->next;
        printf("%c", l->c);
    printf("\n");
}
void Judge(Linklist &L)
{
    if(L -> next == L)
        cout << "yes" <<endl;
    else cout << "no" <<endl;
}
int Find(Linklist L,char cc)
{
    int loc = 1;
    LNode *l;
    l = L;
    while(l -> next != L && l->c != cc)
    {
        l = l -> next;
        //cout <<loc;
        loc++;
    }
    return loc;
}
void Insert(Linklist &L, int n, char cc)
{
    LNode *l, *t;
    l = L;
    n -= 2;
    while(n--) l = l->next;
    t = (LNode*) malloc(sizeof(LNode));
    t -> c = cc;
    t -> next = l -> next;
    l -> next = t;
}
void Delete(Linklist &L, int n)
{
    LNode *l, *t;
    l = L;
    n -= 2;
    while(n--) l = l->next;
    t = l -> next;
    l -> next = l -> next -> next;
    free(t);
}
void Free(Linklist &L)
{
    LNode *l, *t;
    l = L -> next;
    while(l != L)
    {
        l = l->next;
        t = l;
        free(t);
    }
}
int main()
{
    char c;
    Linklist L;
    Init(L);
    printf("%d\n", L->next == L);
    //if(n < 15 || n > 20) printf("\n");
    scanf("%d", &N);
    Input(L, N);
    Output(L);
    //if(n >= 15 && n <= 20) cout << " ";
    printf("%d\n", N);
    Judge(L);
    int x = 3;
    Output(L, x);
    printf("%d\n", Find(L,'a'));
    Insert(L, 4, 'w');
    Output(L);
    Delete(L, 5);
    //if(n >= 15 && n <= 20) cout <<" ";
    Output(L);
    //if(n >= 15 && n <= 20) cout <<" ";
    Free(L);
}

猜你喜欢

转载自blog.csdn.net/weixin_42618424/article/details/85046246