jmu-ds-循环单链表的基本运算

7-1 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 <bits/stdc++.h>
using namespace std;

list<char> lis;
list<char> ::iterator it,it2;
void print()
{
    if(lis.empty())
        return;
    for(it=lis.begin(); it!=lis.end(); it++)
    {
        if(it==lis.begin())
            cout<<*it;
        else
            cout<<' '<<*it;
    }
    cout<<endl;
}
int main()
{
    lis.clear();
    int n;
    char c;
    cout<<1<<endl;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>c;
        lis.push_back(c);
    }
    print();
    cout<<lis.size()<<endl;
    printf(lis.empty()?"yes\n":"no\n");
    int pos=0;
    for(it=lis.begin(); it!=lis.end(); it++)
    {
        pos++;
        if(pos==3)
        {
            cout<<*it<<endl;
            break;
        }
    }
    pos=0;
    for(it=lis.begin(); it!=lis.end(); it++)
    {
        pos++;
        if(*it=='a')
        {
            cout<<pos<<endl;
            break;
        }
    }
    pos=0;
    for( it=lis.begin(); it!=lis.end(); it++)
    {
        pos++;
        if(pos==4)
            break;
    }
    it2=it;
    if(it!=lis.end())
    {
        lis.insert(it,'w');
    }
    print();
    lis.erase(it2);
    print();
    lis.clear();
    return 0;
}

法二:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct LNode
{
    char data;
    struct LNode *next;
} LNode,*LinkList;
void ListPrint_L(LinkList &L)
{
//输出单链表
    LNode *p=L->next;  //p指向第一个元素结点
    while(p!=NULL)
    {
        if(p->next!=NULL)
            printf("%c ",p->data);
        else
            printf("%c\n",p->data);
        p=p->next;
    }
}
void ListIsEmpty_L(LinkList &L)
{
    if(L->next == NULL)
        printf("yes\n");
    else
        printf("no\n");
}
int GetListLength_L(LinkList &L)
{

    LNode *p=L->next;  //p指向第一个元素结点
    int length = 0;
    while(p!=NULL)
    {
        ++length;
        p=p->next;
    }
    return length;
}
void ListPrintfValue_L(LinkList &L)
{
    int pos = 1;
    LNode *p=L->next;  //p指向第一个元素结点
    while(p!=NULL)
    {
        if(pos == 3)
        {
            printf("%c\n", p->data);
            break;
        }
        ++pos;
        p=p->next;
    }
}
void ListPrintfPos_L(LinkList &L)
{
    int pos = 1;
    LNode *p=L->next;  //p指向第一个元素结点
    while(p!=NULL)
    {
        if(p->data == 'a')
        {
            printf("%d\n", pos);
            break;
        }
        ++pos;
        p=p->next;
    }
}
void ListInsert_L(LinkList &L)
{
    int pos = 1;
    LNode *p=L->next;  //p指向第一个元素结点
    while(p!=NULL)
    {
        if(pos == 3)
        {
            LNode *curPtr, *temp;
            curPtr = (LNode *)malloc(sizeof(LNode));
            curPtr->data = 'w';
            temp = p->next;
            p->next = curPtr;
            curPtr->next = temp;
            break;
        }
        ++pos;
        p=p->next;
    }
}
void ListDelet_L(LinkList &L)
{
    int pos = 1;
    LNode *p=L->next;  //p指向第一个元素结点
    while(p!=NULL)
    {
        if(pos == 4)
        {
            LNode *del;
            del = p->next;
            p->next = del->next;
            free(del);
            break;
        }
        ++pos;
        p=p->next;
    }
}
void ListDeletAll_L(LinkList &L)
{
    LNode *p=L->next;  //p指向第一个元素结点
    while(p!=NULL)
    {
            LNode *del;
            del = p;
            p = p->next;
            free(del);
    }
}
int main()
{
    LinkList L;
    LNode *curPtr, *rearPtr;
    rearPtr = L = (LNode *)malloc(sizeof(LNode));
    L->next = NULL;
    printf("1\n");
    int n, i;
    char value;
    scanf("%d", &n);
    getchar();
    for(i = 0; i < n; ++i)
    {
        curPtr = (LNode *)malloc(sizeof(LNode));
        if(i == n - 1)
         scanf("%c", &curPtr->data);
         else
            scanf("%c ", &curPtr->data);
        rearPtr->next = curPtr;
        rearPtr = curPtr;
    }
    rearPtr->next = NULL;
    ListPrint_L(L);
    printf("%d\n",GetListLength_L(L));
    ListIsEmpty_L(L);
    ListPrintfValue_L(L);
    ListPrintfPos_L(L);
    ListInsert_L(L);
    ListPrint_L(L);
    ListDelet_L(L);
    ListPrint_L(L);
    ListDeletAll_L(L);

}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值