山东特级资质建筑公司:5道数据结构编程题,请高手指点!!!

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 04:36:22
1.已知一顺序表A,其元素非递减有序排列。编写一程序删除顺序表中的重复的数据元素。
2.已知带头节点的单链表L中的结点是按整数值递增排列的,试写一算法将值为x的结点插入到表L当中,使L仍然有序。

非递减就可以近似理解成递增,反正是有序,这样很容易了。你相临的比较,相同则删除一个元素,然后把后面的挂上来.依次执行就可以了.

另一个类似啊,不难吧

我最近在学这部分内容 我来写写试试
第二题 lcc-win32下编译通过

#include<stdio.h>
#include<malloc.h>

typedef struct node
{
int data;
struct node *next;
}node;

node *head,*p1,*p2;

/*建链表*/

void creat()
{ int i;

head=p1=(node*)malloc(sizeof(node));

for(i=0;i<40;i+=2)
/*已知带头节点的单链表L中的结点是按整数值递增排列的(就赋0-38所有偶数了)*/
{
p2=(node*)malloc(sizeof(node));
p2->data=i;
p1->next=p2;
p1=p2;
}
p1->next=NULL;
}

/*插入结点*/

void insert(node* temp)
{ p1=head->next;
p2=head;
while(p1!=NULL&&(temp->data)>(p1->data))
{
p2=p1;
p1=p2->next;
}
if(p1!=NULL)
{
temp->next=p1;
p2->next=temp;
}
else
{
p2->next=temp;
temp->next=NULL;

}
}

/*输出链表*/
void print()
{

p1=head->next;
while(p1!=NULL)
{
printf("%d->",p1->data);

p1=p1->next;
}
printf("\n");
}

/*主函数*/
int main()
{ node *temp;

creat();
printf("原链表为:\n");
print();
temp=(node*)malloc(sizeof(node));
printf("输入要插入的值\n");
scanf("%d",&temp->data);
insert(temp);
printf("插入结点后链表为:\n");
print();
return 0;
}