数据结构第2章顺序表和链表的实现

#include <malloc.h>
#include<sal.h>
#define _CRT_SECURE_NO_WARNINGS 1
typedef struct LNode
{
    int data;//每个结点存放的数据元素Elemtype
    struct LNode* next;
}LNode,*LinkList;

//头插法建立单链表的算法如下
LinkList List_HeadInsert(LinkList& L)
{
    LNode* s; int x;
    L = (LinkList)malloc(sizeof(LNode));//创建头结点
    L->next = NULL;
    scanf("%d", &x);//输入结点的值
    while (x != 9999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        s->next = NULL;
        L->next = s;
        scanf("%d", &x);
    }
    return L;
}
//带头结点的单链表判断是否为空
bool EmptyList(LinkList& L)
{
    if (L->next == NULL)
        return true;
    else
        return false;

}

//带头结点的单链表的插入,在表中的第i个位置上插入指定元素e
bool ListInsert(LinkList& L, int i, int e)
{
    if (i < 1)
        return false;
    LNode* s = (LNode*)malloc(sizeof(LNode));
    s->data = e;
    LNode* p = L;//指针p指向当前扫描的结点
    int j = 0;//当前指针p指向的是第几个结点
    while (p != NULL && j < i - 1)
    {
        p = p->next;
        j++;
    }
    if (p == NULL)
        return false;//p的值不合法
    s->next = p->next;
    p->next = s;
    return true;
}
//单链表的建立,尾插法,带有尾指针
LinkList List_Taillnsert(LinkList& L)
{
    int x;
    L = (LinkList)malloc(sizeof(LNode));//初始化空链表
    LNode* s, * r = L;//r为表尾指针
    scanf_s("%d", &x);
    while (x != 9999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        s->next = NULL;
        r->next = s;
        r = s;
        scanf_s("%d", &x);
    }

}
//头插法建立单链表,头插法可用于链表的逆置
LinkList List_HeadInsert(LinkList& L)
{
    LNode* s;
    int x;
    L = (LinkList)malloc(sizeof(LNode));//创建头结点
    L->next == NULL;
    scanf("%d", &x);
    while (x != 9999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        if (s == NULL)
            return false;
        s->data = x;
        s->next = L->next;
        L->next = s;
        scanf("%d", &x);
    }
    return L;
}


//不带结点的单链表
//初始化一个空链表
bool InitList(LinkList& L)
{
    L = NULL;//空表,防止脏数据
    return true;
}
//判断链表为空
bool Empty(LinkList& L)
{
    return(L == NULL);
}

//按位序删除(带头结点)
bool ListDelete(LinkList& L, int i, int& e)
{//先找到第i-1个结点,再将第i-1个结点的指针指向i+1;
    if (i < 1)
        return false;
    LNode* p;//指针p指向当前扫描到的结点
    int j = 0;//当前p指向的是第几个结点
    p = L;
    while (p != NULL && j < i - 1)
    {
        p = p->next;
        j++;
    }
    if (p == NULL)
    {
        return false;
    }
    LNode* q = p->next ;//q指向要删除的结点
    e = q->data;//用e返回元素的值
    p->next = q->next;
    free(q);
    return true;
}
//方法2指定结点的删除
bool DeleteNodeTwo(LNode* p)
{
    LNode* q = p->next;
    p->next = q->next;
    p->data = q->data;
    free(q);
    return true;//注意如果删除结点是最后一个结点的话,这种方法行不通
}
//单链表的查找,按位查找,注意i从1开始,返回第i个元素位置指针
LNode* GetElem(LinkList L, int i)
{
    if (i < 1)
        return NULL;
    LNode* p=L;
    int j = 0;
    while (p != NULL && j < i)
    {
        p = p->next;
        j++;
    }
    return p;

}
//按值查找,找到数据域==e的结点
LNode* LocateElemTwo(LinkList L, int e)
{
    LNode* p = L->next;
    while (p != NULL && (p->data != e))
    {
        p = p->next;
    }
    return p;
}
//求表的长度
int Length(LinkList L) {
    int len = 0;
    LNode* p = L;
    while (p != NULL)
    {
        p = p->next;
        len++;
    }
    return len;
}

//按位序插入不带头结点
//不带头结点插入第一个位置时需要特殊处理
bool InsertList(LinkList& L, int i, int e)
{
    if (i < 1)
        return false;
    if (i == 1)
    {
        LNode* s = (LNode*)malloc(sizeof(LNode));
        s->data = e;
        s->next = L->next;
        L = s;
        return true;
    }
    LNode* p;
    int j = 1;
    p = L;
    while (p != NULL && j < i - 1)
    {
        p = p->next;
        j++;
    }
    if (p == NULL)
        return false;
    LNode* s = (LNode*)malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
}
//指定结点的后插操作
bool InsertNextNode(LNode* p, int e)
{
    LNode* s = (LNode*)malloc(sizeof(LNode));
    if (s == NULL)
        return false;//内存分配失败
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
}

//指定结点的前插操作
bool InserPriorNode(LNode* p, int e)
{
    if (p == NULL)
        return false;
    LNode* s = (LNode*)malloc(sizeof(LNode));
    s->data = p->data;
    p->data = e;
    s->next = p->next;
    p->next = s;
    return true;

}
//指定结点的前插操作方法2
bool InsertPriorNodeTwo(LNode* p, LNode* s)
{
    if (p == NULL && s == NULL)
        return false;
    s->next = p->next;
    int temp = p->data;
    p->data = s->data;
    s->data = temp;
    p->next = s;
    return true;
}

//双链表的初始化(带头结点)
typedef struct DNode {
    int data;
    struct DNode* pior, * next;
}DNode,*DLinklist;

bool InitDlinkList(DLinklist& L)
{
    L = (DNode*)malloc(sizeof(DNode));
    if (L == NULL)
        return false;
    L->pior = NULL;
    L->next = NULL;
    return true;
}
//判断单链表是否为空
bool Empty(DLinklist L)
{
    if (L->next == NULL)
        return true;
    return false;
}
//双链表的插入,注意P是最后一个结点的情况
bool InsetNextNode(DNode* p, DNode* s)
{
    if (p == NULL && s == NULL)
        return false;
    s->next = p->next;
    if (p->next != NULL)
        p->next->pior = s;
    s->next = p->next;
    p->next = s;
    s->pior = p;
    return true;
}

//双链表的删除
void DestoryList(DLinklist& L)
{

}
//删除p的后继结点
bool DeletNextDNode(DNode* p)
{
    if (p == NULL)
        return false;
    DNode* q = p->next;
    if (q == NULL)
        return false;
    p->next = q->next;
    if (q->next != NULL)
        q->next->pior = p;
    free(q);
    return true;
}
//循环单链表
//初始化一个循环单链表
bool IntList(LinkList& L)
{
    L = (LNode*)malloc(sizeof(LNode));
    if (L == NULL)
        return false;
    L->next = L;
    return true;
}
//判断循环单链表是否为空
bool Empty(LinkList L)
{
    if (L->next == L)
        return true;
    return false;
}
//判断p是否为循环单链表的表尾结点
bool isTail(LinkList L, LNode* p)
{
    if (p->next == L)
        return true;
    return false;
}
//循环双链表
//初始化循环双链表
bool InitDLinkList(DLinklist& L)
{
    L = (DNode*)malloc(sizeof(DNode));
    if (L == NULL)
        return false;
    L->next = L;
    L->pior = L;
    return true;
}
//循环双链表的插入,在P后面插入结点S
bool InsertNextDNode(DNode* p, DNode* s)
{
    s->next = p->next;
    s->next->pior = s;
    p->next = s;
    s->pior = p;
}
//双链表的删除,删除p的后继结点q

//静态链表:分配一整片连续的存储空间,各个结点集中安置。数据元素+指向下一个结点的数组下标
#define MAXSIZE 10
struct Node {
    int data;//存储数组元素
    int next;//下一个元素的数组下标
};
typedef struct Node SLinkList[MAXSIZE];

//等价于
typedef struct {
    int data;
    int next;
}SLinkList[MAXSIZE];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值