C语言—头节点链表与无头节点链表

头指针链表与头节点链表

二者差异

  • 头指针是指指向开始结点的指针(没有头结点的情况下)。一个单链表可以由其头指针唯一确定,一般用其头指针来命名单链表
  • 头结点是在链表的开始结点之前附加的一个结点。可以使得无论链表是否为空,头节点总是非空

头节点优点

  • 对于头指针链表,有了头结点后,对在第一个元素结点前插入结点和删除第一个结点,其操作与对其它结点的操作统一了。

以下分别为对头指针链表进行增删插和对头节点增删插操作的代码

头节点:

#include<stdio.h>
#include<stdlib.h>

struct NODE{
    int date;
    struct NODE *next;
};
typedef struct NODE *node;
int count=0;

node creat(){
    node pHead,pEnd,pNew;
    pHead=(node)malloc(sizeof(struct NODE));
    printf("请输入节点个数:");
    scanf("%d",&pHead->date);
    pHead->next=NULL;
    pEnd=pHead;
    while(count<pHead->date){                                       //尾插法创建头节点型链表 
        pNew=(node)malloc(sizeof(struct NODE));
        count++;
        printf("输入第%d个节点的数据:",count);
        scanf("%d",&pNew->date);
        pNew->next=NULL;
        pEnd->next=pNew;
        pEnd=pNew;
    }
    return pHead;   
}

void print(node pHead){
    node pTemp;
    pTemp=pHead->next;
    int index=1;
    while(pTemp!=NULL){
        printf("第%d个节点数据:",index);
        printf("%d \n",pTemp->date);
        pTemp=pTemp->next;
        index++;
    }
}

node insert(node pHead){
    int index;
    node pPre,pNew;
    pPre=pHead;
    printf("请输入要插入的节点位置:");
    scanf("%d",&index);
    for(int i=1;i<index;i++)
       pPre=pPre->next;
    pNew=(node)malloc(sizeof(struct NODE));
    printf("请输入要插入的节点数据:");
    scanf("%d",&pNew->date);
    pNew->next=pPre->next;
    pPre->next=pNew;
}

node _delete(node pHead){
    node pTemp,pd;
    pTemp=pHead->next;
    int index;
    printf("请输入要删除的节点序号:");
    scanf("%d",&index);
    for(int i=1;i<index;i++)
      pTemp=pTemp->next;
      pd=pTemp->next;
    pTemp->date=pTemp->next->date;                     //用后一个节点的数据代替上一个节点的数据,时间复杂度O(1) 
    pTemp->next=pTemp->next->next;
    free(pd);
    return pHead;
}

int main(){
    node pHead;
    pHead=creat();
    print(pHead);
    pHead=insert(pHead);
    print(pHead);
    pHead=_delete(pHead);
    print(pHead);
}

头指针:

#include<stdio.h>
#include<stdlib.h>

struct NODE{
    int date;
    struct NODE *next;
}; 
typedef struct NODE *node;
int count=0;

node creat(){
    node pHead,pEnd,pNew;
    int index;
    printf("请输入节点个数");
    scanf("%d",&index);
    pHead=NULL;
    pEnd=pHead;
    while(count<index){
        pNew=(node)malloc(sizeof(struct NODE));
        count++;
        printf("第%d个节点:",count);
        scanf("%d",&pNew->date);
        pNew->next=NULL;
        if(count==1){
            pHead=pNew;
            pEnd=pNew;
        }
        else{
            pEnd->next=pNew;
            pEnd=pNew;
        }
    }
    return pHead;
}

void print(node pHead){
    node pTemp;
    pTemp=pHead;
    int index=1;
    while(pTemp!=NULL){
        printf("第%d个节点数据:",index);
        printf("%d\n",pTemp->date);
        pTemp=pTemp->next;
        index++;
    }
}

node insert(node pHead){
    node pPre,pNew;
    int index;
    pPre=pHead;
    printf("请输入要插入的节点位置:");
    scanf("%d",&index);
    pNew=(node)malloc(sizeof(struct NODE));
    printf("请输入要插入的节点数据:");
    scanf("%d",&pNew->date);
    if(index==1){
        pNew->next=pHead;
        pHead=pNew;
    }
    else{
        for(int i=1;i<index-1;i++)
           pPre=pPre->next;
        pNew->next=pPre->next;
        pPre->next=pNew;   
    }
    return pHead;
}

node _delete(node pHead){
    node pPre,pTemp;
    pPre=pHead;
    int index;
    printf("请输入要删除的节点序号:");
    scanf("%d",&index);
    if(index==1){
        pHead=pPre->next;
        free(pPre);
    }
    else{
        for(int i=1;i<index-1;i++)
          pPre=pPre->next;
        pTemp=pPre->next;
        pPre->next=pTemp->next;
        free(pTemp);
    }
        return pHead;
}

int main(){
    node pHead;
    pHead=creat();
    print(pHead);
    pHead=insert(pHead);
    print(pHead);
    pHead=_delete(pHead);
    print(pHead);
}
  • 对于初学者,刚开始学习链表的时候有些难以理解,其实多看书,边敲代码边画图,就能快速掌握其中的精髓
  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值