问题描述:

编写一个程序exp2-2.cpp,实现单链表的各种基本运算(假设单链表的元素类型为char),并在此基础上完成如下功能:
(1)初始化单链表h;
(2)采用尾插法依次插入元素a,b,c,d,e;
(3)输出单链表h;
(4)输出单链表h长度;
(5)判断单链表h是否为空;
(6)输出单链表h的第3个元素;
(7)输出元素a的位置;
(8)在第4个元素位置上插入元素f;
(9)输出单链表h;
(10)删除h的第3个元素;
(11)输出单链表h;
(12)释放单链表h。


代码:

#include <iostream>
#include<malloc.h>
using namespace std;

typedef struct LNode
{
    char data;
    struct LNode *next;
}LinkList;
void InitList(LinkList *&l)//初始化单链表
{
    l=(LinkList*)malloc(sizeof(LinkList));
    l->next=NULL;
}
void CreateListR(LinkList *&l,char a[],int n)
{
    LinkList *s,*r;
    r=l;
    int i=0;
    while(i<n)
    {
        s=(LinkList *)malloc(sizeof(LinkList));
        s->data=a[i];
        r->next=s;
        r=s;
        i++;
    }
    r->next=NULL;
}
void DispList(LinkList *l)
{
    LinkList *p=l->next;
    while(p!=NULL)
    {
       cout<<p->data<<" ";
       p=p->next;
    }
    cout<<endl;
}
int ListLength(LinkList *l)
{
    int n=0;
    LinkList *q=l;
    while(q->next!=NULL)
    {
        n++;q=q->next;
    }
    return n;
}
bool ListEmpty(LinkList *L)
{
    return (L->next==NULL);
}
bool GetElem(LinkList *l,int i,char &e)
{
    int j=0;
    LinkList *q=l;
    while(q!=NULL&&j<i)
    {
        j++;
        q=q->next;
    }
    if(q==NULL)
        return false;
    else
    {
        e=q->data;
        return true;
    }
}
int GetLocate(LinkList *l,char e)
{
    int i=1;
    LinkList *q=l->next;
    while(q->data!=e&&q!=NULL)
    {
        i++;
        q=q->next;
    }
    if(q==NULL)
        return 0;
    else
        return i;
}
bool ListInsert(LinkList *&l,int i,char e)
{
    int j=0;
    LinkList *q=l,*s;
    while(j<i-1&&q!=NULL)
    {
        j++;
        q=q->next;
    }
    if(q==NULL)
        return false;
    else
    {
        s=(LinkList *)malloc(sizeof(LinkList));
        s->data=e;
        s->next=q->next;
        q->next=s;
        return true;
    }
}
bool ListDelete(LinkList *&l,int i)
{
    int j=0;
    LinkList *q=l,*p;
    while(q!=NULL&&j<i-1)
    {
        j++;
        q=q->next;
    }
    if(q==NULL)
        return false;
    else
    {
        p=q->next;
        q->next=p->next;
        free(p);
        return true;
    }
}
void DestroyList(LinkList *&l)
{
    LinkList *pre=l,*p=l->next;
    while(p!=NULL)
    {
        free(pre);
        pre=p;
        p=p->next;
    }
    free(pre);
}

int main()
{
    LinkList *L;
    cout<<"1.初始化单链表"<<endl;
    InitList(L);

    cout<<"2.尾插法插入元素a,b,c,d,e"<<endl;
    char a[]={'a','b','c','d','e'};
    CreateListR(L,a,5);

    cout<<"3.输出单链表:";
    DispList(L);

    cout<<"4.单链表的长度为:"<<ListLength(L)<<endl;

    if(ListEmpty(L))
    {
        cout<<"5.单链表为空"<<endl;
    }
    else
    {
        cout<<"5.单链表不为空"<<endl;
    }

    char e;
    GetElem(L,3,e);
    cout<<"6.第3个元素为:"<<e<<endl;

    cout<<"7.元素a的位置为:"<<GetLocate(L,'a')<<endl;

    cout<<"8.在第4个位置上插入f"<<endl;
    ListInsert(L,4,'f');

    cout<<"9.输出单链表:";
    DispList(L);

    cout<<"10.删除第3个元素"<<endl;
    ListDelete(L,3);

    cout<<"11.输出单链表:";
    DispList(L);

    cout<<"12.释放单链表"<<endl;
    DestroyList(L);
    return 0;
}

运行结果:

数据结构学习-线性表(2)_数据结构