链表基本操作

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;
}
posted @ 2012-10-02 19:29  logzh  阅读(259)  评论(0编辑  收藏  举报