作业

#include<iostream>
#include<malloc.h>
using namespace std;
int sum;
struct LNode
{
    char data;
    struct LNode * next;
};
void f1(LNode *&L)
{
    LNode *s,*r;
    int n;
    L=(LNode *)malloc(sizeof(LNode));
    cout<<"请输入元素的个数:"<<endl;
    cin>>n;
    r=L;
    for(int i=0;i<n;i++)
    {
        s=(LNode *)malloc(sizeof(LNode));
        cin>>s->data;
        sum++;
        r->next=s;
        r=s;
    }
    r->next=NULL;
}
void f2(LNode *&L)
{
    LNode *r=L,*s;
    while(r->next)
    {
        r=r->next;
    }
    s=(LNode *)malloc(sizeof(LNode));
    cin>>s->data;
    s->next=NULL;
    r->next=s;
    sum++;
}

void f3(LNode *&L)
{
    LNode *s;
    s=L->next;
    while(s)
    {
        cout<<s->data;
        s=s->next;
    }
}
void f4()
{
    cout<<sum<<endl;
}
void f5()
{
    if(sum==0)
        cout<<"空"<<endl;
    else cout<<"非空"<<endl;
}
void f6(LNode *&L)
{
    LNode *s=L;
    int n;
    cout<<"您要输出第几个元素n:"<<endl;
    cin>>n;
    while(n--)
    {
        s=s->next;
    }
    cout<<s->data;
}
void f7(LNode *&L)
{
    int n=0;
    char c;
    cout<<"请输入要查询的元素:"<<endl;
    cin>>c;
    LNode *s=L;
    while(s->data!='c'&&s)
    {


int main()
{
//    int n;
    sum=0;
    LNode *L;
    cout<<"1:初始化一个链表并且输入元素"<<endl;
    cout<<"2:插入1个元素(尾查法)"<<endl;
    cout<<"3:输出链表"<<endl;
    cout<<"4:输出链表长度"<<endl;
    cout<<"5:判断链表是否为空"<<endl;
    cout<<"6:输出第i个元素"<<endl;
    cout<<"7:输出元素的位置"<<endl;
    cout<<"8:在第i个位置上插入元素"<<endl;
    cout<<"9:删除第i个元素"<<endl;
    cout<<"10:释放链表"<<endl;
    f1(L);
    f6(L);
    return 0;
}

原文地址:https://www.cnblogs.com/zhangdashuai/p/3640373.html