链表代码

/*
    > File Name: struct.c
    > Author: tth
    > Mail: 704299656@qq.com 
    > Created Time: 2020年12月26日 星期六 14时18分11秒
 ************************************************************************/

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


struct Node 
{
 int data;               //数据域
 struct Node* next;      //指针域
};

struct Node* createList()
{
	//火车头
	struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
	//此处malloc作用是向系统申请内存
	//初始化变量
	headNode->next=NULL;
	return headNode;
}

struct Node* createnode(int data)//创建结点区别在于多了数据域
{
	struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));//动态内存申请
	newNode->data=data;
	newNode->next=NULL;//初始化变量
}

void printList(struct Node* headNode)
{
	struct Node* pMove=headNode->next;//pMove就是装卸工人
	while(pMove)
	{
		printf("%d",pMove->data);//记录
		pMove=pMove->next;//向下一节车厢移动
	}
	printf("\n");
}

void insertNodeByhead(struct Node* headNode,int data)
{
	struct Node* newNode=createNode(data);//创建结点
	newNode->next=headNode->next;
	headNode->next=newNode;
}

void deleteNodeByAppoin(struct Node* headNode,int postData)
{
	struct Node* postNode = headNode->next;
	struct Node* postNodeFront = headNode;
	if(postNode == NULL){
		printf("无法删除链表为空");
		//printf("list is null");
	}  else {
		while (postNode->data != postData){
			postNodeFront = postNode;
			postNode = postNodeFront->next;
			if(postNode == NULL){
				printf("没有找到相关信息,无法删除\n");
				//printf("can not delete list");
				return;
			}
		}
		postNodeFront->next = postNode->next;
		free(postNode);
	}
}

int main(int argc,char *argv[])
{
	struct Node* list=createlist();
	insertNodeByhead(list,1);
	insertNodeByhead(list,2);
	insertNodeByhead(list,3);
	printList(list);
	deleteNodeByAppoin(list,2);
	printList(list);

	system("pase");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值