/*
> 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;
}
链表代码
最新推荐文章于 2023-05-15 11:48:50 发布