C语言实现简单单链表的初始化,创建,遍历

初始化,创建,遍历

#include "stdio.h"
#include "stdlib.h"


//定义链表节点的数据结构
typedef struct node
{
    int data;                           //链表节点的数据
    struct node *p_next;                //链表上指向下一个节点的指针,该指针类型为节点的结构体类型
}Node,*pNode; ;

pNode InitList();
pNode CreateList(pNode list);                     //返回的是指向整个node结构体的指针               
void ShowList(pNode list);                        //输出链表内容

int main(){
    pNode LIST;
    LIST = InitList();
    LIST = CreateList(LIST);               //头插创建
    printf("\n");
    ShowList(LIST);
    return 0;
}

pNode InitList(){
    pNode list=(pNode)malloc(sizeof(Node));
    if(list == NULL){
        printf("Failed in init\n");
        exit(-1);
    }
    list->p_next = NULL;
    return list;
}

pNode CreateList(pNode list){
    int i,length,data;
    pNode p_new;
    printf("Please input the node counts: ");
    scanf("%d",&length);
    printf("The list will contain %d nodes!\n",length);
    printf("Now,start create list=======\n");

    for (i=0;i<length;i++){
        printf("Please input Node.No%d data: ",i+1);
        scanf("%d",&data);                           
        p_new=(pNode)malloc(sizeof(Node));    //动态分配一个新节点的地址
        p_new->data = data;                         //赋值给新节点指针指向的节点数据成员
        p_new->p_next = list->p_next;               //将列表的指向下一个节点的地址指针赋值给新节点指针的地址
        list->p_next = p_new;                       //当前的列表的下个节点即为新节点                      
    }
    return list;
}
运行结果:
Please input the node counts: 3
The list will contain 3 nodes!
Now,start create list=======
Please input Node.No1 data: 1
Please input Node.No2 data: 2
Please input Node.No3 data: 3

3
2
1

插入

#include "stdio.h"
#include "stdlib.h"


//定义链表节点的数据结构
typedef struct node
{
    int data;                           //链表节点的数据
    struct node *p_next;                //链表上指向下一个节点的指针,该指针类型为节点的结构体类型
}Node,*pNode; ;

pNode InitList();
pNode CreateList(pNode list);                     //返回的是指向整个node结构体的指针
pNode InsertList(pNode list);                      
void ShowList(pNode list);                        //输出链表内容

int main(){
    pNode LIST;
    LIST = InitList();
    LIST = CreateList(LIST);               //头插创建
    InsertList(LIST);                      //插入
    printf("\n");
    ShowList(LIST);
    return 0;
}

pNode InitList(){
    pNode list=(pNode)malloc(sizeof(Node));
    if(list == NULL){
        printf("Failed in init\n");
        exit(-1);
    }
    list->p_next = NULL;
    return list;
}

pNode CreateList(pNode list){
    int i,length,data;
    pNode p_new;
    printf("Please input the node counts: ");
    scanf("%d",&length);
    printf("The list will contain %d nodes!\n",length);
    printf("Now,start create list=======\n");

    for (i=0;i<length;i++){
        printf("Please input Node.No%d data: ",i+1);
        scanf("%d",&data);                           
        p_new=(pNode)malloc(sizeof(Node));    //动态分配一个新节点的地址
        p_new->data = data;                         //赋值给新节点指针指向的节点数据成员
        p_new->p_next = list->p_next;               //将列表的指向下一个节点的地址指针赋值给新节点指针的地址
        list->p_next = p_new;                       //当前的列表的下个节点即为新节点                      
    }
    return list;
}

pNode InsertList(pNode list){
    int i=0;
    int num,data;
    pNode p_insert;
    printf("Where do you want to insert? n=\n");
    scanf("%d",&num);
    printf("What data do you want to insert? data=\n");
    scanf("%d",&data);

    while(list &&i<num){
        list=list->p_next;
        ++i;
    }
    if(!list||i>num){
        printf("Parameter illegal\n");
        exit(-1);
    }
    p_insert=(pNode)malloc(sizeof(Node));
    p_insert->data = data;
    p_insert->p_next = list->p_next;
    list->p_next = p_insert; 
}

void ShowList(pNode list)                               //输出链表内容
{
    pNode L = list->p_next;
    while(L!=NULL)
    {
        printf("%d\n",L->data);
        L=L->p_next;
    }
    printf("\n");
}
运行结果:
Please input the node counts: 3
The list will contain 3 nodes!
Now,start create list=======
Please input Node.No1 data: 1
Please input Node.No2 data: 2
Please input Node.No3 data: 3
Where do you want to insert? n=
2
What data do you want to insert? data=
5

3
2
5
1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kxwang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值