链表-在链表中找到想找的值,并把想找的值摘取出来形成新的链表而原来的链表只是提出了想找的结点。以及链表的2种头插和2中尾插法

9 篇文章 1 订阅
3 篇文章 0 订阅

2种头插法的写法

//第一种方法
struct node_t* insertHead( struct node_t *h, struct node_t *n )
{
	n->next = h;
	return n;
}

//第二种方法
void  insertHead1( struct node_t **h, struct node_t *n )
{	
	n->next = *h;
	*h = n;
}

2种尾插法的写法

//第一种方法
struct node_t *insertTail( struct node_t *h , struct node_t *n )
{
	if( h == NULL ){
		return n;
	}
	
	struct node_t *p = h;
	while( p->next != NULL ){
		p = p->next;
	}
	p->next = n;
	return h;
}
//第二种方法
void insertTail1( struct node_t **h, struct node_t *n )
{
	while( *h ){
		h = &((*h)->next);
	}
	*h = n;
}

摘取想查找值的结点,并且形成新链表

struct node_t *findNode( struct node_t **h, int goal )
{
	struct node_t *newlist = NULL;
	// head
	while( (*h)->val == goal ){
		struct node_t *tmp = *h;
	
		*h = (*h)->next;
	
		tmp->next = NULL;
		insertTail1( &newlist, tmp );
		
	}
	struct node_t *t = *h;
	while( t->next ){
		struct node_t *p = t->next;
		if( p->val == goal ){
			struct node_t *tmp = p;
			t->next = p->next;
			tmp->next = NULL;
			insertTail1( &newlist, tmp );
		}else{
			t = t->next;
		}
	}
	return newlist;
}

总体程序

#include <stdio.h>
#include <stdlib.h>

struct node_t{
	int idx;
	int val;
	struct node_t *next;
};


void showList( struct node_t *h )
{
	printf("++++++++++++++++++++++++++++++\n");
	while( h ){
		printf("h->idx = %d, h->val = %d\n", h->idx, h->val );
		h = h->next;
	}
}

struct node_t* insertHead( struct node_t *h, struct node_t *n )
{
	n->next = h;
	return n;
}

void  insertHead1( struct node_t **h, struct node_t *n )
{	
	n->next = *h;
	*h = n;
}

struct node_t *insertTail( struct node_t *h , struct node_t *n )
{
	if( h == NULL ){
		return n;
	}
	
	struct node_t *p = h;
	while( p->next != NULL ){
		p = p->next;
	}
	p->next = n;
	return h;
}

void insertTail1( struct node_t **h, struct node_t *n )
{
	while( *h ){
		h = &((*h)->next);
	}
	*h = n;
}

struct node_t *findNode( struct node_t **h, int goal )
{
	struct node_t *newlist = NULL;
	// head
	while( (*h)->val == goal ){
		struct node_t *tmp = *h;
	
		*h = (*h)->next;
	
		tmp->next = NULL;
		insertTail1( &newlist, tmp );
		
	}
	struct node_t *t = *h;
	while( t->next ){
		struct node_t *p = t->next;
		if( p->val == goal ){
			struct node_t *tmp = p;
			t->next = p->next;
			tmp->next = NULL;
			insertTail1( &newlist, tmp );
		}else{
			t = t->next;
		}
	}
	return newlist;
}

int main()
{
	int arr[] = {10,10,9,8,5,20,10,10,30,20,10,10};
	struct node_t *head = NULL;
	int i = 0;
	struct node_t *newnode = NULL;
	while( i < sizeof(arr)/sizeof(int) ){	
		newnode = malloc( sizeof(struct node_t) );	
		newnode->val = arr[i];
		newnode->idx = i;
		newnode->next = NULL;
		//head = insertHead( head, newnode );
		//insertHead1( &head , newnode );	
		//head = insertTail( head, newnode );
		insertTail1( &head, newnode );
		i++;
	}
	
	struct node_t *newlist = findNode( &head, 10 );
	showList( head );	
	showList( newlist );
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值