|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 16beat 于 2023-9-7 15:51 编辑
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void Insert(int data); //插入
- void Delete(int position); //删除
- void print(); //遍历链表
- struct Node
- {
- int Data;
- struct Node* NextNode;
- };
- struct Node* HeadNodePointer;
- void main()
- {
- int n;
- HeadNodePointer = NULL;
- Insert(1);
- Insert(2);
- Insert(3);
- Insert(4);
- Insert(5);
- print();
- Delete(2);
- print();
- }
- void Insert(int data)
- {
- struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
- (*temp).Data = data;
- (*temp).NextNode = HeadNodePointer;
- HeadNodePointer = temp;
- }
- void Delete(int position)
- {
- struct Node* last = HeadNodePointer;
- if (position == 1)
- {
- HeadNodePointer = (*last).NextNode;
- free(last);
- return;
- }
- for (int n = 0;n < (position - 2);n++)
- {
- last = (*last).NextNode;
- }
- struct Node* Current = (*last).NextNode;
- (*last).NextNode = (*Current).NextNode;
- free(Current);
- }
- void print()
- {
- struct Node* temp = HeadNodePointer;
- while (temp != NULL)
- {
- printf("%d ",(*temp).Data);
- temp = (*temp).NextNode;
- }
- }
复制代码
执行到Delete这个函数时会报错,显示
检测到堆损坏:CRT检测到应用程序在堆缓冲区结束后写入内存。
不明白为什么会出现这个问题。 |
|