#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;
}代碼怎么弄


uj5u.com熱心網友回復:
修改見注釋,供參考:#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;
}
}
uj5u.com熱心網友回復:
感謝大佬,我嘗試一下
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/284818.html
標籤:C++ 語言
下一篇:面向物件