顺序表

来源:互联网 发布:java编程入门视频教程 编辑:程序博客网 时间:2024/05/18 19:19
这是我写的头文件,和一般的顺序表没什么不同,加了2个功能,一个是元素去重,二是逆置表。程序里偷了个懒,用了unique和qsort函数(用sort会更方便,但也要写一个compare函数)。
为了测试所有的函数,我还写了一个主函数,代码给出,仅供参考。
很少写这种用指针的代码,一直都是直接用数组实现线性表的。

#ifndef SHUNXUBIAO_H_INCLUDED
#define SHUNXUBIAO_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include<algorithm>
#define LISTINITSIZE 100
#define LISTINCREAMENT 10
#define error 0
#define overflow -2
#define ok 1
using namespace std;
typedef  int ElemType;
typedef struct{
 ElemType *elem;
 int length;
 int listsize;
}SqList;
ElemType InitSqList(SqList &l);
void DestroySqList(SqList &l);
ElemType IsSqListEmpty(SqList &l);
ElemType IsSqListFull(SqList &l);
ElemType SqListLength(SqList &l);
ElemType GetSqListElement(SqList &l,int i);
ElemType SqListLocation(SqList &l,ElemType x);
ElemType SqListPrior(SqList &l,ElemType x);
ElemType SqListNext(SqList &l,ElemType x);
ElemType InsertSqList(SqList &l,int i,ElemType x);
ElemType DeleteSqList(SqList &l,int i);
void SortSqList(SqList &l);
void PrintSqList(SqList &l);
void listreverse(SqList &l);
ElemType listunique(SqList &l);
ElemType InitSqList(SqList &l)
{
 l.elem=new ElemType[LISTINITSIZE];
 if(!l.elem) { printf("failed initialize\n"); return overflow;}
 l.length=0;
 l.listsize=LISTINITSIZE;
 return ok;
}
void DestroySqList(SqList &l)
{
 l.elem=NULL;
 l.length=0;
 l.listsize=0;
}
ElemType IsSqListEmpty(SqList &l)
{
 if(!l.length) return 1;
 return 0;
}
ElemType IsSqListFull(SqList &l)
{
 if(l.length==l.listsize) return 1;
 return 0;
}
ElemType SqListLength(SqList &l)
{
 return l.length;
}
ElemType GetSqListElement(SqList &l,int i)
{
 if(i<1||i>l.length) { printf("illegal data\n"); return error;}
 return l.elem[i-1];
}
ElemType SqListLocation(SqList &l,ElemType x)
{
 for(int i=0;i<l.length;i++)
  if(l.elem[i]==x)
                return i+1;
                printf("no such date\n");
  return 0;
}
ElemType SqListPrior(SqList &l,ElemType x)
{
  for(int i=0;i<l.length;i++)
  if(l.elem[i]==x)
                return l.elem[i-1];
                printf("no such prior elem\n");
  return 0;
}
ElemType SqListNext(SqList &l,ElemType x)
{
                for(int i=0;i<l.length;i++)
  if(l.elem[i]==x)
                return l.elem[i+1];
                printf("no such next elem\n");
  return 0;
}
ElemType InsertSqList(SqList &l,int i,ElemType x)
{
 if(i<1||i>l.length+1) { printf("illegal position\n"); return error;}
 if(l.length==l.listsize) { printf("no available space\n");return error;}
 for(int j=l.length-1;j>i;j--)
 {
  l.elem[j+1]=l.elem[j];
 }
 l.elem[i-1]=x;
 l.length++;
 return ok;
}
ElemType DeleteSqList(SqList &l,int i)
{
 if(i<1||i>l.length) { printf("no such elem\n");return error;}
 for(int j=i-1;j<l.length-1;j++)
  l.elem[j]=l.elem[j+1];
 l.length--;
 return ok;
}
void listreverse(SqList &l)
{
        for(int i=0;i<=l.length/2;i++)
        {
                int t=l.elem[i];
                l.elem[i]=l.elem[l.length-i-1];
                l.elem[l.length-i-1]=t;
        }
}
int listunique(SqList &l)
{
        int m=unique(l.elem,l.elem+l.length)-l.elem;
        return m;
}
int cmp(const void *a,const void *b)
{
 return *(ElemType *)a-*(ElemType  *)b;
}
void SortSqList(SqList &l)
{
 qsort(l.elem,l.length,sizeof(l.elem[0]),cmp);
}
void PrintSqList(SqList &l)
{
 for(int i=0;i<l.length;i++)
        printf("%d\n",l.elem[i]);
}

#endif // SHUNXUBIAO_H_INCLUDED


#include<iostream>
#include"shunxubiao.h"
using namespace std;
int main()
{
        SqList l;
        InitSqList(l);
        int n;
        scanf("%d",&n);
        l.length=n;
        for(int i=0;i<n;i++)
        {
                l.elem[i]=i+1;
        }
        listreverse(l);
        PrintSqList(l);
        return 0;
}






在以上基础上修改了一点,实现了一个学生成绩管理的功能
#ifndef SHUNXUBIAO_H_INCLUDED
#define SHUNXUBIAO_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include<algorithm>
#define LISTINITSIZE 100
#define LISTINCREAMENT 10
#define error 0
#define overflow -2
#define ok 1
using namespace std;
typedef  int ElemType;
struct student
{
        char name[20]={'\0'},num[30]={'\0'};
 double mathscore=0,Englishscore=0,otherscore=0;
};
typedef struct{
 int *elem;
 int length;
 int listsize;
 student s[300];
}SqList;
ElemType InitSqList(SqList &l);
void DestroySqList(SqList &l);
ElemType IsSqListEmpty(SqList &l);
ElemType IsSqListFull(SqList &l);
ElemType SqListLength(SqList &l);
ElemType GetSqListElement(SqList &l,int i);
ElemType SqListLocation(SqList &l,ElemType x);
ElemType SqListPrior(SqList &l,ElemType x);
ElemType SqListNext(SqList &l,ElemType x);
ElemType InsertSqList(SqList &l,int i,ElemType x);
ElemType DeleteSqList(SqList &l,int i);
void SortSqList(SqList &l);
void PrintSqList(SqList &l);
void listreverse(SqList &l);
ElemType listunique(SqList &l);
ElemType InitSqList(SqList &l)
{
 l.elem=new ElemType[LISTINITSIZE];
 if(!l.elem) { printf("failed initialize\n"); return overflow;}
 l.length=0;
 l.listsize=LISTINITSIZE;
 return ok;
}
void DestroySqList(SqList &l)
{
 l.elem=NULL;
 l.length=0;
 l.listsize=0;
}
ElemType IsSqListEmpty(SqList &l)
{
 if(!l.length) return 1;
 return 0;
}
ElemType IsSqListFull(SqList &l)
{
 if(l.length==l.listsize) return 1;
 return 0;
}
ElemType SqListLength(SqList &l)
{
 return l.length;
}
ElemType GetSqListElement(SqList &l,int i)
{
 if(i<1||i>l.length) { printf("illegal data\n"); return error;}
 return l.elem[i-1];
}
ElemType SqListLocation(SqList &l,ElemType x)
{
 for(int i=0;i<l.length;i++)
  if(l.elem[i]==x)
                return i+1;
                printf("no such date\n");
  return 0;
}
ElemType SqListPrior(SqList &l,ElemType x)
{
  for(int i=0;i<l.length;i++)
  if(l.elem[i]==x)
                return l.elem[i-1];
                printf("no such prior elem\n");
  return 0;
}
ElemType SqListNext(SqList &l,ElemType x)
{
                for(int i=0;i<l.length;i++)
  if(l.elem[i]==x)
                return l.elem[i+1];
                printf("no such next elem\n");
  return 0;
}
ElemType InsertSqList(SqList &l,int i,ElemType x)
{
 if(i<1||i>l.length+1) { printf("illegal position\n"); return error;}
 if(l.length==l.listsize) { printf("no available space\n");return error;}
 for(int j=l.length-1;j>i;j--)
 {
  l.elem[j+1]=l.elem[j];
 }
 l.elem[i-1]=x;
 l.length++;
 return ok;
}
ElemType DeleteSqList(SqList &l,int i)
{
 if(i<1||i>l.length) { printf("no such elem\n");return error;}
 for(int j=i-1;j<l.length-1;j++)
  l.elem[j]=l.elem[j+1];
 l.length--;
 return ok;
}
void listreverse(SqList &l)
{
        for(int i=0;i<=l.length/2;i++)
        {
                int t=l.elem[i];
                l.elem[i]=l.elem[l.length-i-1];
                l.elem[l.length-i-1]=t;
        }
}
int listunique(SqList &l)
{
        int m=unique(l.elem,l.elem+l.length)-l.elem;
        return m;
}
int cmp(const void *a,const void *b)
{
 return *(ElemType *)a-*(ElemType  *)b;
}
void SortSqList(SqList &l)
{
 qsort(l.elem,l.length,sizeof(l.elem[0]),cmp);
}
void PrintSqList(SqList &l)
{
 for(int i=0;i<l.length;i++)
        printf("%d\n",l.elem[i]);
}

#endif // SHUNXUBIAO_H_INCLUDED


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include"shunxubiao.h"
using namespace std;
void add(SqList &l,char *name,char *number, double s1,double s2,double s3)
{
        for(int i=0;i<l.length;i++)
        {
                if(l.s[i].name!='\0')
                {
                strcpy(l.s[i].name,name);
                strcpy(l.s[i].num,number);
                l.s[i].mathscore=s1;
                l.s[i].Englishscore=s2;
                l.s[i].otherscore=s3;
                }
        }
}
void deleteinformation(SqList &l,char *name)
{
        for(int i=0;i<l.length;i++)
        {
                if(!strcmp(l.s[i].name,name))
                {
                memset(l.s[i].name,'\0',sizeof(l.s[i].name));
                memset(l.s[i].num,'\0',sizeof(l.s[i].num));
                l.s[i].mathscore=0,l.s[i].Englishscore=0,l.s[i].otherscore=0;
                }
        }
}
void modify(SqList &l,char *name)
{
        char ch;
        int score;
        for(int i=0;i<l.length;i++)
        {
                if(!strcmp(l.s[i].name,name))
                {
                printf("which subject  you want to modify?\n");
                for(;;)
                {
                        printf("math(0), English(1), other(2)\n");
                        ch=getche();
                        putchar('\n');
                        printf("please input corret score\n");
                        scanf("%d",&score);
                        switch(ch)
                        {
                        case '0':  l.s[i].mathscore=score; break;
                        case '1':  l.s[i].Englishscore=score; break;
                        case '2':   l.s[i].otherscore=score;  break;
                        default: printf("input error,please try again\n");
                        }
                        break;
                }
                }
        }
}
int main()
{
        int option,amount;
        char name[20],number[30];
        double s1,s2,s3;
        SqList l;
        InitSqList(l);
        printf("welcome to students' management operation\n");
        printf("1. add some score records\n\n");
        printf("2 .delete some score records\n\n");
        printf("3. correct some score records\n\n");
        printf("4. exit\n\n");
        for(;;)
        {
                printf("\t\tchoose an option\n");
                option=getche();
                putchar('\n');
                if(option=='1')
                {
                        printf("input the quantity you want to add: \n");
                        scanf("%d",&amount);
                        putchar('\n');
                        for(int i=0;i<amount;i++)
                        {
                        printf("name :\n");
                        scanf("%s",name);
                        printf("number :\n");
                        scanf("%s",number);
                        printf("math score: \n");
                        scanf("%lf",&s1);
                        printf("English score: \n");
                        scanf("%lf",&s2);
                        printf("other score \n");
                        scanf("%lf",&s3);
                        add(l,name,number,s1,s2,s3);
                        l.length++;
                        }
                        continue;
                }
                if(option=='2')
                {
                        printf("input the name that you want delete\n");
                        scanf("%s",name);
                        putchar('\n');
                        deleteinformation(l,name);
                        l.length--;
                        continue;
                }
                if(option=='3')
                {
                        printf("input the name that you want corret\n");
                        scanf("%s",name);
                        putchar('\n');
                        modify(l,name);
                        continue;
                }
                if(option=='4') break;
                else
                {
                        printf("input error,please try again\n");
                        continue;
                }
        }
        return 0;
}


原创粉丝点击