作业:
1>顺序表按元素删除
思路:参数【顺序表删除的值key】1.根据删除的元素得到下标
2.根据下标实现【按下标删除】
2>顺序表排序【类似数组排序】
比较: if( list->datai]>list->data[j+1])
头文件:
#ifndef _HEAD_H
#define _HEAD_H
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 7
typedef int datatype;
typedef struct Format{
datatype data[MAXSIZE];
int len;
}form;
form *create(int n);
int empty_form(form *p);
int full_form(form *p);
int insert_rear(form *p,datatype e);
void output(form *p);
int delete_data(form *p,datatype key);
int search_sub(form *p,int sub);
#endif
main文件:
int main(int argc, const char *argv[])
{
int n;
datatype e;
printf("请输入n的值:");
scanf("%d",&n);
form *p=create(n);
for(int i=0;i<n;i++){
printf("请输入要插入的值:");
scanf("%d",&e);
insert_rear(p,e);
}
output(p);
printf("\n");
int sub;
printf("请输入要查找的下标:");
scanf("%d",&sub);
search_sub(p,sub);
datatype key;
printf("请输入要删除的元素下标:");
scanf("%d",&key);
datatype j=delete_data(p,key);
output(p);
printf("\n");
free(p);
p==NULL;
return 0;
}
自定义文件:
#include "head1.h"
form *create(int n){
form *p=(form *)malloc(sizeof(form)*n);
if(p==NULL)
return NULL;
p->len=0;
return p;
}
int full_form(form *p){
return p->len==MAXSIZE?-1:0;
}
int insert_rear(form *p,datatype e){
if(full_form(p)){
printf("顺序表已满\n");
return -1;
}
p->data[p->len]=e;
p->len++;
return 0;
}
int empty_form(form *p){
return p->len==0?-1:0;
}
void output(form *p){
for(int i=0;i<p->len;i++){
printf("%d\t",p->data[i]);
}
}
int search_sub(form *p,int sub){
if(empty_form(p)||sub<0||sub>=p->len){
printf("顺序表查找失败\n");
return -1;
}
printf("查找的元素是:%d\n",p->data[sub]);
return 0;
}
int delete_data(form *p,datatype key){
int i=0;
while(i!=key){
i++;
}
printf("被删除的元素为:%d\n",p->data[i]);
for(i=key+1;i<p->len;i++){
p->data[i-1]=p->data[i];
}
p->len--;
return 0;
}
运行结果:
作业2:
头文件:
#ifndef _HEAD_H
#define _HEAD_H
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 7
typedef int datatype;
typedef struct Format{
datatype data[MAXSIZE];
int len;
}form;
form *create(int n);
int empty_form(form *p);
int full_form(form *p);
int insert_rear(form *p,datatype e);
void output(form *p);
void exchange(form *p,int n);
#endif
main文件:
#include "head.h"
int main(int argc, const char *argv[])
{
int n;
datatype e;
printf("请输入n的值:");
scanf("%d",&n);
form *p=create(n);
for(int i=0;i<n;i++){
printf("请输入要插入的值:");
scanf("%d",&e);
insert_rear(p,e);
}
output(p);
printf("\n");
exchange(p,n);
output(p);
printf("\n");
free(p);
p==NULL;
return 0;
}
自定义文件:
#include "head.h"
form *create(int n){
form *p=(form *)malloc(sizeof(form)*n);
if(p==NULL)
return NULL;
p->len=0;
return p;
}
int full_form(form *p){
return p->len==MAXSIZE?-1:0;
}
int insert_rear(form *p,datatype e){
if(full_form(p)){
printf("顺序表已满\n");
return -1;
}
p->data[p->len]=e;
p->len++;
return 0;
}
int empty_form(form *p){
return p->len==0?-1:0;
}
void output(form *p){
for(int i=0;i<p->len;i++){
printf("%d\t",p->data[i]);
}
}
void exchange(form *p,int n){
for(int i=1;i<n;i++){
for(int j=0;j<n-1;j++){
if(p->data[j]>p->data[j+1]){
datatype t=p->data[j];
p->data[j]=p->data[j+1];
p->data[j+1]=t;
}
}
}
}
运行结果: