链表基本操作

LinkList.h

#ifndef LINKLIST_H
#define LINKLIST_H
typedef int element;
typedef struct ListNode{
    element data;
    ListNode* next;
}LNode;
//初始化一个空链表
void InitLinkList(LNode* *head);

//计算链表的长度
int LinkListLength(LNode* head);

//插入操作:在位置i插入结点,结点数据为x
bool InsertLinkList(LNode* head,int i, element x);

//插入操作:在链表后依次插入数组list的每个元素
bool ArrInsertToList(LNode* head, element list[], int n);

//删除操作:删除位置i的结点,数据保存在x中
bool DeleteLinkList(LNode* head,int i, element& x);

//读数据:读取位置i的数据,保存在x中
bool GetLinkList(LNode* head,int i, element& x);

//反转链表
LNode* ReverseLinkList(LNode* head);
LNode* ReverseLinkList2(LNode* head);//递归
LNode* ReverseLinkList3(LNode* head);//反转相邻结点
//打印链表
void PrintLinkList(LNode* head);

//释放链表
void DestroyLinkList(LNode* *head);

/*总结:
1、注意概念,头指针head,头结点a[0]
2、判断参数指针是不是为空
3、初始化和销毁函数 指针的指针
4、插入或者删除某个位置、画图,条理清晰
*/
#endif

 

LinkList.cpp

#include <iostream>
#include <assert.h>
#include "LinkList.h"
using namespace std;
void InitLinkList(LNode* *head)
{
    *head = (LNode*)malloc(sizeof(LNode));
    if(*head == NULL)
    {
        exit(1);
    }
    (*head)->next=NULL;
}
void DestroyLinkList(LNode* *head)
{
    LNode* p=*head;
    LNode* q=NULL;
    while(p != NULL)//
    {
        q=p;
        p=p->next;
        free(q);
        q=NULL;
    }
    *head=NULL;
}

int LinkListLength(LNode* head)
{
    int len=0;
    LNode* p = head;

    assert(p!=NULL);

    while(p->next != NULL)
    {
        len++;
        p=p->next;
    }
    return len;
}

bool ArrInsertToList(LNode* head, element list[], int n)
{
    LNode* p;
    p=head;

    if(p==NULL)
        return false;

    while(p->next != NULL)
    {
        p=p->next;
    }

    for(int i=0; i<n; i++)
    {
        ListNode* q=(LNode*)malloc(sizeof(LNode));
        if(q==NULL)
        {
            exit(1);
        }
        q->data=list[i];
        q->next=NULL;
        p->next=q;
        p=p->next;
    }
    return true;
}


bool InsertLinkList(LNode* head,int i, element x)
{
    LNode* p;
    p=head;

    if(p==NULL)
        return false;

    /*
    head,a[0],a[1],...,a[i-1],a[i],...,a[len-1]
    在位置i(0<=i<=len)处插入数据x,即在找到a[i-1],在a[i-1]后插入x
    */
    if(i>LinkListLength(head)+1 || i<0)//0...len
    {
        cout << "插入位置错误!";
        return false;
    }
    for(int j=-1;j<i-1; j++)//得到a[i-1]
        p=p->next;
    
    ListNode* q=(LNode*)malloc(sizeof(LNode));
    if(q==NULL)
    {
        exit(1);
    }
    q->data=x;
    q->next=p->next;
    p->next=q;
    return true;
}

bool DeleteLinkList(LNode* head,int i, element& x)
{
    LNode* p;
    LNode* q;
    p=head;

    if(p==NULL)
        return false;

    if(i>=LinkListLength(head) || i<0)//0...len-1
    {
        cout << "删除位置错误!";
        return false;
    }

    for(int j=-1;j<i-1; j++)//得到a[i-1]
        p=p->next;

    q=p->next;
    x=q->data;

    p->next=p->next->next;
    free(q);
    return true;
}

bool GetLinkList(LNode* head,int i, element& x)
{
    LNode* p;
    p=head;

    if(p==NULL)
        return false;
    if(i>=LinkListLength(head) || i<0)//0...len-1
    {
        cout << "位置i错误!";
        return false;
    }
    for(int j=-1;j<i; j++)//得到a[i]
        p=p->next;

    x=p->data;
    return true;
}

void PrintLinkList(LNode* head)
{
    LNode* p=head;
    
    if(p==NULL)//注意
    {
        return;
    }

    p=head->next;

    while(p !=NULL)
    {
        cout << p->data <<",";
        p=p->next;
    }
    return;
}

LNode* ReverseLinkList(LNode* head)
{
    if(head == NULL)//注意
        return head;

    LNode* p=head->next;
    LNode* temp=NULL;
    LNode* trail=NULL;

    while(p != NULL)
    {//4句
        temp=p;
        p=p->next;
        temp->next=trail;
        trail=temp;
    }
    head->next=trail;//

    return head;
}
LNode* ReverseLinkList2(LNode* head)//测试失败
{
    if(head->next==NULL)
        return head;
    LNode* ret=ReverseLinkList2(head->next);
    head->next->next=head;
    head->next=NULL;

    return ret;

}
LNode* ReverseLinkList3(LNode* head)
{
    if(head == NULL)
        return head;
    LNode* p;
    LNode* temp;
    LNode* trail=head;

    while(trail->next !=NULL && trail->next->next != NULL)
    {
        p=trail->next;
        temp=trail->next->next;
        trail->next=temp;
        p->next=temp->next;
        temp->next=p;
        trail=p;
    }

    return head;
}

转载于:https://www.cnblogs.com/zjhnl/archive/2012/10/02/2710505.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值