描述
请你定义一个线性表,可以对表进行“在某个位置之前插入一个元素”、“删除某个位置的元素”、“清除所有元素”、“获取某个位置的元素”等操作。键盘输入一些命令,可以执行上述操作。本题中,线性表元素为整数,线性表的第一个元素位置为1。线性表的最大长度为1000。
输入
各个命令以及相关数据,它们对应的格式如下:
在某个位置之前插入操作:insert,接下来的一行是插入的组数n,下面是n行数据,每行数据有两个值,分别代表位置与插入的元素值
清除线性表:clear
获取某个位置的元素:getelem,接下来一行是需要获取的元素位置
删除某个位置的元素:delete,接下来一行是被删除的元素位置
当输入的命令为exit时,程序结束
输出
当输入的命令为getelem时,请输出获取的元素值,
当输入的命令是delete时,请输出被删除的那个元素值
注意,所有的元素均占一行
样例输入
insert
2
1 1
2 2
delete
1
clear
insert
2
1 3
2 4
getelem
2
exit
样例输出
1
4
我完全不知道这种数据结构提的代码应该怎么写,书上的代码都不太能看懂,有木有好心的大神帮帮忙,告诉我写代码的格式思路等等,。。。家qq更好。谢谢
数据结构练习题——线性表操作
答案:3 悬赏:20
解决时间 2021-04-27 21:15
- 提问者网友:放荡不羁
- 2021-04-27 16:54
最佳答案
- 二级知识专家网友:佛说妍妍很渣
- 2021-04-27 17:39
#include<iostream.h>
template<class T>
class arrlist{
private:
T *alist;
int maxsize;
int curlen;
int position;
public:
arrlist(const int size){
maxsize=size;
alist=new T[maxsize];
curlen=position=0;
}
~arrlist(){
delete[] alist;
}
void clear(){
delete[] alist;
curlen=position=0;
alist=new T[maxsie];
}
bool getpos(int&p,const T value){
int i;
for(i=0;i<10;i++)
if(value==alist[i]){
p=i;
return true;
}
cout<<"查不到此数";
return false;
}
bool append(const T value){
int i;
if(curlen==maxsize)
{cout<<"表满"<<endl;
return false;}
for(i=curlen;i<maxsize;i++)
alist[i]=value;
curlen++;
return true;}
bool insert(const int p,const T value)
{
int i;
if(curlen==maxsize){
cout<<"表已满"<<endl;
return false;
}
if(p<0||p>curlen){
cout<<"插错位置"<<endl;
return false;
}
for(i=curlen;i>p;i--)
alist[i]=alist[i-1];
alist[p]=value;
curlen++;
return true;
}
bool deete(const int p){
int i;
if(curlen==0)
{
cout<<"空表"<<endl;
return false;
}
if(p<0||p>curlen-1)
{
cout<<"差错位置"<<endl;
return false;}
for(i=p;i<curlen-1;i++)
alist[i]=alist[i+1];
curlen--;
return true;
}
};
void main(){
arrlist<int> a(100);
int i,A[10]={0,1,2,3,4,5,6,7,8,9},q=0;
for(i=0;i<=9;i++)
a.append(A[i]);
a.deete(0);
a.getpos(q,9);
cout<<q<<endl;
}这个程序可以执行查找,删除,插入,清空。主函数我只写了删除的,慢慢学,数据结构学起来确实不容易。我也正在学习阶段,大二。我的qq1505670045,加不加随意。
template<class T>
class arrlist{
private:
T *alist;
int maxsize;
int curlen;
int position;
public:
arrlist(const int size){
maxsize=size;
alist=new T[maxsize];
curlen=position=0;
}
~arrlist(){
delete[] alist;
}
void clear(){
delete[] alist;
curlen=position=0;
alist=new T[maxsie];
}
bool getpos(int&p,const T value){
int i;
for(i=0;i<10;i++)
if(value==alist[i]){
p=i;
return true;
}
cout<<"查不到此数";
return false;
}
bool append(const T value){
int i;
if(curlen==maxsize)
{cout<<"表满"<<endl;
return false;}
for(i=curlen;i<maxsize;i++)
alist[i]=value;
curlen++;
return true;}
bool insert(const int p,const T value)
{
int i;
if(curlen==maxsize){
cout<<"表已满"<<endl;
return false;
}
if(p<0||p>curlen){
cout<<"插错位置"<<endl;
return false;
}
for(i=curlen;i>p;i--)
alist[i]=alist[i-1];
alist[p]=value;
curlen++;
return true;
}
bool deete(const int p){
int i;
if(curlen==0)
{
cout<<"空表"<<endl;
return false;
}
if(p<0||p>curlen-1)
{
cout<<"差错位置"<<endl;
return false;}
for(i=p;i<curlen-1;i++)
alist[i]=alist[i+1];
curlen--;
return true;
}
};
void main(){
arrlist<int> a(100);
int i,A[10]={0,1,2,3,4,5,6,7,8,9},q=0;
for(i=0;i<=9;i++)
a.append(A[i]);
a.deete(0);
a.getpos(q,9);
cout<<q<<endl;
}这个程序可以执行查找,删除,插入,清空。主函数我只写了删除的,慢慢学,数据结构学起来确实不容易。我也正在学习阶段,大二。我的qq1505670045,加不加随意。
全部回答
- 1楼网友:承载所有颓废
- 2021-04-27 19:13
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct node //定义结点
{
char data[10]; //结点的数据域为字符串
struct node *next; //结点的指针域
}ListNode;
typedef ListNode * LinkList; // 自定义LinkList单链表类型
int Getlen(Linklist L)
{int num=0;
LNode *p;
p=L->next;
while(p!=NULL)
{
num++;
p=p->next;
}
returb()num;
}
//==========用尾插入法建立带头结点的单链表===========
LinkList CreatListR1(void)
{
char ch[100],ch1[10],c;
int i=0,j=0;
LinkList head=(LinkList)malloc(sizeof(ListNode)); //生成头结点
ListNode *s,*r;
r=head;
r->next=NULL;
printf("Input # to end "); //输入"#"代表输入结束
printf("Please input Node_data:");
scanf("%s",ch); //输入各结点的字符串
do
{
c=*(ch+i++);
*(ch1+j++)=c;
if(c==',')
{
s=(LinkList)malloc(sizeof(ListNode));
for(j=0;(j<10)&&*(ch1+j)!=',';j++) *(s->data+j)=*(ch1+j);
*(s->data+j)='\0';
s->next =NULL;
r->next =s;
r=s;
j=0;
}
}
while(c!='#');
return head; //返回头指针
}
//==========按值查找结点,找到则返回该结点的位置,否则返回NULL==========
ListNode *LocateNode(LinkList head, char *key)
{
ListNode *p=head->next; //从开始结点比较
while(strcmp(p->data,key)!=0 && p) //直到p为NULL或p-> data为key止
p=p->next; //扫描下一个结点
return p; //若p=NULL则查找失败,否则p指向找到的值为key的结点
}
//===========打印链表=======
void printlist(LinkList head)
{
ListNode *p=head->next; //从开始结点打印
while(p){
printf("%s, ",p->data);
p=p->next;
}
printf("\n");
}
//==========删除所有结点,释放空间===========
void DeleteAll(LinkList head)
{
ListNode *p=head,*r;
while(p->next){
r=p->next;
free(p);
p=r;
}
free(p);
}
int Delete(LinkList hend,int i)
{int pos=1;
ListNode *q=hend,*p;
if(i<1||i>Getlen(hend) exit(1))
while(pos<i)
{q=q->next;
pos++;
}
p=q->next;
q->next=p->next;
free(p);
return -1;
}
int insert_sq(LinkList head,int pos,char *key)
{
int inspos=0,i=0;
ListNode *p=head,*s;
while(p->next)
{
if(++inspos==pos)
{
s=(LinkList)malloc(sizeof(ListNode));
for(i=0;*(key+i)!='#';i++)*(s->data+i) =*(key+i);
*(s->data+i) ='\0';
s->next=p->next;
p->next=s;
return inspos;
}
p=p->next;
}
return -1;
}
//==========主函数 ==========
main()
{
LinkList h;
char cmd;
int i=0;
char temp[100];
Getlen(&i);
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH\n");
while(1)
{
cmd=getchar();
switch(cmd)
{
case 'C':
case 'c':
h=CreatListR1();
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARCH\n");
break;
case 'I':
case 'i':
printf("please input the number to be inserted,end with '#'\n");
scanf("%s",temp);
printf("please input the position to be inserted\n");
scanf("%d",&i);
printf("%d\n",insert_sq(h,i,temp));
printf("**********Sqlist inserted**********\n");
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSER,S=SEARCT\n");
break;
case 'S':
case 's':
printf("please input the number wanted\n");
scanf("%s",temp);
printf("\n%s\n",temp);
printf("%d\n",LocateNode(h,temp));
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARC\n");
break;
case 'P':
case 'p':
printlist(h);
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARC\n");
break;
case 'D':
case 'd':
printf("please input the position to be inserted\n");
scanf("%d",&i);
printf("%d\n",Delete(h,i));
printf("X=EXIT,C=CREAT,P=DISPLAY,I=INSERT,S=SEARC\n");
break;
case 'X':
case 'x':
DeleteAll(h);
return 1;
default:break;
}
}
}
- 2楼网友:有钳、任性
- 2021-04-27 17:57
对不起呀 大哥 我是mm
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息