软基上机的报告链表拓展题.docx

上传人:b****6 文档编号:8186691 上传时间:2023-01-29 格式:DOCX 页数:23 大小:17.67KB
下载 相关 举报
软基上机的报告链表拓展题.docx_第1页
第1页 / 共23页
软基上机的报告链表拓展题.docx_第2页
第2页 / 共23页
软基上机的报告链表拓展题.docx_第3页
第3页 / 共23页
软基上机的报告链表拓展题.docx_第4页
第4页 / 共23页
软基上机的报告链表拓展题.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

软基上机的报告链表拓展题.docx

《软基上机的报告链表拓展题.docx》由会员分享,可在线阅读,更多相关《软基上机的报告链表拓展题.docx(23页珍藏版)》请在冰豆网上搜索。

软基上机的报告链表拓展题.docx

软基上机的报告链表拓展题

1、程序流程说明

2、1)创建一个单链表,其数据元素为整数,从键盘输入,输入0结束(注意0不放到链表内);

3、2)从键盘任意输入一个整数,在单链表中查询该数,如果单链表中已经存在这个数,就调用删除函数,删除该元素所在结点,并将单链表在删除前后的数据元素依次输出到屏幕上;

4、如果单链表中不存在这个数,就调用插入函数,将这个数插入到单链表尾,并将单链表在插入前后的数据元素依次输出到屏幕上。

5、3)教材第一章习题第9题(用链表实现)ex2_3——扩展题

6、1)删除单链表中全部的负数

7、2)创建一个双向链表,按照冒泡排序的思路对这个双向链表进行排序,打印排序结果。

注意,本算法在交换元素时是将链点整个交换而不是将链点中的元素值交换。

8、

9、

10、

11、

2_2

1)创建一个单链表,其数据元素为整数,从键盘输入,输入0结束(注意0不放到链表内);(ifx!

=0,scanf)

2)从键盘任意输入一个整数,在单链表中查询该数,如果单链表中已经存在这个数,就调用删除函数,删除该元素所在结点,并将单链表在删除前后的数据元素依次输出到屏幕上;(search返回重复的位置,然后删除(同实验一))

如果单链表中不存在这个数,就调用插入函数,将这个数插入到单链表尾,并将单链表在插入前后的数据元素依次输出到屏幕上。

(同实验一,直接加在链表末尾)

3)判断插入元素与表内元素的大小,如temp->datalink->data,则插入temp后

2_3

1)删除单链表中全部的负数(逐个判断是不是负数,并且返回负数所在位置,删除过春哥同实验一)

2)创建一个双向链表,按照冒泡排序的思路对这个双向链表进行排序,打印排序结果。

注意,本算法在交换元素时是将链点整个交换而不是将链点中的元素值交换。

(从一开始逐个比较,将比第一位大的与第一位交换位置,一直比较到最后一位然后开始对于第二位重复比较)

二、程序代码

2_2

#include

#include

#include

typedefstructNode{

intdata;

structNode*link;

}node;

typedefstructList{

node*head;

node*tail;

intlength;

}list;

voidcreat_list(list*table)

{

intx,i;

node*temp;

table->head=NULL;

table->tail=NULL;

table->length=0;

x=1;

scanf("%d",&x);

for(i=0;x!

=0;i++)

{

fflush(stdin);

printf("\n");

temp=(node*)malloc(sizeof(node));

temp->data=x;

temp->link=NULL;

temp->link=table->head;

table->head=temp;

table->length++;

scanf("%d",&x);

}

}

node*create_node(intnew_node)

{

node*temp;

temp=(node*)malloc(sizeof(node));

temp->data=new_node;

temp->link=NULL;

returntemp;

}

voidget(list*table,intn)

{

inti,loc=table->length+1;

node*temp;

node*newnode;

temp=table->head;

table->length++;

newnode=create_node(n);

for(i=2;i

{

temp=temp->link;

}

newnode->link=temp->link;

temp->link=newnode;

}

voidshow_list(list*table)

{

intx,i;

node*temp;

printf("链表为:

\n");

temp=table->head;

for(i=1;i<=table->length;i++)

{

x=temp->data;

printf("%d",x);

temp=temp->link;

}

}

voiddelete_node(list*table,intn)

{

inti;

node*temp,*t;

temp=table->head;

if(n==1)

{

temp=temp->link;

table->head=temp;

}

if(n>2&&nlength)

{

temp=table->head;

for(i=2;i

temp=temp->link;

t=temp->link;

temp->link=t->link;

}

if(n==table->length)

{for(i=2;i

temp=temp->link;

temp->link=NULL;

}

if(n==2)

{

temp=table->head;

t=temp->link;

temp->link=t->link;

}

table->length--;

}

voidsearch(list*table,intx,inta[2])

{

inti=0,j=0;

node*temp;

temp=table->head;

for(i=1;i<=table->length;i++)

{

if(x==temp->data)

{a[1]=i;

j++;

}

temp=temp->link;

}

if(j==0)

{a[0]=0;

a[1]=0;}

else

a[0]=1;

}

voidmain()

{

listtable;

intm,a[2];

creat_list(&table);

show_list(&table);

printf("\n输入一个整数\n");

scanf("%d",&m);

search(&table,m,a);

if(a[0]==1)

{

delete_node(&table,a[1]);

show_list(&table);

}

else{

get(&table,m);

show_list(&table);

}

}

2_2(3)

#include

#include

#include

typedefstructNode{

intdata;

structNode*link;

}node;

typedefstructList{

node*head;

node*tail;

intlength;

}list;

voidcreat_list(list*table)

{

intx,i;

node*temp;

table->head=NULL;

table->tail=NULL;

table->length=0;

x=1;

scanf("%d",&x);

for(i=0;x!

=0;i++)

{

fflush(stdin);

printf("\n");

temp=(node*)malloc(sizeof(node));

temp->data=x;

temp->link=NULL;

temp->link=table->head;

table->head=temp;

table->length++;

scanf("%d",&x);

}

}

node*create_node(intnew_node)

{

node*temp;

temp=(node*)malloc(sizeof(node));

temp->data=new_node;

temp->link=NULL;

returntemp;

}

voidget(list*table,intn)

{

inti,m=0;

node*temp;

node*newnode;

temp=table->head;

table->length++;

newnode=create_node(n);

if(ndata)

{

newnode->link=temp;

temp=newnode;

}

else

for(i=1;m==0;i++)

{

if(i<=table->length&&temp->datalink->data)

{

newnode->link=temp->link;

temp->link=newnode;

m=1;

}

elseif(i==table->length)

{temp->link=newnode;

newnode->link=NULL;

m=1;

}

temp=temp->link;

}

table->length++;

}

voidshow_list(list*table)

{

intx,i;

node*temp;

printf("链表为:

\n");

temp=table->head;

for(i=1;i<=table->length;i++)

{

x=temp->data;

printf("%d",x);

temp=temp->link;

}

}

voidmain()

{

listtable;

intm,a[2];

creat_list(&table);

show_list(&table);

printf("\n输入一个整数\n");

scanf("%d",&m);

get(&table,m);

show_list(&table);

}

2_3

(1)

#include

#include

#include

typedefstructNode{

intdata;

structNode*link;

}node;

typedefstructList{

node*head;

node*tail;

intlength;

}list;

voidcreat_list(list*table)

{

intx,i;

node*temp;

table->head=NULL;

table->tail=NULL;

table->length=0;

x=1;

scanf("%d",&x);

for(i=0;x!

=0;i++)

{

fflush(stdin);

printf("\n");

temp=(node*)malloc(sizeof(node));

temp->data=x;

temp->link=NULL;

temp->link=table->head;

table->head=temp;

table->length++;

scanf("%d",&x);

}

}

node*create_node(intnew_node)

{

node*temp;

temp=(node*)malloc(sizeof(node));

temp->data=new_node;

temp->link=NULL;

returntemp;

}

voidget(list*table,intn)

{

inti,loc=table->length+1;

node*temp;

node*newnode;

temp=table->head;

table->length++;

newnode=create_node(n);

for(i=2;i

{

temp=temp->link;

}

newnode->link=temp->link;

temp->link=newnode;

}

voidshow_list(list*table)

{

intx,i;

node*temp;

printf("\n链表为:

\n");

temp=table->head;

for(i=1;i<=table->length;i++)

{

x=temp->data;

printf("%d",x);

temp=temp->link;

}

}

voiddelete_node(list*table,intn)

{

inti;

node*temp,*t;

temp=table->head;

if(n==1)

{

temp=temp->link;

table->head=temp;

}

if(n>2&&nlength)

{

temp=table->head;

for(i=2;i

temp=temp->link;

t=temp->link;

temp->link=t->link;

}

if(n==table->length)

{for(i=2;i

temp=temp->link;

temp->link=NULL;

}

if(n==2)

{

temp=table->head;

t=temp->link;

temp->link=t->link;

}

table->length--;

}

voidsearch(list*table,inta[2])

{

inti=0,j=0;

node*temp;

temp=table->head;

for(i=1;i<=table->length;i++)

{

if(temp->data<0)

{a[1]=i;

j=1;

break;

}

temp=temp->link;

}

if(j==0)

{a[0]=0;

a[1]=0;}

else

{a[0]=1;

}

}

voidpaixu(list*table)

{inti,j,m,u;

node*temp,*t,*p;

for(i=1;i<=table->length;i++)

{temp=table->head;

for(u=1;u

temp=temp->link;

t=temp;

for(j=i;j<=table->length;j++)

{

temp=t;

for(m=i;m<=j;m++)

temp=temp->link;

if(t->data<(temp->data))

{

p=temp->link;

temp->link=t->link;

t->link=p;

}

}

}

}

voidmain()

{

listtable;

intm,a[2];

creat_list(&table);

show_list(&table);

search(&table,a);

for(m=1;a[0]==1;m++)

{

search(&table,a);

if(a[0]==1)

{

delete_node(&table,a[1]);

}

}

show_list(&table);

}

2_3

(2)

#include

#include

#include

typedefstructNode{

intdata;

structNode*link,*last;

}node;

typedefstructList{

node*head;

node*tail;

intlength;

}list;

voidcreat_list(list*table)

{

intx,i;

node*temp,*t,*p;

table->head=NULL;

table->tail=NULL;

table->length=0;

x=1;

scanf("%d",&x);

for(i=0;x!

=0;i++)

{

fflush(stdin);

printf("\n");

temp=(node*)malloc(sizeof(node));

temp->data=x;

temp->link=NULL;

temp->link=table->head;

table->head=temp;

table->length++;

scanf("%d",&x);

}

t=(node*)malloc(sizeof(node));

t=table->head;

t->last=NULL;

for(i=2;i<=table->length;i++)

{

p=t;

t=t->link;

t->last=p;

}

}

voidshow_list(list*table)

{

intx,i;

node*temp;

printf("链表为:

\n");

temp=table->head;

for(i=1;i<=table->length;i++)

{

x=temp->data;

printf("%d",x);

temp=temp->link;

}

}

voidpaixu(list*table)

{

inti,j,m;

node*temp,*t,*p,*q;

temp=table->head;

t=temp;

for(i=2;i<=table->length;i++)

{

t=t->link;

if(t->data>temp->data)

{

p=t->link;

t->link=temp->link;

temp->link=p;

t->last->link=temp;

table->head=t;

t->last=NULL;

temp->last=t->last;

p=t;

t=temp;

temp=t;/**/

}

}

temp=table->head;

for(i=2;i<=table->length;i++)

{

temp=temp->link;

t=temp;

for(j=i+1;j<=table->length;j++)

{

t=t->link;

if(t->data>temp->data)

{

p=t->link;

t->link=temp->link;

temp->link=p;

t->last->link=temp;

temp->last->link=t;

p=t->last;

t->last=temp->last;

temp->last=p;

p=t;

t=temp;

temp=t;

}

}

}

}

voidmain()

{

listtable;

creat_list(&table);

show_list(&table);

paixu(&table);

show_list(&table);

}

三、测试数据*

输入:

2_2

1

3

2

-4

5

-2

9

-7

0

输入一个整数

3

输入一个整数

10

2_2(3)

7

5

4

3

2

1

0

链表为:

123457

输入一个整数

6

2_3

(1)

1

-2

3

-4

-7

9

5

0

2_3

(2)

1

-2

3

-4

-7

9

5

0

应输出(上机前自己分析的结果):

2_2

链表为:

79-25-4231

链表为:

-79-25-421

链表为:

-79-25-42110

2_2(3)

链表为:

1234567

2_3

(1)

链表为:

59-7-43-21

链表为:

5931

2_3

(2)

链表为:

59-7-43-21

链表为:

9531-2-4-7

四、上机时遇到的问题(可分为编译问题和逻辑问题)*

①问题现象:

冒泡排序时出现错误原因:

循环次数出现问题,少套一个循环;解决办法:

加一个循环

②问题现象:

删除负数时删错元素原因:

对于提供的删除元素位置处理不得当;解决办法:

通过调试中找到正确处理方法

五、实际运行结果:

2_2

链表为:

79-25-4231

链表为:

-79-25-421

链表为:

-79-25-42110

2_2(3)

链表为:

1234567

2_3

(1)

链表为:

59-7-43-21

链表为:

5931

2_3

(2)

链表为:

59-7-43-21

链表为:

9531-2-4-7

六、小结体会*:

整体做起来比较困难,需要加强对于链表的练习。

同时,思考对于2_2,若原链表中有多个需要处理的元素如何一并处理。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1