链表c语言删除结点(【C语言编程】写一个函数del,删除动态链表中指定的结点)

本文目录
- 【C语言编程】写一个函数del,删除动态链表中指定的结点
- c语言中删除链表中的一个节点
- C语言如何删除链表头节点
- c语言 删除链表的某个节点
- C语言关于链表删除某个节点的问题,不知道写法,麻烦指点
- 创建链表,并删除链表中指定的结点的数据,用C语言实现
- c语言 删除链表头节点 望大神指教
- C语言,单向链表 不知道前驱指针,如何删除某一节点 情况考虑详尽一些!
【C语言编程】写一个函数del,删除动态链表中指定的结点
#include 《stdio.h》
#include 《malloc.h》
#include 《conio.h》
typedef struct node //定义节点
{
int value;
struct node* next;
}note;
note* head = NULL;
void del (note** head, int k)//删除链表
{
note* pp;
note* pt;
note* pq;
pp = *head;
if ((*head)-》value == k)//如果头结点的值等于k,删除头结点
{
*head = (*head)-》next;
return;
}
while(pp-》value != k)
{
pt = pp;
pq = pp-》next;
pp = pq;
}
pt-》next = pp-》next;//删除结点
}
void insert(note** head, int q)//建立链表
{
note* pp;
note* pt;
note* p = (note*)malloc(sizeof(note));
p-》value = q;
p-》next = NULL;
pp = *head;
if (*head==NULL)
{
*head=p;
return;
}
while(pp-》next!=NULL)
{
pt = pp-》next;
pp = pt;
}
pp-》next = p;
}
void print(note* head)//打印链表
{
note* pp;
while(head!=NULL)
{
printf("%d ", head-》value);
pp = head-》next;
head = pp;
}
}
int main()
{
int i;
int n,k,value;
scanf("%d %d",&n, &k);
for(i=0; i《n; i++)
{
scanf("%d", &value);
insert(&head, value); //把head的地址传过去
}
del(&head, k);
print(head);
getch();//随意按个键退出界面
return 0;
}
c语言中删除链表中的一个节点
temp=p;
p=p-》next;
temp-》next=NULL;
这三句存在问题,temp=p,让temp指向p所指向的节点,p=p-》next,p指向后移
temp-》next=NULL,让temp的后继为空,这里出了问题,链表从temp指向的节点断开,相当于删除p之后的所有节点。
应该先判断p是不是最后节点
if(p-》next==NULL)
如果是,只好去找p的前趋pre,让pre-》next=NULL,free(p)
如果不是最后节点,将p的后继节点数值域复制给p,然后将p的后继节点删除,等同与删除p
p-》data=p-》next-》data;
p-》next=p-》next-》next;
free(p);
C语言如何删除链表头节点
这种删除方法是头节点存放值的,这样可以清楚的看到是否删除掉了头节点。
用p保存头节点 p=head;
head指向下一个节点,成为新的头节点 head=p-》next;
释放原来的头节点 free(p);
#include《iostream》
#include《stdio.h》
#include 《stdlib.h》
void printList(struct node *head);
struct node* delHead(struct node *head);
struct node{
int data;
struct node* next;
};
int main(){
int i;
struct node *tail,*head,*p;
//尾插法插入数据
p=(struct node*)malloc(sizeof(struct node));
p-》data=0;
tail=head=p;
tail-》next=NULL;
for(i=1;i《10;i++){
p=(struct node*)malloc(sizeof(struct node));
tail-》next=p;
p-》data=i;
p-》next=NULL;
tail=p;
}
printList(head);
head=delHead(head);
printList(head);
system("pause");
return 0;
}
//删除头结点
struct node* delHead(struct node *head){
struct node *p = head;
head=p-》next;
free(p);
return head;
}
//打印链表
void printList(struct node *head){
struct node *p = head;
while (p != NULL){
printf("%i ", p-》data);
p = p-》next;
}
printf("\n");
}
c语言 删除链表的某个节点
temp=p;
p=p-》next;
temp-》next=NULL;
这三句存在问题,temp=p,让temp指向p所指向的节点,p=p-》next,p指向后移
temp-》next=NULL,让temp的后继为空,这里出了问题,链表从temp指向的节点断开,相当于删除p之后的所有节点。
应该先判断p是不是最后节点
if(p-》next==NULL)
如果是,只好去找p的前趋pre,让pre-》next=NULL,free(p)
如果不是最后节点,将p的后继节点数值域复制给p,然后将p的后继节点删除,等同与删除p
p-》data=p-》next-》data;
p-》next=p-》next-》next;
free(p);
C语言关于链表删除某个节点的问题,不知道写法,麻烦指点
struct node *delete(struct node* head)//删除函数
{
printf("请输入要删除的学生姓名");
char k;
scanf("%s", k);
struct node *pre = NULL;
struct node *q = head;
while (q) {
if (strcmp(q-》data.name, k) == 0){
if (pre)
pre-》next = q-》next;
else
head = q-》next;
free(q);
break;
}
pre = q;
q = q-》next;
}
return head;
}
创建链表,并删除链表中指定的结点的数据,用C语言实现
#include 《stdio.h》
#include 《time.h》
#include 《stdlib.h》
typedef struct node
{
int a;
struct node *next;
} NODE;
void main()
{
NODE * create(), *head;
NODE * SearchNode(NODE *head, int n);
void display(NODE *head);
NODE * DeleteNode(NODE *head, int n);
int n;
head = create();
if (head != NULL)
{
printf ("链表内容:\n");
display(head);
printf ("输入要删除的结点:");
scanf ("%d", &n);
head = DeleteNode(head, n);
if (head != NULL)
display(head);
else
printf ("链表被删除空了!\n");
}
else
printf ("链表为空!\n");
}
NODE * create()
{
NODE *head, *p, *q;
int a;
head = q = NULL;
srand ((unsigned) time (NULL));
while (1)
{
a = rand () % 100;
if (a != 30)
{
p = (NODE *) malloc (sizeof (NODE));
p-》a = a;
if (head == NULL)
head = p;
else
q-》next = p;
q = p;
}
else if (30 == a)
break;
}
if (q != NULL)
q-》next = NULL;
return head;
}
void display(NODE *head)
{
NODE *h;
h = head;
while (h != NULL)
{
printf ("%3d", h-》a);
h = h-》next;
}
putchar (10);
}
NODE * SearchNode(NODE *head, int n)
{
NODE *prev, *h;
h = head;
while (h != NULL && h-》a != n)
{
prev = h;
h = h-》next;
}
return prev;
}
NODE *DeleteNode(NODE *head, int n)
{
int count = 0, flag = 0, num = 0;
NODE *prev, *h;
h = head;
while (h != NULL)
{
if (h-》a == n)
count++;
h = h-》next;
num++;
}
if (count == 0)
{
printf ("抱歉,没有找到要删除的结点!\n");
exit (0);
}
else
{
if (num == count)
count--;
while (head-》a == n && count != 0)
{
h = head;
head = head-》next;
free (h);
count--;
flag = 1;
}
if (count != 0 )
{
while (count--)
{
prev = SearchNode(head, n);
h = prev-》next;
prev-》next = h-》next;
free (h);
}
}
else if (head-》a == n)
{
printf ("链表被删除空了!\n");
free (head);
exit (1);
}
}
return head;
}
c语言 删除链表头节点 望大神指教
1、对于有头结点(该结点不存储数据)的链表,删除某个结点容易操作。
int EraseNode(LIST *head,type data) {
LIST *q,*p;
for(p = head; p-》next; p = p-》next) {
if(p-》next-》data == data) { /* 满足删除条件 */
q = p-》next;
p-》next = q-》next;
free(q);
return 1;
}
}
return 0; /* 没有找到满足条件的结点,失败返回 */
}
2、对于没有头结点的链表,操作方法有些不同。
LIST *EraseNode(LIST *head,type data) { /* 返回链表头指针 */
LIST *q,*p;
if(head-》data == data) { /* 先处理头结点 */
q = head;
p = q-》next;
free(q);
return p;
}
for(p = head; p-》next; p = p-》next) {
if(p-》next-》data == data) { /* 满足删除条件 */
q = p-》next;
p-》next = q-》next;
free(q);
return head;
}
}
return head; /* 没有找到满足条件的结点,失败返回 */
}
C语言,单向链表 不知道前驱指针,如何删除某一节点 情况考虑详尽一些!
p非尾结点:用p的下一节点q代替p(p-》data=q-》data,p-》next=q-》next),然后删除q节点。
p是尾结点:从头结点开始遍历整个链表直到倒数第二个节点q,令q-》next=NULL,然后删除p节点。

本文相关文章:
找不到datedif函数(excel中的datedif这个函数在哪里)
2026年9月1日 13:40
plot函数怎么定义颜色(matlab中plot函数一共能调用多少种颜色)
2026年8月24日 04:30
sendmessage函数使用方法(SendMessage()函数的 第二、三、四个参数是什么 怎么用)
2026年8月23日 19:20
round if函数公式怎么用(round和IF函数怎么用)
2026年7月31日 06:50
php函数参数传递方式三种(php中构造函数的参数是接口类型,在实例化一个对象时这个参数怎么传)
2026年6月30日 09:40
更多文章:
form表单制作(为什么制作的form表单会在网页显示中多出一行)
2026年10月11日 09:10
teammate(teammate,company,partner)
2026年10月11日 06:10
javascript arraybuffer(javascript可以把base64编码转换成二进制代码吗求示例代码!)
2026年10月11日 04:00
text函数公式(excel中round和text函数的区别是什么)
2026年10月11日 03:50
google chrome打不开(chrome浏览器打不开怎么回事 浏览器打不开的处理方法)
2026年10月11日 02:00
websocket整合springboot(Springboot整合Websocket遇到的坑)
2026年10月11日 01:40
drawerlayout(android 怎样让drawerlayout设置的侧滑菜单的内容充满屏幕)
2026年10月10日 19:20
xor四位数怎么运算(单片机怎样用C语言实现4个数字间的异或)
2026年10月10日 17:50


