静态链表-新片

.h文件声明

//
// Created by sanzhixiong on 2017/10/31.
//

#ifndef DAY14_LINKLIST_H
#define DAY14_LINKLIST_H

//节点操作
typedef struct Node
{
    int data;
    struct Node *pNext;
}*pNode;

//创建头
pNode CreateLinkHead();
//打印
void PrintLinkList(pNode pHead);
//插入
void install_List(pNode pHead,int num,int value);
//删除
void del_list(pNode pHead,int num);

#endif //DAY14_LINKLIST_H

.c文件

//
// Created by sanzhixiong on 2017/10/31.
//
#include "LinkList.h"
#include <stdio.h>
#include <stdlib.h>

//节点的操作
pNode CreateLinkHead()
{
    pNode pHand = NULL;
    pNode pTitle = NULL;
    int len;
    int i;
    int value;
    pHand = (pNode)malloc(sizeof(struct Node));
    if(NULL == pHand)
    {
        printf("CreateLinkHead malloc is error\n");
        exit(-1);
    }

    printf("请输入节点个数\n");
    scanf("%d",&len);
    if(len<0)
    {
        printf("拜托你选择一个正确的选择啊必须要大于0的数\n");
    }

    pTitle = pHand;
    pHand->pNext = NULL;
    for (i = 0; i <len ; ++i)
    {
        pNode p = (pNode)malloc(sizeof(struct Node));
        if(NULL == p)
        {
            printf("malloc is error\n");
            exit(-1);
        }
        else
        {
            printf("请输入节点的数据\n");
            scanf("%d",&value);
            //核心
            p->data = value;
            pTitle->pNext = p;
            p->pNext = NULL;
            pTitle = p;
        }
    }
    return pHand;
}

//打印函数
void PrintLinkList(pNode phead)
{
    pNode head = NULL;
    if(NULL == phead)
    {
        printf("PrintLineList");
        exit(-1);
    }
    head = phead;

    while(head!=NULL)
    {
        printf("打印出数据%d\n",head->data);
        head = head->pNext;
    }
}

//插入函数
void install_List(pNode pHead,int num,int value)
{
    int i = 0;
    if(pHead == NULL && num <0 && value<0)
    {
        printf("install list is error\n");
        return;
    }
    pNode phead = pHead;

    //核心
    while(phead != NULL)
    {
        if(i == num)
        {
            //创建对象
            pNode p = (pNode)malloc(sizeof(struct Node));
            if(NULL == p)
            {
                printf("p malloc is error\n");
            }
            p->data = value;
            if(phead->pNext) {
                p->pNext = phead->pNext;
                phead->pNext = p;
            }
            else
            {
                phead->pNext = p;
                p->pNext = NULL;
            }
        }
        phead = phead->pNext;
        i++;
    }
}

void del_list(pNode pHead,int num)
{
    pNode phead = NULL;
    int i = 0;
    if(pHead == NULL && num <0)
    {
        printf("del_list is error\n");
        return;
    }
    phead = pHead;

    while(phead != NULL)
    {
        if(i == num)
        {
            if(phead->pNext) {
                if(phead->pNext->pNext)
                    phead->pNext = phead->pNext->pNext;
                else
                {
                    phead->pNext = NULL;
                }
            }
        }

        phead = phead->pNext;
        i++;
    }
}


测试main文件

#include <stdio.h>
#include <stdlib.h>
#include "LinkList.h"

int main() {

    pNode phead = CreateLinkHead();
    if(NULL == phead)
    {
        printf("1111111111\n");
    }
    PrintLinkList(phead);
    printf("插入前打印\n");
    install_List(phead,2,3);
    printf("插入后的打印\n");
    PrintLinkList(phead);
    del_list(phead,0);
    printf("删除以后打印\n");
    PrintLinkList(phead);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值