表-list

顺序表

seqlist

#include <stdio.h>
#include <stdlib.h>
//定义一个顺序表的结构体
#define N 5
typedef int datatype_t;
typedef struct list_t
{
    datatype_t data[N];
    int last;
} seqlist_t, *seqlist_p;
  
//1.创建一个空的顺序表                   *****
//要通过一个函数更改一个变量的值,有两种方式 1》函数的返回值
//2》通过将变量的地址传递给函数,函数更改变量的值
seqlist_t *createSeqList(void)
{
    seqlist_t *p = (seqlist_t *)malloc(sizeof(seqlist_t));
    if (NULL == p)
    {
        printf("malloc seqlist err.\n");
        return NULL;
    }
    p->last = -1; //空表
    return p;
}
//2.向顺序表的指定位置插入数据         ****
int insertPost(seqlist_t *p, int post, datatype_t data)
{
    //1.容错处理
    if (post > p->last + 1 || post < 0 || p->last + 1 == N)
    {
        printf("post insert err.\n");
        return -1;
    }
    //2.将指定位置及之后的数据全部后移一个
    for (int i = p->last + 1; i > post; i--)
    {
        p->data[i] = p->data[i - 1];
    }
    //3.插入
    p->data[post] = data;
    p->last++;
    return 0;
}

//3.判断表是否为满  0没满   1满
int isFullSeqList(seqlist_t *p)
{
    return p->last + 1 == N;
}

//4.遍历顺序表,输出表中的数据              *****
void showSeqList(seqlist_t *p)
{
    for (int i = 0; i <= p->last; i++)
    {
        printf("%d ", p->data[i]);
    }
    putchar(10);
}

//5.判断顺序表是否为空  1空  0非空
int isEmptySeqList(seqlist_t *p)
{
    return p->last == -1;
}

// 6.修改指定位置的数据
int  changeSeqList(seqlist_t *p,int post,datatype_t data)
{
    //1.判断位置是否正确
    if(post < 0 || post > p->last || isEmptySeqList(p))
    {
        printf("change post err.\n");
        return -1;
    }
    p->data[post]=data;
    return 0;
}
//8.删除指定位置的数据       *****
int delectPost(seqlist_t *p,int post)
{
    //1.判断位置是否正确
    if(post < 0 || post > p->last || isEmptySeqList(p))
    {
        printf("delect post err.\n");
        return -1;
    }
    for(int i=post;i<p->last;i++)
    {
        p->data[i]=p->data[i+1]
    }
    p->last--;
    return 0;
}

//7.删除指定的数据
int delectDate(seqlist_p *p,datatype_t data)
{
    if(isEmptySeqList(p))
    {
        printf("is empty.\n");
        return -1;
    }
    for(int i=0;i<=p->last;i++)
    {
        if(p->data[i]==data)
        {
            delectPost(p,i);
            i--;
        }
    }
}

//9.清空顺序表
void clearSeqList(seqlist_t *p)
{
    p->last=-1;
}
// 10.删除顺序表
void delectList(seqlist_t **sp)
{
    free(*sp);
    *sp=NULL;
}

int main(int argc, char const *argv[])
{
    seqlist_t *p = createSeqList();
    int a = 1, post = 0; //1-5
    while (1)
    {
        if (isFullSeqList(p))
        {
            break;
        }
        else
        {
            insertPost(p, post, a);
            post++;
            a++;
        }
    }

    showSeqList(p);

    free(p);
    p=NULL;

    return 0;
}
int main()
{
 seqlist_t *p=createSeqList()
stu data[1]={1,'q',45.5};
stu data[2]={2,'w',58.5};
stu data[3]={3,'e',98.5};
inSeqList(p,0,1);
inSeqList(p,1,2);
inSeqList(p,2,3);
DelcSeqList(p,1);
ShowPost(p);
return 0;
}

链表

#include<stdio.h>
#include<stdlib.h>
#ifndef __LINK_H_
#define __LINK_I_
typedef int datatype_t;
typedef struct node_t    //节点
{
    datatype_t data;
    struct node_t *next;
}linklist_t,*linklist_p;
//1创建空的有头单链表   ***
linklist_t *creatEmptyLink(void)
{
 linklist_t *p=(linklist_t *)malloc(sizeof(linklist_t));
  if(NULL==p)
  {
      printf("malloc head node err.\n");
      return NULL;
  }
  p->next=NULL;

  return p;
}
//2想链表指定位置插入数据   ***
 int insertPost(linklist_t *p,int post,datatype_t data)
 {
     //1.
     linklist_t *pnew=NULL;
   if(post<0 || post>len)
   {
       printf("errno\n"); 
       return -1;
       //返回-1.
   }
   //2.malloc开辟节点空间。
       pnew=linklist_t *p=(linklist_t *)malloc(sizeof(linklist_t))
       //pnew=malloc;
    if(NULL==pnew)
    {
        printf("err\n");
        return -1;
    }
    //3.初始化节点(data)
     pnew->data=data;
     pnew->next=NULL;
 //4.插入
    //for循环  1)将头指针移动到插入位置的前一个位置
   for(int i=0;i<post;i++)
     p=p->next;    //?
     // 2)先用新节点的next保存后节点的地址
     pnew->next=p->next;
     p->next=pnew;
     return 0;
 }
 //3.链表的长度
 int lenLink(linklist_t *p)
 {
     int len=0;
     while(p->next !=NULL)
     {
       printf("%d ",p->next);
       len++;
     }
     return len;
 }
//4遍历链表                      ***
void showLink(linklist_t *p)
{
  while(p->next !=NULL)
  {
   p=p->next;  ///
   printf("%d ",p->data);
  }
  putchar(10);
}

//5删除链表指定位置数据  post=2     ***
   //指定位置删除数据
   int delecData(linklist_t *p,int post)
   {
       linklist_t *pdel=NULL;
       //容错处理
    if (post<0 || post>=len)
    {
     printf("%d ",err\n);
     return -1;
    }
   for(int i=0;i<post;i++)
   {
       p=p->next;
   }
      pdel=p->next;
      p->next=pdel->next;
     free(pdel);
     pdel=NULL;
   return 0;
   }

//6修改指定位置de数据
 int changePost(linklist_t *p,int post,datatype_t data)
 {
      if (post<0 || post>=len)
    {
     printf("%d ",err\n);
     return -1;
    }
     for (int i=0;i<=post;i++)
     {
         p=p->next;
     }
       p->data=data;
       return 0;
 }
//7查询指定数据的位置
int   searchPost(linklist_t *p,int post)
{ 
   int post=0;
   if(isEmptyLink(p))//判断是否为空
   {
       printf("err\n");
       return -1;
   }
    while (p->next!=NULL)
    {
        p=p->next;
        if(p->data==data)//当前位置数据等于查找的   找位置
        {
        return post;//返回位置
        }
        post++;//后加加。
    }
    return -1;
}

//8.删除指定数据  不需要返回值
void deleData(linklist_t *p,datatype_t data) //链表和数据
{
    linklist_t *pdel=NULL;//定义一个新指针
  while(p->next !=NULL)  //循环遍历
  {
      if(p->next->data==data)
      {
          pdel=p->next;
          p->next=pdel->next;
          free(pdel);//释放空间
          pdel=NULL;
      }
      else
      {
          p=p->next;
      }
      
  }
}
//9。
//  清空链表
 void clearList(linklist_t *p)
 {
     while (p->next !=NULL)
     {
         deleData(p,0);
     }
     
 }

//10链表的倒置          ***
void daozhi(linklist_t *p)
{
    linklist_t *ph=NULL;
    linklist_t *pt=p->next;
    p->next=NULL;

   while (pt !=NULL)
   {
       ph=pt;
       pt=pt->next;
       ph->next=p->next;
       p->next=ph;
   }
}

//11判断链表是否为空
int isEmptyLink(linklist_t *p)
{
    return p->next==NULL;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值