zoukankan      html  css  js  c++  java
  • 作业

    #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;
    }

  • 相关阅读:
    [数据结构]图的DFS和BFS的两种实现方式
    [算法]两个栈实现一个队列
    [数据结构]手动实现队列
    [数据结构]手动实现栈
    [数据结构]手动实现单链表
    Hive分组取Top K数据
    HBase解决海量图片存储方案
    非结构化数据存储方案
    头条面试题之实现两个线程轮流打印字符串
    [算法]最大连续子数组和,最长重复子串,最长无重复字符子串
  • 原文地址:https://www.cnblogs.com/zhangdashuai/p/3640373.html
Copyright © 2011-2022 走看看