大佬们求救c++

cj云 2021-05-22 03:45:13
#include<iostream> #include<stdio.h> using namespace std; #define ok 0 #define error -1 #define max 6 typedef char typeelem; typedef int status; typedef struct no {     struct no *next;     typeelem data; } noe, *noa; typedef struct noc {     noa tail, head;     int leng; } sqlist; status initlist(sqlist &L);//创建一个空的链式线性表 status shuzhi(sqlist &L);//对表进行创建max大小链式表 status traverselist(sqlist L);//对链式表进行遍历 status getelem(sqlist L, int i);//在链式表中将第i个元素取出 status locateelem(sqlist L, typeelem e, int c);//在链式表中找值为e的结点并找第c个结点的值 status listinsert(sqlist &L, int d, typeelem a);//链式表中插入值 status listdelete(sqlist &L, typeelem b);//删除值为b的结点 int main() {     sqlist L;     int i, d, c;     typeelem e, a, b;     initlist(L);     cout << "put max=" << max << "elem" << endl;     shuzhi(L);     cout << "traverselist" << endl;     traverselist(L);     cout << "put i from 1-" << max << endl;     cin >> i;     getelem(L, i);     cout << "seek number c and elem e" << endl;     cin >> e;     locateelem(L, e, c);     return ok; } status initlist(sqlist &L) {     L.head = new noe[1];     if(!L.head)     {         cout <<"\bfailure to open up space" << endl;         return error;     }     else         L.tail = L.head;         L.leng = 0; } status shuzhi(sqlist &L) {        noa q;        L.leng = max;        for (int i = 0; i < L.leng; i++)        {            q = new noe[1];            if (!q)            {                cout << "failure to open up space" << endl;                return error;            }            cin >> q->data;            L.tail->next = q;            L.tail = q;            q->next = NULL;     }         return ok; } status traverselist(sqlist L) {     noa p;     p = L.head->next;     for (int i = 0; i < L.leng; i++)     {         cout << p->data << endl;         p = p->next;     }     return ok; } status getelem(sqlist L, int i) {     int j;     noa q = L.head;     if(max<i<0)     {         cout << "i is error" << endl;         return error;     }     else     for (j = 1; j <= i; j++)     {         q = q->next;     }  cout << q->data << endl;     return ok; } status locateelem(sqlist L, typeelem e, int c) {     c = 1;     noa q = L.head->next;        while(q->data != e)         {             q = q->next;             c++;         }       if (c > L.leng)         {             cout << c << "isn't ture" << endl;             return error;         }         else         {             cout << "the potition is" << c << endl;             cout << q->data << endl;                 return ok;        }      老佬们,我想求教一下在我写的locateelem函数中当我输入的值在线性表中没有,我想输出if (c > L.leng)         {             cout << c << "isn't ture" << endl;             return error;         }代码怎么弄
...全文
176 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cj云 2021-05-23
  • 打赏
  • 举报
回复
引用 1 楼 qzjhjxj的回复:
修改见注释,供参考:
#include<iostream>
#include<stdio.h>
using namespace std;
#define ok 0
#define error -1
#define max 6

typedef char typeelem;
typedef int status;

typedef struct no
{
    struct no *next;
    typeelem data;
} noe, *noa;

typedef struct noc
{
    noa tail, head;
    int leng;
} sqlist;

status initlist(sqlist &L);//创建一个空的链式线性表
status shuzhi(sqlist &L);//对表进行创建max大小链式表
status traverselist(sqlist L);//对链式表进行遍历
status getelem(sqlist L, int i);//在链式表中将第i个元素取出
status locateelem(sqlist L, typeelem e, int c);//在链式表中找值为e的结点并找第c个结点的值
status listinsert(sqlist &L, int d, typeelem a);//链式表中插入值
status listdelete(sqlist &L, typeelem b);//删除值为b的结点

int main()
{
    sqlist L;
    int i, d, c;
    typeelem e, a, b;
    initlist(L);
    cout << "put max=" << max << "elem" << endl;
    shuzhi(L);
    cout << "traverselist" << endl;
    traverselist(L);
    cout << "put i from 1-" << max << endl;
    cin >> i;
    getelem(L, i);
    cout << "seek number c and elem e" << endl;
    cin >> e;
    locateelem(L, e, c);
    
    return ok;
}
status initlist(sqlist &L)
{
    L.head = new noe[1];
    if(!L.head)
    {
        cout <<"\bfailure to open up space" << endl;
        return error;
    }
    else
        L.tail = L.head;
        L.leng = 0;
}
status shuzhi(sqlist &L)
{
       noa q;
       L.leng = max;
       for (int i = 0; i < L.leng; i++)
       {
           q = new noe[1];
           if (!q)
           {
               cout << "failure to open up space" << endl;
               return error;
           }
           cin >> q->data;
           L.tail->next = q;
           L.tail = q;
           q->next = NULL;
        }
        return ok;
}
status traverselist(sqlist L)
{
    noa p;
    p = L.head->next;
    for (int i = 0; i < L.leng; i++)
    {
        cout << p->data << endl;
        p = p->next;
    }
    return ok;
}
status getelem(sqlist L, int i)
{
    int j;
    noa q = L.head;
    if(i>max || i<1)    //if(max<i<0)
    {
        cout << "i is error" << endl;
        return error;
    }
    else
    for (j = 1; j <= i; j++)
    {
        q = q->next;
    }
    cout << q->data << endl;
    return ok;
}
status locateelem(sqlist L, typeelem e, int c)
{
    c = 1;
    noa q = L.head->next;
    while(q->data != e && q->next!=NULL)
    {
          q = q->next;
          c++;
    }
    if(q->data != e)//if (c > L.leng)
    {
          cout << c+1 << " isn't ture" << endl;
          return error;
    }
    else
    {
          cout << "the potition is " << c << endl;
          cout << q->data << endl;
          return ok;
    }
}
感谢大佬,我尝试一下
qzjhjxj 2021-05-22
  • 打赏
  • 举报
回复
修改见注释,供参考:
#include<iostream>
#include<stdio.h>
using namespace std;
#define ok 0
#define error -1
#define max 6

typedef char typeelem;
typedef int status;

typedef struct no
{
    struct no *next;
    typeelem data;
} noe, *noa;

typedef struct noc
{
    noa tail, head;
    int leng;
} sqlist;

status initlist(sqlist &L);//创建一个空的链式线性表
status shuzhi(sqlist &L);//对表进行创建max大小链式表
status traverselist(sqlist L);//对链式表进行遍历
status getelem(sqlist L, int i);//在链式表中将第i个元素取出
status locateelem(sqlist L, typeelem e, int c);//在链式表中找值为e的结点并找第c个结点的值
status listinsert(sqlist &L, int d, typeelem a);//链式表中插入值
status listdelete(sqlist &L, typeelem b);//删除值为b的结点

int main()
{
    sqlist L;
    int i, d, c;
    typeelem e, a, b;
    initlist(L);
    cout << "put max=" << max << "elem" << endl;
    shuzhi(L);
    cout << "traverselist" << endl;
    traverselist(L);
    cout << "put i from 1-" << max << endl;
    cin >> i;
    getelem(L, i);
    cout << "seek number c and elem e" << endl;
    cin >> e;
    locateelem(L, e, c);
    
    return ok;
}
status initlist(sqlist &L)
{
    L.head = new noe[1];
    if(!L.head)
    {
        cout <<"\bfailure to open up space" << endl;
        return error;
    }
    else
        L.tail = L.head;
        L.leng = 0;
}
status shuzhi(sqlist &L)
{
       noa q;
       L.leng = max;
       for (int i = 0; i < L.leng; i++)
       {
           q = new noe[1];
           if (!q)
           {
               cout << "failure to open up space" << endl;
               return error;
           }
           cin >> q->data;
           L.tail->next = q;
           L.tail = q;
           q->next = NULL;
        }
        return ok;
}
status traverselist(sqlist L)
{
    noa p;
    p = L.head->next;
    for (int i = 0; i < L.leng; i++)
    {
        cout << p->data << endl;
        p = p->next;
    }
    return ok;
}
status getelem(sqlist L, int i)
{
    int j;
    noa q = L.head;
    if(i>max || i<1)    //if(max<i<0)
    {
        cout << "i is error" << endl;
        return error;
    }
    else
    for (j = 1; j <= i; j++)
    {
        q = q->next;
    }
    cout << q->data << endl;
    return ok;
}
status locateelem(sqlist L, typeelem e, int c)
{
    c = 1;
    noa q = L.head->next;
    while(q->data != e && q->next!=NULL)
    {
          q = q->next;
          c++;
    }
    if(q->data != e)//if (c > L.leng)
    {
          cout << c+1 << " isn't ture" << endl;
          return error;
    }
    else
    {
          cout << "the potition is " << c << endl;
          cout << q->data << endl;
          return ok;
    }
}

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧