第四周项目二

 

  1. 烟台大学计算机学院  
  2.   
  3. 文件名称:sq.cpp  
  4.   
  5. 作者zhangsiqi 
  6.   
  7. 完成日期:2017年9月24日  
  8.   
  9. 问题描述:建立单链表算法库 
  10.   
  11. 输入描述:无 
  12.   
  13. 输出描述:进行了各个的链表的值 
  14.   
  15. */   
  16.   
  17.   
  18.   
  19. list.cpp:  
  20.   
  21.   
  22.   
  23. #include <stdio.h>   
  24. #include <malloc.h>   
  25. #include "list.h"   
  26. void initList(Linklist *&L)//初始化链表   
  27. {  
  28.     L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存   
  29.     L->next=NULL;//链表为空   
  30. }  
  31.   
  32. bool ListInsert(Linklist *&L,int i,ElemType e)//链表插入   
  33. {  
  34.     int j=0;  
  35.   
  36.     Linklist *p=L,*s;  
  37.   
  38.     if(i<=0)//输入的i比0小不合法   
  39.     {  
  40.         return false;  
  41.   
  42.     }  
  43.   
  44.     while(j<i-1 && p!=NULL)//遍历   
  45.     {  
  46.         j++;  
  47.   
  48.         p=p->next;  
  49.     }  
  50.   
  51.     if(p==NULL)//未找到i-1位置结点   
  52.     {  
  53.         return false;  
  54.     }  
  55.   
  56.     else//找到   
  57.     {  
  58.   
  59.         s=(Linklist *)malloc(sizeof(Linklist));  
  60.   
  61.         s->data=e;  
  62.   
  63.         s->next=p->next;  
  64.   
  65.         p->next=s;//插入操作   
  66.   
  67.         return true;  
  68.     }  
  69. }  
  70.   
  71. void DispList(Linklist *L)//输出链表的元素值   
  72. {  
  73.     Linklist*p=L->next;  
  74.   
  75.     while(p!=NULL)//输出+遍历   
  76.     {  
  77.         printf("%d ",p->data);  
  78.   
  79.         p=p->next;  
  80.   
  81.     }  
  82.   
  83.     printf("\n");  
  84. }  
  85.   
  86.   
  87. void  DestroyList(Linklist *&L)//销毁链表   
  88. {  
  89.     Linklist *pre=L,*p=L->next;  
  90.   
  91.     while(p!=NULL)  
  92.     {  
  93.         free(pre);  
  94.   
  95.         pre=p;  
  96.   
  97.         p=pre->next;  
  98.     }  
  99.     free(pre);  
  100. }  
  101. void  CreateListF(Linklist *&L,ElemType a[],int n)  
  102. {  
  103.     Linklist *s;  
  104.   
  105.     L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存空间   
  106.   
  107.     L->next=NULL;  
  108.   
  109.     for(int i=0;i<n;i++)//头插法   
  110.     {  
  111.         s=(Linklist *)malloc(sizeof(Linklist));  
  112.         s->data=a[i];  
  113.         s->next=L->next;  
  114.         L->next=s;  
  115.     }  
  116. }  
  117.   
  118. void  CreateListR(Linklist *&L,ElemType a[],int n)//尾插法   
  119. {  
  120.      Linklist *s,*r;  
  121.   
  122.      L=(Linklist *)malloc(sizeof(Linklist));  
  123.   
  124.      r=L;  
  125.   
  126.      for(int i=0;i<n;i++)//尾插法   
  127.      {  
  128.          s=(Linklist *)malloc(sizeof(Linklist));  
  129.   
  130.          s->data=a[i];  
  131.   
  132.          r->next=s;  
  133.   
  134.          r=s;  
  135.   
  136.      }  
  137.     r->next=NULL;  
  138. }  
  139.   
  140.   
  141.   
  142. bool ListDelete(Linklist *&L,int i,ElemType &e)//删除链表元素   
  143. {  
  144.   
  145.     int j=0;  
  146.   
  147.     Linklist *p=L,*q;  
  148.   
  149.     if(i<=0)return false;  
  150.   
  151.     while(j<i-1 && p!=NULL)//找到i的前一节点i-1   
  152.     {  
  153.         j++;  
  154.   
  155.         p=p->next;  
  156.     }  
  157.     if(p==NULL)//p为空,未找到元素   
  158.     {  
  159.         return false;  
  160.     }  
  161.   
  162.     else  
  163.     {  
  164.         q=p->next;  
  165.         if(q==NULL)//未找到元素   
  166.         {  
  167.             return false;  
  168.         }  
  169.         e=q->data;//删除的元素保留到e   
  170.   
  171.         p->next=q->next;  
  172.   
  173.         free(q);  
  174.   
  175.         return true;  
  176.     }  
  177. }  
  178.   
  179.   
  180.   
  181. main:  
  182.   
  183. #include <stdio.h>   
  184. #include <malloc.h>   
  185. #include "list.h"   
  186.   
  187.   
  188. int main()  
  189. {  
  190.     Linklist *L1,*L2;  
  191.   
  192.     ElemType a[8]={7,9,8,2,0,4,6,3};  
  193.   
  194.     printf("头插法建表结果:");  
  195.   
  196.     CreateListF(L1,a,8);  
  197.   
  198.     DispList(L1);  
  199.   
  200.     printf("尾插法建表结果:");  
  201.   
  202.     CreateListR(L2,a,8);  
  203.   
  204.     DispList(L2);  
  205.   
  206.     DestoryList(L1);  
  207.   
  208.     DestoryList(L2);  
  209.   
  210.     int b;  
  211.   
  212.     Linklist *L3;  
  213.   
  214.     CreateListR(L3,a,8);  
  215.   
  216.     ListDelete(L3,4,b);  
  217.   
  218.     printf("删除a数组中的元素:");  
  219.     printf("%d\n",b);  
  220.     DispList(L3);  
  221.   
  222.   
  223.   
  224.   
  225.   
  226.   
  227.   
  228.   printf("插入验证:");  
  229.   Linklist*L;  
  230.   initList(L);  
  231.   ListInsert(L,1,15);  
  232.   ListInsert(L,1,10);  
  233.   ListInsert(L,1,5);  
  234.   ListInsert(L,1,20);  
  235.   DispList(L);  
  236.   DestroyList(L);  
  237.  return 0;  
  238. }  
  239.   
  240. list.h:  
  241.   
  242. #include <stdio.h>   
  243. #include <malloc.h>   
  244. #include <stdio.h>   
  245. #include <malloc.h>   
  246. typedef int ElemType;  
  247.   
  248. typedef struct LNode  
  249. {  
  250.     ElemType data;  
  251.   
  252.     struct LNode *next;  
  253. }Linklist;  
  254. void initList(Linklist *&L);//初始化链表   
  255. bool ListInsert(Linklist *&L,int i,ElemType e);//链表插入   
  256. void DispList(Linklist *L);//输出   
  257. void  DestroyList(Linklist *&L);//销毁   
  258. void  CreateListF(Linklist *&L,ElemType a[],int n);//头插法   
  259. void  CreateListR(Linklist *&L,ElemType a[],int n);//尾插法   
  260. bool ListDelete(Linklist *&L,int i,ElemType &e);//元素删除  
/* 
烟台大学计算机学院 
 
文件名称:sq.cpp 
 
作者:zhangsiqi 
 
完成日期:2017年9月24日 
 
问题描述:建立单链表算法库
 
输入描述:无
 
输出描述:进行了各个的链表的值
 
*/ 



list.cpp:



#include <stdio.h>
#include <malloc.h>
#include "list.h"
void initList(Linklist *&L)//初始化链表
{
    L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存
    L->next=NULL;//链表为空
}

bool ListInsert(Linklist *&L,int i,ElemType e)//链表插入
{
    int j=0;

    Linklist *p=L,*s;

    if(i<=0)//输入的i比0小不合法
    {
        return false;

    }

    while(j<i-1 && p!=NULL)//遍历
    {
        j++;

        p=p->next;
    }

    if(p==NULL)//未找到i-1位置结点
    {
        return false;
    }

    else//找到
    {

        s=(Linklist *)malloc(sizeof(Linklist));

        s->data=e;

        s->next=p->next;

        p->next=s;//插入操作

        return true;
    }
}

void DispList(Linklist *L)//输出链表的元素值
{
    Linklist*p=L->next;

    while(p!=NULL)//输出+遍历
    {
        printf("%d ",p->data);

        p=p->next;

    }

    printf("\n");
}


void  DestroyList(Linklist *&L)//销毁链表
{
    Linklist *pre=L,*p=L->next;

    while(p!=NULL)
    {
        free(pre);

        pre=p;

        p=pre->next;
    }
    free(pre);
}
void  CreateListF(Linklist *&L,ElemType a[],int n)
{
    Linklist *s;

    L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存空间

    L->next=NULL;

    for(int i=0;i<n;i++)//头插法
    {
        s=(Linklist *)malloc(sizeof(Linklist));
        s->data=a[i];
        s->next=L->next;
        L->next=s;
    }
}

void  CreateListR(Linklist *&L,ElemType a[],int n)//尾插法
{
     Linklist *s,*r;

     L=(Linklist *)malloc(sizeof(Linklist));

     r=L;

     for(int i=0;i<n;i++)//尾插法
     {
         s=(Linklist *)malloc(sizeof(Linklist));

         s->data=a[i];

         r->next=s;

         r=s;

     }
    r->next=NULL;
}



bool ListDelete(Linklist *&L,int i,ElemType &e)//删除链表元素
{

    int j=0;

    Linklist *p=L,*q;

    if(i<=0)return false;

    while(j<i-1 && p!=NULL)//找到i的前一节点i-1
    {
        j++;

        p=p->next;
    }
    if(p==NULL)//p为空,未找到元素
    {
        return false;
    }

    else
    {
        q=p->next;
        if(q==NULL)//未找到元素
        {
            return false;
        }
        e=q->data;//删除的元素保留到e

        p->next=q->next;

        free(q);

        return true;
    }
}



main:

#include <stdio.h>
#include <malloc.h>
#include "list.h"


int main()
{
    Linklist *L1,*L2;

    ElemType a[8]={7,9,8,2,0,4,6,3};

    printf("头插法建表结果:");

    CreateListF(L1,a,8);

    DispList(L1);

    printf("尾插法建表结果:");

    CreateListR(L2,a,8);

    DispList(L2);

    DestoryList(L1);

    DestoryList(L2);

    int b;

    Linklist *L3;

    CreateListR(L3,a,8);

    ListDelete(L3,4,b);

    printf("删除a数组中的元素:");
    printf("%d\n",b);
    DispList(L3);







  printf("插入验证:");
  Linklist*L;
  initList(L);
  ListInsert(L,1,15);
  ListInsert(L,1,10);
  ListInsert(L,1,5);
  ListInsert(L,1,20);
  DispList(L);
  DestroyList(L);
 return 0;
}

list.h:

#include <stdio.h>
#include <malloc.h>
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;

typedef struct LNode
{
    ElemType data;

    struct LNode *next;
}Linklist;
void initList(Linklist *&L);//初始化链表
bool ListInsert(Linklist *&L,int i,ElemType e);//链表插入
void DispList(Linklist *L);//输出
void  DestroyList(Linklist *&L);//销毁
void  CreateListF(Linklist *&L,ElemType a[],int n);//头插法
void  CreateListR(Linklist *&L,ElemType a[],int n);//尾插法
bool ListDelete(Linklist *&L,int i,ElemType &e);//元素删除




运行结果:


学习心得:

学会了建立链表算法库。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值