静态链表-新片

.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 
#include 

//节点的操作
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 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 
#include 
#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;
}



你可能感兴趣的:(数据结构,c语言,数据结构)