7.25(Day 3)

1.思维导图

2.存放学生的信息。

i) 独立完成今天顺序表的创建、判空、判满、增加、遍历、任意位置插入数据。(要求:尽量每行代码都有注释)

ii) 完成顺序表的任意位置删除数据功能函数的封装。

(1).student.c内容
#include "student.h"
//创建顺序表函数
seqptr seq_create()
{
  seqptr p=(seqptr)malloc(sizeof(seq));
  if(NULL == p)
  {
	  printf("申请失败!\n");
	  return NULL;
  }
 else
	{
		printf("申请成功!\n");
	}
memset(p->st,0,sizeof(p->st));
p->len=0;
return p;
}
//判空函数
int empty(seqptr p)
{
  if(NULL == p)
  {
    printf("判空失败!\n");
	return -1;
  }
  return p->len == 0;
}
//判满函数
int full(seqptr p)
{
	if(NULL == p)
	{
		printf("判满失败!\n");
		return -1;
	}
	return p->len == MAX;
}
//顺序表的增加
int seq_add(seqptr p,char *d1,int d2,int d3)
{
  if(NULL == p ||full(p))                                       
    {                                                          
        printf("增加失败!\n");                                
        return 0;                                              
    }                                                          
                                                               
    strcpy(p->st[p->len].name, d1);
	p->st[p->len].id=d2;
	p->st[p->len].score=d3;                                      
    p->len++;                                                  
    return 1;
}
//顺序表的遍历
void show(seqptr p)
{
    if(NULL == p || empty(p))
    {
        printf("遍历失败!\n");
        return;
    }
    else
    {
        for(int i=0;i<p->len;i++)
        {
            printf("%s\t%d\t%d\n ",p->st[i].name,p->st[i].id,p->st[i].score);
        }
        printf("\n");
    }
}
//任意位置插入数据
int seq_input(seqptr p,int n,char *d1,int d2,int d3)
{
	if(NULL == p || full(p) || n<=0 || n>MAX || n>p->len)
	{
		printf("插入失败!\n");
		return 0;
	}
	n=n-1;
	for(int i=0;i<p->len-n;i++)
	{
		p->st[p->len-i]=p->st[p->len-1-i];
	}
    strcpy(p->st[n-1].name, d1);
	p->st[n-1].id=d2;
	p->st[n-1].score=d3;

    p->len++;                                                  
    return 1;
	}
//任意位置删除数据
int seq_del(seqptr p,int n)
{
	if(NULL == p || n<=0 || n>MAX || n>p->len)
	{
		printf("删除失败!\n");
		return 0;
	}
	for(int i=0;i<p->len-n+1;i++)
	{
		p->st[n-1+i]=p->st[n+i];
	}
	p->len--;
}
(2)student.h内容
#ifndef __STUDENT_H_
#define __STUDENT_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 30
typedef struct student
{
   char name[20];
   int id;
   int score;
}stu;

typedef struct stulist
{
  int len;
  stu st[MAX];
}seq,*seqptr;

seqptr seq_create();

int empty(seqptr p);
int full(seqptr p);

int seq_add(seqptr p,char *d1,int d2,int d3);

void show(seqptr p);
int seq_input(seqptr p,int n,char *d1,int d2,int d3);
int seq_del(seqptr p,int n);
#endif
(3)main.c内容
#include "student.h"
int main(int argc, const char *argv[])
{
	seqptr p=seq_create();
	seq_add(p,"张三",1001,63); 
    seq_add(p,"李四",1002,72);
    seq_add(p,"王五",1003,82);
    seq_add(p,"梁七",1004,92);

    show(p);

	seq_input(p,3,"jiang",1008,88);
	show(p);

	seq_del(p,2);
	show(p);

	return 0;
}
(4)编译并运行代码
编译:gcc *.c    运行:./a.out
结果:
申请成功!
 张三	1001	63
 李四	1002	72
 王五	1003	82
 梁七	1004	92
 
 张三	1001	63
 jiang	1008	88
 王五	1003	82
 王五	1003	82
 梁七	1004	92
 
 张三	1001	63
 王五	1003	82
 王五	1003	82
 梁七	1004	92

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值