c语言创建非空链表,编程题,急求 已知非空线性链表head,编写程序,将链表中数据域值最小的那个结点移至表的最前面。要求:不得额外申请新的结点。...

满意答案

02ae427d08e371d7e90d5b995e828d6d.png

sjhgs808

2014.07.06

02ae427d08e371d7e90d5b995e828d6d.png

采纳率:58%    等级:11

已帮助:7202人

这是我在以前写的代码的基础上修改的,main里完成的就是找出最小值节点然后插入表头:

#include

#include

typedef struct _list {

int val;

struct _list* next;

} *node, list;

node Insert( node* head, node pos, int val )

{

node tmp;

tmp = ( node )malloc( sizeof( list ) );

tmp->val = val;

tmp->next = pos ? pos->next : *head;

if ( pos ) {

pos->next = tmp;

} else {

*head = tmp;

}

return tmp;

}

node Create( int* beg, int* end )

{

node head, t;

head = t = NULL;

while ( beg != end ) {

t = Insert( &head, t, *beg++ );

}

return head;

}

void Print( node head )

{

while ( head ) {

printf( "%d ", head->val );

head = head->next;

}

putchar( '\n' );

}

int main()

{

int a[] = { 6,1,3,9,2,8,5,0,7,4 };

node head, min, prev, next;

head = Create( a, a + 10 );

Print( head );

min = prev = next = head;

while ( next->next ) {

if ( min->val > next->next->val ) {

prev = next;

min = next->next;

}

next = next->next;

}

prev->next = min->next;

min->next = head;

head = min;

Print( head );

getchar();

return 0;

}

10分享举报

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值