数据结构复习:单链表的实现

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

typedef struct LNode
{
    int data;
    struct LNode *next;
} LNode, *LinkList;
//typedef struct LNode *LinkList;
//查找第i个元素
LNode* GetElem(LinkList L,int i)
{
    LNode* p = L->next;
    int j = 1;
    if(i ==0)
    {
        return L;
    }
    if(i < 0)
    {
        return NULL;
    }
    while(p!=NULL && j < i )
    {
        p = p->next;
        j++;
    }
    return p;
}
//查找值为e的元素
LNode* LocateElem(LinkList L,int e)
{
    LNode* p = L->next;
    while(p&& p->data!= e)
    {
        p = p->next;
    }
    return p;
}


//初始化链表(带头结点)
bool InitList(LinkList &L)
{

    printf("初始化单链表:\n");
    //给头结点分配空间
    L = (struct LNode*)malloc(sizeof(struct LNode));
    if(L== NULL)
    {
        return false;
    }
    L->next = NULL;
    //L->next =L;//循环单链表
    return true;
}
//判断链表为空
bool Empty(LinkList L)
{
    //return L->next == L;//循环单链表
    return L == NULL;
}

//打印单链表
void PrintList(LinkList L)
{
    printf("\n打印当前元素列表:\n");
    LNode*p = L->next;
    int i = 0;
    while(p)
    {
        printf("第%d个元素的值为%d\n",(++i),p->data);
        p = p->next;
    }
    printf("\n");
}
//求表长度
int Length(LinkList L)
{
    LNode *p = L->next;
    int len = 0;
    while(p)
    {
        len++;
        p = p->next;
    }
    return len;
}
//在第i个位置插入元素e
bool ListInsert(LinkList &L,int i,int e)
{
    if(i <1 || i> Length(L)+1)
    {
        return false;
    }
    LNode*p = L;
    p = GetElem(L,i-1);

    /*LNode*q = p->next;

    int j = 1;
    while(q!=NULL &&j<i)
    {
        p = q;
        q = q->next;
        j++;
    }
    LNode* node = (LNode*)malloc(sizeof(LNode));
    if(node == NULL )
    {
        return false;
    }
    node->data=e;
    node->next = q;
    p->next = node;*/

    LNode* node = (LNode*)malloc(sizeof(LNode));
    if(node == NULL )
    {
        return false;
    }
    node->data=e;
    node->next = p->next;
    p->next = node;

    return true;
}

//删除链表的第i个结点
int ListDelete(LinkList &L,int i, int &e)
{
    if(i <1 || i> Length(L))
    {
        return false;
    }
    LNode*p = L;
    LNode*q = p->next;
    int j = 1;
    while(q && j < i)
    {
        /*if(j == i)
        {

            break;

        }*/
        p = q;
        q = q->next;
        j++;
    }
//删除当前结点
    e = q->data;
    p->next = q->next;
    free(q);
    return -1;
}
//在p节点 之后插入元素e
bool InsetNextNode(LNode* p, int e)
{
    if(p == NULL)
    {
        return false;
    }
    LNode* s = (LNode*)malloc(sizeof(LNode));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
}
//在p 节点之前插入元素
//思路一:找到p的前驱结点
//q->next = p;
//s = (LNode*)malloc(sizeof(LNode));
//s->next = q->next;
//q->next = s;
//思路二:将新的结点添加到p结点后面,交换两个结点的数据
bool InsertPriorNode(LNode*p,int e)
{
    LNode *s = (LNode*)malloc(sizeof(LNode));
    if(s== NULL)
    {
        return false;
    }
    s->next = p->next;
    p->next = s;
    s->data= p->data;
    p->data = e;
    return true;

}


//删除的指定结点(不能删除最后一个结点,最后一个结点需要传入头结点遍历找到改结点的前一个结点)
/**
    q = p->next
    p->data=p->next->data;
    p->next = q->next;
    free(q);
**/
bool DeleteNode(LNode *p)
{
    if(p==NULL)
    {
        return false;
    }
    LNode *q = p->next;
    p->data=p->next->data;
    p->next = q->next;
    free(q);
    return true;

}
//尾插法建立单链表
LinkList List_TailInsert(LinkList &L)
{
    printf("尾插法建立单链表,请输入数据:\n");
    int x = -1;
    scanf("%d",&x);
    /*while(x!=9999) {

        ListInsert(L,Length(L)+1,x);
        scanf("%d",&x);
    }*/
    LNode* s,*tail = L;//s存储当前结点,tail的指针指向表尾
    while(x != 9999)
    {

        s = (LNode*)malloc(sizeof(LNode));
        if(s==NULL)
        {
            printf("动态内存分配失败\n");
            continue;
        }
        s->data = x;
        tail->next = s;
        tail = s;
        scanf("%d",&x);
    }
    tail->next = NULL;
    return L;
}

//头插法建立单链表(应用:单链表的逆置)
LinkList List_HeadInsert(LinkList &L) {
    printf("头插法建立单链表,请输入数据:\n");
    int x = -1;
    scanf("%d",&x);
    //每次都把数据插入到单链表的头部之后
    L = (LNode*)malloc(sizeof(LNode));
    L->next = NULL;
    LNode  *head=L,*cur,*s;
    while(x!=9999) {
        s = (LNode*)malloc(sizeof(LNode));

        s->data= x;
        /*s->next = cur;
        head->next = s;
        cur = s;*/
        s->next = L->next;
        L->next = s;
        scanf("%d",&x);
    }
    return L;

}
int main()
{
    LNode* L;
    InitList(L);

    LNode* n2 = (struct LNode*)malloc(sizeof(struct LNode));
    LNode* n3 = (struct LNode*)malloc(sizeof(struct LNode));

    LNode* n6 = (struct LNode*)malloc(sizeof(struct LNode));

    LNode* n5 = (struct LNode*)malloc(sizeof(struct LNode));
    L->next = n2;
    n2->next = n3;
    n2->data = 2;
    n3->data = 3;
    n3->next = n5;
    n5->next= n6;
    n5->data= 5;
    n6->data = 6;
    n6->next = NULL;

    PrintList(L);
    printf("在位置1插入元素1\n");
    ListInsert(L,1,1);
    ListInsert(L,4,4);
    printf("在位置4插入元素4\n");
    ListInsert(L,7,7);
    printf("在位置7插入元素7\n");
    PrintList(L);
    printf("当前表长:%d\n",Length(L));

    int i = 1;
    int e = -1;
    LNode* node = GetElem(L,i);
    if(node == NULL)
    {
        printf("第%d个元素未找到\n",i);
    }
    else
    {
        printf("查找到第%d个元素%d\n",i,node->data);
    }
    e = 6;
    node = LocateElem(L,e);

    if(node == NULL)
    {
        printf("值为%d的元素未找到\n",e);
    }
    else
    {
        printf("查找到值为%d的元素%d\n",e,node->data);
    }
    e = -1;
    i = 7;

    if( ListDelete(L,i,e))
    {

        printf("第%d个元素%d已删除\n",i,e);
        PrintList(L);
    }
    else
    {
        printf("第%d个元素删除失败\n",i);
    }
    i= 6;
    e = -1;

    if( ListDelete(L,i,e))
    {

        printf("第%d个元素%d已删除\n",i,e);
        PrintList(L);
    }
    else
    {
        printf("第%d个元素删除失败\n",i);
    }
    i= 1;
    e = -1;

    if( ListDelete(L,i,e))
    {

        printf("第%d个元素%d已删除\n",i,e);
        PrintList(L);
    }
    else
    {
        printf("第%d个元素删除失败\n",i);
    }
    LNode *p = n5;
    e=6;

    if(InsetNextNode(p,e))
    {
        printf("在元素%d后插入元素%d成功\n",p->data,e);
        PrintList(L);
    }
    else
    {
        printf("在元素%d后插入元素%d失败\n",p->data,e);
    }

    e = 1;
    p = n2;

    int pData = p->data;

    if(InsertPriorNode(p,e))
    {
        printf("在元素%d前插入元素%d成功\n",pData,e);
        PrintList(L);
    }
    else
    {
        printf("在元素%d前插入元素%d失败\n",pData,e);
    }


    InitList(L);
    List_TailInsert(L);
    PrintList(L);

    List_HeadInsert(L);
    PrintList(L);


    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值