数据结构--每天一题-单链表插入-头插
单链表插入-头插
单链表插入-头插,可以运行的!!!!!!
///9999为结束
tips:后缀必须cpp
套娃 43 2 1
头插法在头部插入
输出当然是反的,自己画图
// An highlighted block
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;
void PrintList(LinkList L)
{
L = L->next;//局部变量,主函数L不影响
while (L != NULL)
{
printf("%4d", L->data);
L = L->next;
}
printf("\n");
}
LinkList HeadCreatList(LinkList &L) {
LNode* s;
int x;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
scanf("%d", &x);
//3 4 5 6 9999
while (x != 9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;//L为头指针
L->next = s;
scanf("%d", &x);
//头插法在头插入,逆序。
}
//1,申请头结点空间
//2.插入新节点空间
//3.给数据
//4.操作
return L;
}
int main()
{
LinkList L;//链表头
LinkList search;
HeadCreatList(L);
PrintList(L);
}