C语言链表--链表结点删除(4)

论坛 期权论坛 脚本     
已经匿名di用户   2022-7-2 21:50   1803   0

1、

例子:删除中间结点3

要找到删除结点的前一个结点

struct node
{
    int data;
    struct node *next;
};
struct node *p,*q;
q = (struct node *)malloc(sizeof(struct node));
q = head -> next ;
p = q -> next;

q -> next = p -> next ;
free(p);

若没有指针P,只有q,那么

struct node
{
    int data;
    struct node *next;
};
struct node *q;
q = (struct node *)malloc(sizeof(struct node));
q = head -> next ;

q -> next = q -> next -> next ;
free(q->next);

(2) 删除头结点

struct node
{
    int data;
    struct node *next;
};  
struct node *p;  //定义指针p使其指向要删除的头结点
p = (struct node *)malloc(sizeof(struct node));
p = head  ;
haed = head -> next; //头指针下移
free(p);


(3)删除为结点

struct node
{
    int data;
    struct node *next;
};  
struct node *p;  //定义指针p使其指向要删除的头结点
p = (struct node *)malloc(sizeof(struct node));
p = q -> next  ;
q -> next = NULL ;  //删除尾结点

free(p);

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP