#include <stdio.h>
#include<stdlib.h>
struct Node
{
int data; //数据域
struct Node* next; //指针域
};
struct Node* createList() //创建链表表头
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//headNode 成为了结构体变量
//变量使用前必须被初始化
//headNode->data = 1;
headNode->next = NULL;
return headNode;
}
struct Node* creatNode(int data) //创建结点
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printList(struct Node* headNode) //打印链表
{
struct Node* pMove = headNode->next;
while (pMove)
{
printf("%d\t", pMove->data);
pMove = pMove->next;
}
printf("\n");
}
void insertNodeByHead(struct Node* headNode, int data) //头插,插入结点,插入哪个结点,插入的数据是多少
{
//1.创建插入的节点
struct Node* newNode = creatNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteNodeByAppoin(struct Node* headNode, int posData) //删除指定位置结点
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
printf("无法删除链表为空\n");
else
{
while (posNode->data != posData)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("没有找到相关信息,无法删除\n");
break;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
int main()
{
//struct Node Node1 = { 1,NULL };
//struct Node Node2 = { 2,NULL };
//struct Node Node3 = { 1,NULL };
//Node1.next = &Node2;
//Node2.next = &Node3;
struct Node* list = createList();
insertNodeByHead(list, 1);
insertNodeByHead(list, 2);
insertNodeByHead(list, 3);
printList(list);
deleteNodeByAppoin(list, 2);
printList(list);
system("pause");
return 0;
}
49行这里有个提醒是什么意思?应该怎么修改?照着写的正确代码写的为什么我这不对?