#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//带头指针的单链表
typedef struct LNode{
int data;
struct LNode *next;
}LNode, *LinkList;
bool InitList(LinkList &L){
L = (LNode *) malloc(sizeof(LNode));
if(L== NULL){
return false; //内存不足分配失败
}
L->next =NULL;
return true;
}
bool empty(LinkList &L){
return (L->next==NULL);
}
LinkList List_headInsert(LinkList &L){
LNode *s ;
L->next =NULL;
int x;
scanf("%d",&x);
while(x!=9999){
s = (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
scanf("%d",&x);
}
return L;
}
int main(){
LinkList L;
InitList(L);
List_headInsert(L);
LNode *p = L->next;
while(p){
printf("%d \n",p->data);
p=p->next;
}
return 0;
}
头插建立单链表
带头指针单链表的初始化与插入操作
最新推荐文章于 2025-11-25 11:31:36 发布
文章介绍了如何使用C语言实现一个带头指针的单链表,包括链表的初始化函数InitList和在链表头部插入节点的List_headInsert函数,以及主函数中对链表进行遍历的示例。
4684

被折叠的 条评论
为什么被折叠?



