简介
指针本质是内存地址,是引用数据的另外一种方式。
指针可以多个输入的数据通过结构体和指针的方式连接在一起,形成一个单向链表!!!
闲话少说直接上代码!!!
#include<stdio.h>
#include<stdlib.h>
struct Node//节点结构体
{
int data;
struct Node* next;
};
int main()
{
int n, i;
scanf("%d", &n);
struct Node* list = NULL;//头节点
struct Node* tail = NULL;//尾节点
//创建节点
for (i = 0; i < n; i++)
{
int data;
scanf("%d", &data);
struct Node* n = (struct Node*)malloc(sizeof(struct Node));
n->data = data;
n->next = NULL;
//第一个节点为空指针
if (list == NULL)
{
//头尾都是相同节点
list = n;
tail = n;
}
else
{
tail->next = n;
tail = tail->next;
}
}
struct Node* cur = list;
//删除节点
int Del;
scanf("%d", &Del);
struct Node* prev = NULL;
while (cur)
{
if (cur->data == Del)
{
//如果为第一个节点的话
if (cur == list)
{
list = cur->next;
cur = list;//从当前位置继续寻找
}
else//不是第一个节点
{
prev->next = cur->next;
cur = prev->next;//从下一个位置继续寻找
}
n--;
}
//这边的prev和cur相邻
else
{
prev = cur;
cur = cur->next;
}
}
printf("%d\n",n);//打印节点个数
//打印节点
cur = list;
while (cur)//知道cur为NULL跳出循环
{
printf("%d ", cur->data);
cur = cur->next;
}
//释放空间
while (cur)
{
struct Node* pd = cur;
cur = cur->next;
free(pd);
}
return 0;
}
若对你有帮助,别忘了分享收藏 哦!Good Day!!!