有一个长度为n以顺序结构存储的线性表,结点值均为正整数,编写一个算法,把该线性表分解为两个线性表,其
来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/05/08 08:07:33
有一个长度为n以顺序结构存储的线性表,结点值均为正整数,编写一个算法,把该线性表分解为两个线性表,其
中一个线性表的结点值均为奇数,另一个线性表的结点值均为偶数.
中一个线性表的结点值均为奇数,另一个线性表的结点值均为偶数.
//不知道可不可以,你试下
#include
#include
#include
struct node
{
int date;//数据域
struct node *pn;//指针域
};
struct node* init(int x);
void insert(struct node *phead,int pos,int *pval);
bool isempty(struct node *phead);
bool isfull(struct node *phead);
void show(struct node *phead);
int lenth(struct node *phead);
struct node* init(int x)//初始化链表,给链表赋值
{
int val;
struct node *phead=(struct node*)malloc(sizeof(struct node));
if(NULL==phead)
{
printf("内存分配失败!\n");
exit(-1);
}
struct node *ptail;
ptail=phead;
ptail->pn=NULL;
if(x==0)
{
return phead;
}
printf("输入%d个数\n",x);
for(int a=0;adate=val;
ptail->pn=p;
p->pn=NULL;
ptail=p;
}
return phead;
}
void show(struct node *phead)
{
if(phead->pn==NULL)
{
printf("链表为空!\n");
//exit(-1);
}
struct node *p=phead->pn;
while(p!=NULL)
{
printf(" %d ",p->date);
p=p->pn;
}
printf("\n");
}
int lenth(struct node *phead)
{
int len=0;
struct node *p=phead->pn;
while(p!=NULL)
{
len=len+1;
p=p->pn;
}
return len;
}
bool isempty(struct node *phead)
{
if(phead->pn==NULL)
{
return true;
}
else
{
return false;
}
}
bool isfull(struct node *phead)
{
if(phead->pn!=NULL)
{
return true;
}
else
{
return false;
}
}
bool insert(struct node *phead,int pos,int pval)
{
int i=0;
struct node *p=phead;
while(NULL!=p&&ipn;
++i;
}
if(i>pos-1||NULL==p)
{
return false;
}
struct node * pnew=(struct node *)malloc(sizeof(struct node ));
if(NULL==pnew)
{
printf("动态内存分配失败!");
exit(-1);
}
pnew->date=pval;
struct node * q=p->pn;
p->pn=pnew;
pnew->pn=q;
return true;
}
int main()
{
struct node *phead1;//头指针
struct node *phead2;
struct node *phead3;
int pval;
int a=1,b=1;
phead1=init(10);
phead2=init(0);
phead3=init(0);
printf("线性表1中的元素是:\n");
show(phead1);
struct node *p=phead1->pn;
while(p!=NULL)
{
if((p->date)%2==1)
{
insert(phead3,a,p->date);
a++;
}
if((p->date)%2==0)
{
insert(phead2,b,p->date);
b++;
}
p=p->pn;
}
printf("线性表2中的元素(偶数):\n");
show(phead2);
printf("线性表3中的元素(奇数):\n");
show(phead3);
return 0;
}
#include
#include
#include
struct node
{
int date;//数据域
struct node *pn;//指针域
};
struct node* init(int x);
void insert(struct node *phead,int pos,int *pval);
bool isempty(struct node *phead);
bool isfull(struct node *phead);
void show(struct node *phead);
int lenth(struct node *phead);
struct node* init(int x)//初始化链表,给链表赋值
{
int val;
struct node *phead=(struct node*)malloc(sizeof(struct node));
if(NULL==phead)
{
printf("内存分配失败!\n");
exit(-1);
}
struct node *ptail;
ptail=phead;
ptail->pn=NULL;
if(x==0)
{
return phead;
}
printf("输入%d个数\n",x);
for(int a=0;adate=val;
ptail->pn=p;
p->pn=NULL;
ptail=p;
}
return phead;
}
void show(struct node *phead)
{
if(phead->pn==NULL)
{
printf("链表为空!\n");
//exit(-1);
}
struct node *p=phead->pn;
while(p!=NULL)
{
printf(" %d ",p->date);
p=p->pn;
}
printf("\n");
}
int lenth(struct node *phead)
{
int len=0;
struct node *p=phead->pn;
while(p!=NULL)
{
len=len+1;
p=p->pn;
}
return len;
}
bool isempty(struct node *phead)
{
if(phead->pn==NULL)
{
return true;
}
else
{
return false;
}
}
bool isfull(struct node *phead)
{
if(phead->pn!=NULL)
{
return true;
}
else
{
return false;
}
}
bool insert(struct node *phead,int pos,int pval)
{
int i=0;
struct node *p=phead;
while(NULL!=p&&ipn;
++i;
}
if(i>pos-1||NULL==p)
{
return false;
}
struct node * pnew=(struct node *)malloc(sizeof(struct node ));
if(NULL==pnew)
{
printf("动态内存分配失败!");
exit(-1);
}
pnew->date=pval;
struct node * q=p->pn;
p->pn=pnew;
pnew->pn=q;
return true;
}
int main()
{
struct node *phead1;//头指针
struct node *phead2;
struct node *phead3;
int pval;
int a=1,b=1;
phead1=init(10);
phead2=init(0);
phead3=init(0);
printf("线性表1中的元素是:\n");
show(phead1);
struct node *p=phead1->pn;
while(p!=NULL)
{
if((p->date)%2==1)
{
insert(phead3,a,p->date);
a++;
}
if((p->date)%2==0)
{
insert(phead2,b,p->date);
b++;
}
p=p->pn;
}
printf("线性表2中的元素(偶数):\n");
show(phead2);
printf("线性表3中的元素(奇数):\n");
show(phead3);
return 0;
}
已知长度为n的线性表A采用顺序存储结构,请写一算法,找出该线性表中值最小的数据元素.
已知长度为n的线性表A采用顺序存储结构,请写出一个时间复杂度为O(n)、空间复杂度为O(1)的算法,该算法可删除线性表中
用c++实现,假设有两个元素递增的有序排列线性表A和B,均以顺序表作存储结构.试编写算法将A表和B表归并成一个按元素值递
设有一个线性表采用顺序存储结构,表中的数据元素值为正整数(n个).设在O(n) 时间内,将线性表分成两为两部分,其中左半
已知长度为n的线性表A采用顺序存储结构,写一时间效率有效的算法,删除数据元素[x,y]之间的所有元素.
对于长度为n的顺序存储的线性表,当随机插入和删除一个元素时,需平均移动元素的个数为
已知长度为n的线性表A中的元素是整数,采用顺序储存结构,删除线性表中所有值为x的数据元素.
以单链表为存储结构,写一实现线性表就地逆置的算法(用C++写)
在一个长度为n的顺序线性表中顺序查找值为x的元素时,查找成功时平均查找长度为多少
在一个长度为n顺序线性表中顺序查找值为x的元素时,查找的平均长度为
设计一个算法,将某一个X值插入到一个有序(运用顺序存储结构),对线性表进
线性表的顺序存储结构和线性表的链式存储结构分别是