zoukankan      html  css  js  c++  java
  • 链表的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
    ");
    }
    
    //查找单链表的长度
    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("
    ");
    }
    
    /*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
    ",Getlength(&head));
    
        //位置插入
        Insert_pos(&head,0,11);
        Show(&head);
    
        //key值前驱的数据
        printf("%d
    ",Search(&head,0)->data);
    
        //删除某一元素
        Delete(&head,4);
        Show(&head);
    
        //删除某一元素
        Destroy(&head);
    
        system("pause");
        return 0;
    }

  • 相关阅读:
    InterlockedIncrement函数详解
    c#事件调用
    jdk-8u281-windows-x64.exe JavaSE开发包
    安卓SDK_installer_r24.4.1-windows
    c#中@符号作用
    c#winformAPI_MFC_API
    NFC上位机未发现RFID设备
    MFC隐藏到托盘双击还原_右键退出实现方法
    win10 win+R快捷指令启动程序汇总
    CAN总线调试---节点掉线问题
  • 原文地址:https://www.cnblogs.com/sweetlittlebaby/p/12919182.html
Copyright © 2011-2022 走看看