• 数据结构复习链表练习程序


    这次是一个单链表练习程序,属于复习,所以比较简单,供有需要的同学参考,也欢迎大家拍砖。

    以下依次列出各个文件内容,其中有些是VS自动生成的(尤其是前头那些h文件中的内容)。

    运行环境:

    Windows2003 x86

    VS2008sp1

    开启预编译头文件选项

    上代码:

    stdafx.h

    // stdafx.h : 标准系统包含文件的包含文件,
    // 或是经常使用但不常更改的
    // 特定于项目的包含文件
    //
    
    #pragma once
    
    #include "targetver.h"
    
    #include <stdio.h>
    #include <tchar.h>
    
    //namespace相关的include
    #include<iostream>
    using namespace std;
    
    
    // TODO: 在此处引用程序需要的其他头文件
    #include"mylist.h"

    stdafx.cpp

    // stdafx.cpp : 只包括标准包含文件的源文件
    // cnsDSExec.pch 将作为预编译头
    // stdafx.obj 将包含预编译类型信息
    
    #include"stdafx.h"
    
    // TODO: 在 STDAFX.H 中
    // 引用任何所需的附加头文件,而不是在此文件中引用

    targetver.h

    #pragma once
    
    // 以下宏定义要求的最低平台。要求的最低平台
    // 是具有运行应用程序所需功能的 Windows、Internet Explorer 等产品的
    // 最早版本。通过在指定版本及更低版本的平台上启用所有可用的功能,宏可以
    // 正常工作。
    
    // 如果必须要针对低于以下指定版本的平台,请修改下列定义。
    // 有关不同平台对应值的最新信息,请参考 MSDN。
    #ifndef _WIN32_WINNT            // 指定要求的最低平台是 Windows Vista。
    #define _WIN32_WINNT 0x0600     // 将此值更改为相应的值,以适用于 Windows 的其他版本。
    #endif

    mylist.h

    //链表练习的h文件定义
    
    #pragma once
    
    #define LISTOK 1
    #define LISTERROR 0
    
    //#define LISTMAX 20
    
    typedef int Elemtype;
    
    //定义链表节点
    typedef struct stuNode
    {
        Elemtype listData;
        struct stuNode *next;
    }Listnode;
    
    typedef Listnode *PListnode;
    
    //以下凡有改变pList值的操作均使用引用
    //获得链表中某个元素返回给e
    extern int ListGet(PListnode,int,Elemtype *);
    
    //查找链表中某元素
    extern int ListFind(PListnode);
    
    //向链表中加入元素
    extern int ListInsert(PListnode &,int,Elemtype *);
    
    //从链表中删除元素
    extern int ListDel(PListnode &,int,Elemtype *);
    
    //初始化链表
    extern int ListInit(PListnode &,Elemtype *);
    
    //输出整个链表的内容
    extern int ListPrint(PListnode &);
    
    //删除整个链表
    extern int ListClear(PListnode &);
    
    //获得链表长度
    extern int ListLength(PListnode &);

    mylist.cpp

    #include"stdafx.h"
    
    //获得链表中某个元素返回给elem
    int ListGet(PListnode pList,int iIndex,Elemtype *elem)
    {
        PListnode pListtemp;
        pListtemp=pList;    //先定位到首节点
        
        int iTemp;
        iTemp=1;
        
        //循环前往iIndex位置
        while(pListtemp && (iTemp<iIndex))
        {
            pListtemp=pListtemp->next;
            iTemp++;
        }
        
        if(!pListtemp || (iTemp>iIndex))    //其中后一个条件在范围外的iIndex时起效,如iIndex=-1
            return LISTERROR;
        else
        {
            *elem=pListtemp->listData;
            return LISTOK;
        }
    }
    
    //查找链表中某元素
    int ListFind(PListnode pList)
    {
    
        return LISTOK;
    }
    
    //向链表中加入元素
    int ListInsert(PListnode &pList,int iIndex,Elemtype *elem)
    {
        PListnode pListtemp,pListinsert;
        pListtemp=pList;    //先定位到首节点
        
        int iTemp;
        iTemp=1;
        
        //循环前往iIndex位置
        while(pListtemp && (iTemp<iIndex))
        {
            pListtemp=pListtemp->next;
            iTemp++;
        }
        
        if(!pListtemp || (iTemp>iIndex))    //其中后一个条件在范围外的iIndex时起效,如iIndex=-1
            return LISTERROR;
        else
        {
            //创建一个新节点
            pListinsert=(PListnode)malloc(sizeof(Listnode));
            pListinsert->listData=*elem;
            pListinsert->next=pListtemp->next;    
            
            //将目前位置节点的链接指向新节点
            pListtemp->next=pListinsert;
            
            return LISTOK;
        }
    }
    
    //从链表中删除元素
    int ListDel(PListnode &pList,int iIndex,Elemtype *elem)
    {
        PListnode pListtemp,pListnext;
        pListtemp=pList;    //先定位到首节点
        
        int iTemp;
        iTemp=1;
        
        //循环前往iIndex位置
        while(pListtemp && (iTemp<iIndex))
        {
            pListtemp=pListtemp->next;
            iTemp++;
        }
        
        if(!pListtemp || (iTemp>iIndex))    //其中后一个条件在范围外的iIndex时起效,如iIndex=-1
            return LISTERROR;
        else
        {
            //如下两句跨越待删除节点完成连接关系的重建
            pListnext=pListtemp->next;
            pListtemp->next=pListnext->next;    
    
            *elem=pListnext->listData;
            free(pListnext);
            cout<<"List Node "<<iIndex<<" was deleted"<<endl;
            return LISTOK;
        }
    }
    
    //初始化链表
    int ListInit(PListnode &pList,Elemtype *elem)
    {
        pList=(PListnode)malloc(sizeof(Listnode));  //申请结点空间 
        if(NULL==pList)                                //判断是否有足够的内存空间 
        {
            printf("Memory alloc failure");
            return LISTERROR;
        }
        else
        {
            pList->listData=*elem;
            pList->next=NULL;
            
            return LISTOK;
        }
    }
    
    //输出整个链表的内容
    int ListPrint(PListnode &pList)
    {
        if(!pList)
        {
            cout<<"List is empty"<<endl;
            return LISTERROR;
        }
        else
        {
            PListnode pListtemp;
            pListtemp=pList;
        
            //cout<<pList->listData<<endl;
    
            int iIndex=1;
            
            while(pListtemp)
            {
                cout<<"List Node "<<iIndex<<"->Data="<<(pListtemp->listData)<<endl;
                pListtemp=pListtemp->next;
                iIndex++;
            }
            cout<<"List print complete"<<endl;
    
            return LISTOK;
        }
    }
    
    
    //删除整个链表
    int ListClear(PListnode &pList)
    {
        PListnode pListdel;
        PListnode pListtemp;
        
        //pListtemp=pList;
        pListdel=pList;
        
        while(pListdel)
        {
            pListtemp=pListdel->next;        //listTemp先连接到待删除节点的下一个节点
            free(pListdel);                //正式删除待删除节点
            pListdel=pListtemp;            //定位到新的待删除节点
        }
        
        pList=NULL;                        //最终将首节点置为NULL防止随机分配导致链表长度或输出函数中无法判断
        
        cout<<"ListClear Complete"<<endl;
        return LISTOK;
    }
    
    //获得链表长度
    int ListLength(PListnode &pList)
    {
        if(!pList)
        {
            //cout<<"List is empty"<<endl;
            return LISTERROR;
        }
        else
        {
            PListnode pListtemp;
            pListtemp=pList;    //先定位到首节点
        
            int iTemp;
            iTemp=0;
        
            //循环前往iIndex位置
            while(pListtemp)
            {
                pListtemp=pListtemp->next;
                iTemp++;
            }
            return iTemp;
        }
    }

    主程序文件 cnsDSExec.cpp

    // cnsDSExec.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    
    //定义一批值用于链表操作
    #define INT_HEAD 2
    #define INT_SIZEMAX 10
    
    #define INT_DEL 4
    
    int main(int argc, _TCHAR* argv[])
    {
        PListnode pListfirst;
        //PListnode pListfirst=NULL;
    
        int iFirst=INT_HEAD;
        int *pIntfirst=&iFirst;
        
        //初始化
        ListInit(pListfirst,pIntfirst);
        
        //加入若干节点及内容
        for(int i=0;i<=INT_SIZEMAX-1;i++)
        {
            ListInsert(pListfirst,i+1,&i);
        }
        
        //首次显示链表内容
        ListPrint(pListfirst);
    
        //删除某节点
        int *pIntdel,intDel=0;
        pIntdel=&intDel;
        ListDel(pListfirst,INT_DEL,pIntdel);
    
        //删除某节点后显示链表内容
        ListPrint(pListfirst);
        
        //最终删除该链表
        ListClear(pListfirst);
        //
        //cout<<"The length of List is "<<ListLength(pListfirst)<<endl;
    
        ////显示链表内容
        ListPrint(pListfirst);
    
        fflush(stdin);
        getchar();
        return 0;
    }
  • 相关阅读:
    git命令使用方法
    git与svn对比
    浏览器缓存原理
    网络性能优化常用方法
    sass/scss 和 less的区别
    AngularJS和ReactJS对比
    让IE6,7,8支持HTML5新标签的方法
    Retina 屏移动设备 1px解决方案
    HttpClient学习
    国家二字码对照表
  • 原文地址:https://www.cnblogs.com/vbspine/p/list.html
Copyright © 2020-2023  润新知