单链表基本操作(1)

单链表基本操作

创建

  • 创建
  • 头插入加载
  • 尾插入加载

创建空链表

创建一个单链表,且为空表,并且返回链表
创建结构类型,在CS.c文件中


typedef struct node{
    int data;
    struct node *next;
}LINKLIST;

在Link.h写出方法声明


#include <stdio.h>
#include "CS.c"

/*
 创建一个空单链表
 */
LINKLIST *inill();

在Link.c中实现此方法

#include "Link.h"
LINKLIST *inill(){
    LINKLIST *L;
    L=(LINKLIST *)malloc(sizeof(LINKLIST));
    L->next=NULL;
    L->data=-1;
    return (L);
}

声明一个链表变量,并且分配空间,设置next指针域为NULL,data数据域为-1.

在main.c中调用此方法,并且进行判断

#include <stdio.h>
#include "Link.h"
int main(int argc, const char * argv[]) {

    //创建了空的链表
    LINKLIST *linklist=inill();
    printf("inill() method  create is a null  line %d \n",linklist->next==NULL);

    return 0;
}

根据定义:头结点的指针域存放指向首节点的指针,当链表为空时,首节点的指针为空,即:头结点的指针域为空.

打印数据:
inill() method create is a null line 1
所以为linklist为空链表

头插入加载

link.h

/*
 头插入加载
 */
LINKLIST * loadHead();

link.c

LINKLIST * loadHead(){
    LINKLIST *p,*Q;
    int x;

    Q=(LINKLIST *)malloc(sizeof(LINKLIST));
    Q->data=-1;
    Q->next=NULL;

    printf("please input the integer to inster\n");

    scanf("%d",&x);

    while (x!=-1) {
        p=(LINKLIST *) malloc(sizeof(LINKLIST));
        p->data=x;
        p->next=Q->next;
        Q->next=p;
        scanf("%d",&x);
    }
    return Q;
}

1.先创建一个空链表Q
2.然后从键盘输入 x
3.这里写图片描述

main.c

 //头插入
    LINKLIST *headInsertList=loadHead();

    printf("headInsertList=[");

    while (headInsertList!=NULL) {
        if(headInsertList->data==-1){
            printf("%d",headInsertList->data);
        }else{
            printf(",%d",headInsertList->data);
        }
        headInsertList=headInsertList->next;
    }
    printf("]\n");

打印结果:


please input the integer to inster
1
2
3
4
5
-1
headInsertList=[-1,5,4,3,2,1]

尾插入加载

link.h

/*
 尾插入加载
 */
LINKLIST * loadRear();

link.c


LINKLIST * loadRear(){
    LINKLIST *q,*Q,*R;
    Q=(LINKLIST *) malloc(sizeof(LINKLIST));
    R=Q;
    Q->data=-1;
    Q->next=NULL;
    int x;
    printf("please insert integer \n");
    scanf("%d",&x);
    while (x!=-1) {
        q=(LINKLIST *) malloc(sizeof(LINKLIST));
        q->data=x;
        q->next=NULL;
        Q->next=q;
        Q=q;
        scanf("%d",&x);
    }
    return R;
}

解释
1.创建了一个空链表Q,并且付给了R,
2.从键盘输入x
3.输入的数据不为-1
3.1创建一个空链表q
3.2设置q的数据域为x q->data=x;
3.3设置q的指针域为NULL
3.4设置Q的指针域为输入的q
3.5最后把刚创建的链表赋给Q,供下次再次插入的时候使用,这是关键的一步操作
4.最后把R返回,表示链表的头指针
这里就不做流程图了,就是按照的是头插入加载差不多

main.c

//尾插入

    LINKLIST *lastInsertList=loadRear();

    printf("lastInsertList=[");

    while (lastInsertList!=NULL) {
        if(lastInsertList->data==-1){
            printf("%d",lastInsertList->data);
        }else{
            printf(",%d",lastInsertList->data);
        }
        lastInsertList=lastInsertList->next;
    }
    printf("]\n");

打印结果:

please insert integer 

1
2
3
4
5
-1
lastInsertList=[-1,1,2,3,4,5]
Program ended with exit code: 0

源码下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值