C语言静态链表(指定节点的前后插入新节点与删除)

静态链表相对动态链表,(基于你指针和结构体,内存有清晰的认识的情况下)比较简单,下面是小生的练习代码,提供给大家。

简单的链表样式

#include <stdio.h>
#include <stdlib.h>
struct test
{
	int data;
    struct test *next;
};

void prinlink(struct test *head)
{
	struct test *p = head;
    while(p != NULL){
		printf("%d ",p->data);
        p = p->next;
    }
}

int main()
{
	struct test t1={1,NULL};
	struct test t2={2,NULL};   
    struct test t8={100,NULL}; 
    struct test t3={3,NULL};
    struct test t4={4,NULL};
    
    t1.next = &t2;
	t2.next = &t8;
    t8.next = &t3;
    t3.next = &t4;
    
    prinlink(&t1);
    
	system("pause");
	return 0;
}

统计链表节点个数及查找链表

#include <stdio.h>
#include <stdlib.h>
struct test
{
	int data;
    struct test *next;
};
void prinlink(struct test *head)
{
	struct test *p = head;
    while(p != NULL){
		printf("%d ",p->data);
        p = p->next;
    }
	   putchar('\n');
}

int  chazhao(struct test *head) //查找节点个数
{
	int cnt = 0;
	struct test *p = head;
	while(p != NULL){
		cnt++;
		p = p->next;
    }
	return cnt;
}

int chashu(struct test *head,int data) //查找有没有这个点
{
	struct test *p = head;
    while(p != NULL){
		if(p->data == data){
			return 1;
        }
        p = p->next;
    }
    return 0;
}

int main()
{
	struct test t1={1,NULL};
    struct test t2={2,NULL};
    struct test t3={3,NULL};
    struct test t4={4,NULL};
    struct test t5={5,NULL};
    
	t1.next = &t2;
    t2.next = &t3;
    t3.next = &t4;
    t4.next = &t5;
    
	prinlink(&t1);
 
    
    int ret;
    ret = chazhao(&t1);
    printf("ret = %d",ret);
    putchar('\n');

	int pon;
	pon = chashu(&t1,3);
    if(pon == 1){
		printf("have 3");
    }else{
		printf("no have 3");
    }
    putchar('\n');  
    pon = chashu(&t1,9);
    if(pon == 1){
		printf("have 9");
    }else{
		printf("no have 9");
    }    
    
	system("pause");
	return 0;
}

链表从指定节点后面插入新节点

#include <stdio.h>
#include <stdlib.h>
struct test 
{
	int data;
    struct test *next;
};

void prinlink(struct test *head)
{
	struct test *p = head;
    while(p != NULL){
		printf("%d ",p->data);
        p = p->next;
    }
    putchar('\n');
}

int houm(struct test *head,int data,struct test *new)
{
	struct test *p = head;
    while(p != NULL){
		if(p->data == data){
			new->next = p->next;
            p->next = new;
            return 1;
        }
        p = p->next;
    }
    return 0;
}

int main()
{
	struct test t1 = {1,NULL};
    struct test t2 = {2,NULL};
    struct test t3 = {3,NULL};
    struct test t4 = {4,NULL};
    struct test t5 = {5,NULL};
    
    struct test t6 = {6,NULL};
    
    t1.next = &t2;
    t2.next = &t3;
    t3.next = &t4;
    t4.next = &t5;
    
	prinlink(&t1);
    putchar('\n');
    
    houm(&t1,3,&t6);
    prinlink(&t1);
    
	system("pause");
	return 0;
}

链表从指定节点前面插入新节点

#include <stdio.h>
#include <stdlib.h>
struct test
{
	int data;
    struct test *next;
};

struct test *qianm(struct test *head,int data,struct test *new)
{
	struct test *p = head;
		if(p->data == data){
			new->next = new;
			return new;
		}	
    while(p->next != NULL){
		if(p->next->data == data){
			new->next = p->next;
            p->next = new;
            return head;
        }
		p = p->next;
    }
    return 0;
}

void prinlink(struct test *head)
{
	struct test *p = head;
    while(p != NULL){
		printf("%d ",p->data);
		p = p->next;
    }
}

int main()
{
	struct test *head = NULL;

	struct test t1 = {1,NULL};
    struct test t2 = {2,NULL};
    struct test t3 = {3,NULL};
    struct test t4 = {4,NULL};
    struct test t5 = {5,NULL};
    
    
    t1.next = &t2;
    t2.next = &t3;
    t3.next = &t4;
    t4.next = &t5;
    
    head = &t1;
    struct test new = {999,NULL};
    head = qianm(head,5,&new);
    prinlink(head);

	putchar('\n');
    
    struct test new2 = {555,NULL};
    head = qianm(head,999,&new2);
    prinlink(head);
    
    
	system("pause");
	return 0;
}

链表中节点的删除

#include <stdio.h>
#include <stdlib.h>
struct test
{
	int data;
    struct test *next;
};

void prinlink(struct test *head)
{
	struct test *p = head;
    while(p != NULL){
		printf("%d ",p->data);
		p = p->next;
    }
}

struct test *shanchu(struct test *head,int data)
{
	struct test *p = head;
	if(p->data == data){
		head = head -> next;
        free(p);
        return head;
    }
    
	while(p->next != NULL){
		if(p->next->data == data){
			p->next  = p->next->next;
            return head;
        }
        p = p->next;
    }
	return 0;
}

int main()
{
	struct test *head = NULL;

	struct test t1 = {1,NULL};
	struct test t2 = {2,NULL};
    struct test t3 = {3,NULL};
    struct test t4 = {4,NULL};
    struct test t5 = {5,NULL};
    
		
    
    t1.next = &t2;
    t2.next = &t3;
    t3.next = &t4;
    t4.next = &t5;
    	
	head  = &t1;
        
	prinlink(head);

	putchar('\n');
        
	head = shanchu(head,3);
    prinlink(head);
    
    putchar('\n');
   
        
	system("pause");
	return 0;
}

小结

函数名起的不是很好,见谅o( ̄▽ ̄)d 。
代码里面有一些小算法,看不懂可以多看几遍o( ̄▽ ̄)d 。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值