8.C精华——链表一

静态链表

创建与遍历

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

void printNode(struct Test *head) {
	struct Test *p = head;
	while(p != NULL){	
		printf("%d \n",p->data);
		P = P->next;
	}
}

int main() {
	struct Test t1 = {1,NULL};
	struct Test t2 = {2,NULL};
	struct Test t3 = {3,NULL};
	struct Test t4 = {4,NULL};
	// 创建一个静态链表,将整个结构体的地址存放在 next 中
	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;

	// 遍历链表
	printNode(&t1);
	return 0;
}

统计节点个数

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

int getNumNode(struct Test *head) {
        int num = 0;
        struct Test *p = head;
        while(p != NULL){
                num++;
                p = p->next;
        }
        return num;
}

int main() {
        struct Test t1 = {1,NULL};
        struct Test t2 = {2,NULL};
        struct Test t3 = {3,NULL};
        struct Test t4 = {4,NULL};

        t1.next = &t2;
        t2.next = &t3;
        t3.next = &t4;

		// 统计链表节点个数
        int n = getNumNode(&t1);
        printf("number = %d\n",n);
        return 0;
}

添加节点——尾插与头插

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

struct Test *addNode1(struct Test *head,struct Test *new,int data){
        struct Test *p = head;
        if(head->data == data) {
                new->next = head;
                return new;
        }

        while(p->next != NULL){
                if(p->data == data) {
                        new->next= p->next;
                        p->next = new;
                        break;
                }
                p = p->next;
        }
        return head;
}

struct Test *addNode2(struct Test *head,struct Test *new,int data){
        struct Test *p = head;
        if(head->data == data) {
                new->next = head;
                return new;
        }

        while(p->next != NULL){
                if(p->next->data == data) {
                        new->next = p->next;
                        p->next = new;
                        break;
                }
                p = p->next;
        }
        return head;
}

void printNode(struct Test *head) {
        int num = 0;
        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 t3 = {3,NULL};
        struct Test t4 = {4,NULL};

        t1.next = &t2;
        t2.next = &t3;
        t3.next = &t4;

        struct Test new = {66,NULL};
        struct Test *head = &t1;


//      head = addNode1(head,&new,3);		// 尾插法
        head = addNode2(head,&new,3);		// 头插法
        printNode(head);
        return 0;
}

删除节点

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


struct Test *deleteNode(struct Test *head,int data){
        struct Test *p = head;
        if(head->data == data) {
                head = head->next;
                return head;
        }

        while(p != NULL){
                if(p->data == data) {
                        p->next = p->next->next;
                        break;
                }
                p = p->next;
        }
        return head;
}

void printNode(struct Test *head) {
        int num = 0;
        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 t3 = {3,NULL};
        struct Test t4 = {4,NULL};

        t1.next = &t2;
        t2.next = &t3;
        t3.next = &t4;

        struct Test *head = &t1;

        head = deleteNode(head,2);
        printNode(head);
        return 0;
}

更换节点

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


struct Test *changeNode(struct Test *head,struct Test *new,int data){
        struct Test *p = head;
        if(head->data == data) {
                new->next = head->next;
                return new;
        }

        while(p->next != NULL){
                if(p->next->data == data) {
                        new->next = p->next->next;
                        p->next = new;
                        break;
                }
                p = p->next;
        }
        return head;
}

void printNode(struct Test *head) {
        int num = 0;
        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 t3 = {3,NULL};
        struct Test t4 = {4,NULL};

        t1.next = &t2;
        t2.next = &t3;
        t3.next = &t4;

        struct Test *head = &t1;
        struct Test new = {88,NULL};

        head = changeNode(head,&new,2);
        printNode(head);
        return 0;
}

动态链表

尾插法

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

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

}

struct Test *addNode(struct Test *head,struct Test *new) {
        struct Test *p = head;
        if (head == NULL)
                return new;
        while (p->next != NULL)
                p = p->next;

        p->next = new;
        return head;
}

struct Test *creatNode(struct Test *head){
        int i;
        struct Test *p = head;
        struct Test *new;
        for (i = 0; i < 5; ++i) {
                new = (struct Test *)malloc(sizeof(struct Test));
                printf("数据%d:",i+1);
                scanf("%d",&(new->data));
                head = addNode(head,new);
        }
}

int main() {
        struct Test *head = NULL;
        head = creatNode(head);
        printNode(head);

        return 0;
}

头插法

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

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

}

struct Test *addNode(struct Test *head,struct Test *new) {
        struct Test *p = head;
        if (head == NULL) {
                head = new;
        } else {
                new->next = head;
                head = new;
        }
        return head;
}

struct Test *creatNode(struct Test *head){
        int i;
        struct Test *p = head;
        struct Test *new;
        for (i = 0; i < 5; ++i) {
                new = (struct Test *)malloc(sizeof(struct Test));
                printf("数据%d:",i+1);
                scanf("%d",&(new->data));
                head = addNode(head,new);
        }
}

int main() {
        struct Test *head = NULL;
        head = creatNode(head);
        printNode(head);

        return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值