首页 > 编程知识 正文

创建带头结点的单链表,c语言头节点怎么定义

时间:2023-05-06 15:11:45 阅读:117323 作者:1808

初始化

先知道头的节点

头节点是一个特殊节点,其数据字段中没有存储信息,通常头指针指向的节点是头节点,头节点没有存储信息,因此第一个实际节点而不是数据结构中的实际节点

那么头部节点的作用是什么

不插入开头节点链接表

插入第一个节点链表

有了头部节点,可以看到无论在链表的哪个位置执行插入操作都是一样的。 因此,头节点的作用是“统一操作”。 值得注意的是,除非删除链表,否则头部节点将继续存在。

插入操作也是如此

删除第一个节点链表的操作

删除第一个节点链表的操作

理解头部节点后,让我们看看头部节点的设定方法,也就是初始化

不进行起始节点链表的初始化

代码

//初始化,头节点*初始化列表(note * l ) {返回空值; }初始化第一个节点链表

代码

//初始化,创建头节点*初始化列表(note* ) note * (malloc ) sizeof (note ) ); //为头部节点分配空间L-next=NULL; 在//head-node指针字段中放置空的返回l; }创建链表

用插头法编制链表代码

//末尾插入法不指定个数,而是指定开头节点的单链表note *createList () ({ int i; 数据类型数据; note*L=initlist(L ),*n; //分别定义头部指针、新指针data=getchar (; wile (数据!='# ' ()//使用“#”作为终结符() n=) note * (malloc ) sizeof (note ) ); //为新节点申请空间n-data=data; n-next=L-next; //新指针的指针字段是头指针的下一个节点地址,L-next=n; //将新指针链入头部节点后,单击getchar (; scanf('%c ',data ); }返回l; }

用尾插法编写链表代码

//末尾插入法不指定个数,而是指定开头节点的单链表note *createList () ({ int i; 数据类型数据; note*L=initlist(L )、*r、*n; //末尾指针分别定义了开头指针、末尾指针、新的指针r=L末尾指针被初始化为开头指针data=getchar (; wile (数据!='# ' ()//使用“#”作为终结符() n=) note * (malloc ) sizeof (note ) ); //为新节点申请空间n-data=data; n-next=空; //新指针的指针字段包含空的r-next=n; //将新指针链入单链表末尾的r=r-next; //末尾的指针是getchar (向后移动; scanf('%c ',data ); }返回l; )插头法和插尾法的区别

当然,也可能需要指定节点数来创建链表。 以下是适当的代码。 读者可以自己比较

末尾插入法链表的创建(指定节点数)代码

//尾插入法创建指定的个数,开头节点的单链表note *createList (() { int len; printf (元素个数); 扫描(' % d ',len ); printf (请用'空格分隔); 数据类型数据; note*L=initlist(L )、*r、*n; //分别定义了开头指针、结尾指针、新的指针r=L的//结尾指针被初始化为开头指针for (inti=0; i len; I )扫描(“% d”,数据); n=(note* ) malloc ) sizeof (note ); //申请用于新节点的空间的n-data=data; n-next=空; //新指针的指针字段包含空的r-next=n; //将新指针链入单链表末尾的r=r-next; //将末尾指针向后移动} return L; ) ) /搜索单链表中的第I个节点,返回该节点的地址not

e *find(note *L,int i ){ int j = 0; note *p = L; if (i < 0) { printf("该结点不存在"); } else if( i == 0) { return p ;//此时,p指向头结点 } while ( p && i!=j) { p = p->next; j++; } return p ;} 插入 //插入note *insert(note *L){ int pos ; datatype x; printf("请输入插入的位置和元素(用空格间隔):"); scanf("%d %c",&pos,&x); note *p; note *q; p = find(L,pos); if(p) { q = (note*)malloc(sizeof(note));//为要插入的结点申请空间 q->data = x; q->next = p->next; p->next = q; return L; } else { printf("该结点不存在"); return L; }} 删除 //查找单链表中第i个结点,并且返回该结点的地址note *find(note *L,int i ){ int j = 0; note *p = L; if (i < 0) { printf("该结点不存在"); } else if( i == 0) { return p ;//此时,p指向头结点 } while ( p && i!=j) { p = p->next; j++; } return p ;} 输出链表 void printLinkList(note *L){ note *p; p = L->next; while(p) { printf("%d ",p->data); p = p->next; }}

这里需要注意

输出链表肯定是要输出实际结点

对于无头节点的链表,从head指向的结点开始输出(p = L)

对于有头结点的链表,需要从head->next指向的结点开始输出(p=L->next)

完整代码

#include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef char datatype;//定义单链表结构typedef struct note_list{ datatype data;//数据域 struct note_list *next;//指针域,指向下一节点}note;//函数声明note *initList();note *createList();note *find();note *insert();note *delete();void printLinkList();int main(){ note *L; printf("请输入元素,用空格间隔,用“ # ”结尾:"); L = createList(); insert(L); delete(L); printf("nL链表所有元素:"); printLinkList(L); return 0;}//初始化,创建头结点note *initList(note *L){ L = (note*) malloc(sizeof(note));//为头结点分配空间 L->next = NULL;//头结点指针域置空 return L;}//尾插法创建不指定个数,带头结点单链表//尾插法创建不指定个数,带头结点单链表note *createList(){ int i; datatype data ; note *L = initList(L), *r, *n;//分别定义头指针、尾指针、新指针 r = L;//尾指针初始化为头指针 data = getchar(); while (data != '#')//以“#”作为结束符 { n = (note*) malloc(sizeof(note));//为新结点申请空间 n->data = data ; n->next = NULL;//新指针指针域置空 r->next = n;//将新指针链入单链表末尾 r = r->next;//尾指针往后移 getchar();//吃掉空格 scanf("%c",&data); } return L;}//查找单链表中第i个结点,并且返回该结点的地址note *find(note *L,int pos ){ int j = 0; note *p = L; if (pos < 0) { printf("该结点不存在"); } else if( pos == 0) { return p ;//此时,p指向头结点 } while ( p && pos!=j) { p = p->next; j++; } return p ;}//插入note *insert(note *L){ int pos ; datatype x; printf("请输入插入的位置和元素(用空格间隔):"); scanf("%d %c",&pos,&x); note *p; note *q; p = find(L,pos); if(p) { q = (note*)malloc(sizeof(note));//为要插入的结点申请空间 q->data = x; q->next = p->next; p->next = q; return L; } else { printf("该结点不存在"); return L; }}//删除操作note *delete(note *L){ int pos; printf("请输入要删除元素的位置"); scanf("%d",&pos); note *q; note *pre;//记录要删除结点的前一个结点 datatype data; q = find(L,pos); if(!q) { printf("此链表不存在该结点,故不能删除"); return L; } data = q->data;//记录要删除链结点的数据域 pre = find(L,pos-1);//返回要删除结点的前一个结点的地址 pre->next = q->next; free(q);//释放空间 printf("%c已删除",data); return L;}//输出链表void printLinkList(note *L){ note *p; p = L->next; while(p) { printf("%c ",p->data); p = p->next; }}

结果 

写在最后:

写这篇文章只是整理学习笔记,没有独到的见解,读者看到不合理之处还望批评指正

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。