链表的c语言实现与c++实现

数据结构书中代码,未检验

头文件:

#ifndef List_H

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Position p,List L);
Position Find(int x,List L);
void Delete(int x,List L);
Position FindPrevious(int x,List L);
void Insert(int x,List L,Position P);
void DeleteList(List L);
void show(List L);
Position Header(List L);
Position First(List L);
Position Advance(Position P);

#endif

list.cpp(函数接口问题,之前写过)

#include"list.h"
#include<bits/stdc++.h>

struct Node
{
    int element;
    Position Next;
};

int IsEmpty(List L)
{
    return L->Next == NULL;
}

int IsLast(Position P,List L)
{
    return P->Next==NULL;
}

/*返回x在l中的位置,找不到返回NULL */
Position Find(int x,List L)
{
    Position P;

    P=L->Next;
    while(P!=NULL&&P->element!=x)
     P=P->Next;

     return P;
}

/*删除x在表中第一次出现*/
/*假如用一次头指针*/
void Delete(int x,List L)
{
    Position P,TmpCell;
    P=FindPrevious(x,L);

    if(!IsLast(P,L))//x被找到,删除x;
    {
        TmpCell=P->Next;
        P->Next=TmpCell->Next;
        free(TmpCell);
    }

}

Position FindPrevious(int x,List L)
{
    Position P;

    P=L;
    while(P->Next!=NULL && P->Next->element!=x)
     P=P->Next;

    return P;
}

void Insert(int x,List L,Position P)
{
    Position TmpCell;

    TmpCell=(struct Node *)malloc( sizeof( struct Node) );

      TmpCell->element =x;
      TmpCell->Next=P->Next;
      P->Next=TmpCell;
}

 简单的链表实现:

 

/*头文件*/
#ifndef List_H

typedef int Elemtype;
typedef struct Node
{
    Elemtype data;
    struct Node *next; 
}Node,*List;
//初始化单链表
void InitList(List L);

//头插法
bool Insert_head(List L,Elemtype x);

//尾插法
bool Insert_tail(List L,Elemtype x);

//pos位置插入
bool Insert_pos(List L,int pos,Elemtype x);

//查找key的前驱
Node *Search(List L,int key);

//删除key节点
bool Delete(List L,int key);

//是否为空
bool Is_Empty(List L);

//摧毁函数
void Destroy(List L);

//打印长度
int Getlength(List L);

//打印链表
void Show(List L);

#endif


/*list.cpp*/
#include<stdio.h>
#include"list.h"
#include<assert.h>
//初始化链表
void InitList(List L)
{
    assert(L!=NULL);//assert如果它的条件返回错误,则终止程序执行
    L->next=NULL;
}

//创建新节点
static Node*GetNode(Elemtype x)
{
    Node *PGet=(Node*)malloc(10*sizeof(Node));
    assert(PGet!=NULL);
    PGet->data=x;
    PGet->next=NULL;
    return PGet;
}

//头插法
bool Insert_head(List L,Elemtype x)
{
    assert(L!=NULL);
    Node *PGet=GetNode(x);
    PGet->next=L->next;
    L->next=PGet;
    return true;
}

//尾插法
bool Insert_tail(List L,Elemtype x)
{
    assert(L!=NULL);
    Node *PGet=GetNode(x);
    Node *P=L->next;
    while(P->next!=NULL)
     P=P->next;
    P->next=PGet;
    return true;
}

//pos位置插入
bool Insert_pos(List L,int pos,Elemtype x)
{
    assert(L!=NULL);
    if(pos<0||pos>Getlength(L))
     return false;
    Node *PGet=GetNode(x);
    Node *P=L;
    int i=0;
    while(i!=pos)
      P=P->next;
    PGet->next=P->next;
    P->next=PGet;
    return true;
}

//查找key的前驱
Node*Search(List L,int key)
{
    assert(L!=NULL);
    if(Is_Empty(L))
     return NULL;
    Node *P=L;
    while(P->next!=NULL)
    {
        if(P->next->data==key)
          return P;
        P=P->next;
    }
    return NULL;
}

//删除key节点
bool Delete(List L,int key)
{
    assert(L!=NULL);
    if(Is_Empty(L))
     return false;
    Node *P=L;
    while(P->next!=NULL)
    {
        if(Search(L,key)!=NULL)
        {
            Node *q=Search(L,key);
            P=q->next;
            q->next=P->next;
            P=q;
        }
        else
          P=P->next;
    }
    return true;
}

//是否为空
bool Is_Empty(List L)
{
    if(L->next==NULL)
     return true;
    return false;
}

//摧毁链表
void Destroy(List L)
{
    assert(L!=NULL);
    Node *p=NULL;
    while(L->next!=NULL)
    {
        p=L->next;
        L->next=p->next;
        free(p);
    }
    p=NULL;
    printf("Destory\n");
}

//查找单链表的长度
int Getlength(List L)
{
    int i=0;
    Node *p=L->next;
    while(p!=NULL)
    {
        p=p->next;
        i++;
    }
    return i;
}

//打印链表
void Show(List L)
{
    assert(L!=NULL);
    Node *p=L->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}

/*main.cpp*/
#include<stdio.h>
#include<stdlib.h>
#include"list.cpp"
int main()
{
    Node head;
    InitList(&head);

    //头插
    for(int i=0; i<5; i++)
     Insert_head(&head,i);
    Show(&head);

    //尾插
    for(int j=0; j<5; j++)
    {
        Insert_tail(&head,j);
    }
    Show(&head);

    //打印长度
    printf("%d\n",Getlength(&head));

    //位置插入
    Insert_pos(&head,0,11);
    Show(&head);

    //key值前驱的数据
    printf("%d\n",Search(&head,0)->data);

    //删除某一元素
    Delete(&head,4);
    Show(&head);

    //删除某一元素
    Destroy(&head);

    system("pause");
    return 0;
}

 

 

posted on 2020-05-19 20:39  mmn  阅读(308)  评论(0编辑  收藏  举报