数据结构——顺序表(C语言编写)

一、顺序表创建

输入

1 2 3 4 5 -1

 输出

1 2 3 4 5

题目分析

掌握使用  last  作为结构体的下标,不要自己出现奇怪的想法,要学会使用一些约定俗成的结构

#include<stdio.h>
#include<stdlib.h>

typedef int datatype;
#define MAXLEN 50

typedef struct
{
    datatype data[MAXLEN];
    int last;
}SeqList;

SeqList* CreatSeqList()
{
    SeqList *head=(SeqList *)malloc(sizeof(SeqList));
    head->last=-1;
    int x;
    while(scanf("%d",&x),x!=-1)
    {
        head->last++;
        head->data[head->last]=x;
    }
    return head;
}

void ShowSeqList(SeqList* L)
{
    SeqList *p=L;
    for(int i=0;i<L->last;i++)
        printf("%d ",L->data[i]);
    printf("%d",L->data[L->last]);
}

int main()
{
    SeqList *L;
    L=CreatSeqList();
    ShowSeqList(L);
    return 1;
}

二、顺序表的插入删除

输入

121 aa 95 1

129 bb 90 3

111 cc 80 2

444 dd 70 2

1 1 1 -1 3

 输出

插入成功

121 aa 95

位置出错

121 aa 95

插入成功

121 aa 95

111 cc 80

插入成功

121 aa 95

444 dd 70

111 cc 80

请输入删除位序:

删除后:

121 aa 95

444 dd 70

 题目分析

注意一些边界条件,熟练掌握顺序表的插入和删除

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 50
typedef struct student //学生结构体类型
{
   int num;
   char name[20];
   int n;
}stu;
    
typedef stu DataType;


typedef struct             //顺序表类型
{ 
    DataType data[MAXLEN];
    int last;
}SeqList;

SeqList* InitSeqList()            //创建一个空的顺序表
{
   SeqList *L;
   L=(SeqList *)malloc(sizeof(SeqList));
   L->last=-1;
   return L;
}

int InsertSeqList(SeqList *Lq,int i,DataType x)
{
    int k;
    if(i<1 || i > Lq->last+2 || Lq->last==MAXLEN-1)
        return 0;
    for(k=Lq->last;k>=i-1;k--)
        Lq->data[k+1]=Lq->data[k];
    Lq->data[i-1].n=x.n;
    strcpy(Lq->data[i-1].name,x.name);
    Lq->data[i-1].num=x.num;
    Lq->last++;
	return 1; 
}

int DelSeqList(SeqList *Lq,int i)
{
    int k;
    if(Lq->last==-1 || i<1 ||i>Lq->last+1)
        return 0;
    for(k=i;k<=Lq->last;k++)
        Lq->data[k-1]=Lq->data[k];
    Lq->last--;
    return 1;
}

void ShowSeqList(SeqList *L)
{
    for(int i=0;i<=L->last;i++)
        printf("%d %s %d\n",L->data[i].n,L->data[i].name,L->data[i].num);
}

int main()
{
    SeqList *Lq;
    DataType x;
    int flag;             //flag用来记录操作是否成功
     int i;

    Lq=InitSeqList();
    
    i=0;//输入学生信息以及插入位置
    while(scanf("%d %s %d %d",&x.n, x.name, &x.num,&i),i!=-1)
    {
        if(InsertSeqList(Lq,i,x)==1)
        {
            printf("插入成功\n");
            ShowSeqList(Lq);
        }
        else
        {
            printf("位置出错\n");
            ShowSeqList(Lq);
        }
    } 

    printf("请输入删除位序:\n");
    scanf("%d",&i);
    flag=DelSeqList(Lq,i);         //删除顺序表中的某一个学生

    if(flag==1) 
    {
        printf("删除后:\n");
        ShowSeqList(Lq);   
    }    //删除成功时输出顺序表,删除不成功时显示“不存在第i个元素”
    else
        printf("不存在第%d个元素\n",i);
   return 1;
}   
    

 三、顺序表应用——查找公共元素

输入

5 4 1 3 2 8 7 -1

2 1 4 -1

 输出

4 1 2

 题目分析

熟练运用各种函数的传递

#include<stdio.h>
#include"stdlib.h"
#define MAXLEN  100

typedef  int  datatype;
typedef struct
{
   datatype  data [MAXLEN];
     int         last;
} SeqList;

SeqList* CreatList_Seq()
{
    SeqList *head=(SeqList*)malloc(sizeof(SeqList));
    head->last=-1;
    return head;
} 

int InsList_Seq (SeqList *Lq, int i, datatype x)
{
    int j;
    for(j=Lq->last;j>=i-1;j--)
        Lq->data[j+1]=Lq->data[j];
    Lq->data[i-1]=x;
    Lq->last++;
    return 1;
} 
int SearchList_Seq (SeqList *Lq , datatype x )
{
    int i=0;
    while(i<=Lq->last && Lq->data[i]!=x)
        i++;
    if(i<=Lq->last) return i;
    else return -1;
}

void ShowList_Seq(SeqList *Lq)
{
    int i;
    for(i=0;i<=Lq->last;i++)
        printf("%d ",Lq->data[i]);
}

datatype Getdata(SeqList *h,int i)
{
    return h->data[i];
}


void common(SeqList *a,SeqList *b,SeqList *c)
{       
    int i,j,pos;
    for(i=0;i<=a->last;i++)
    {
        pos = SearchList_Seq(b,Getdata(a,i));
        if(pos!=-1)
            InsList_Seq(c,c->last+2,Getdata(a,i));
    }
}

int main()
{
     SeqList *a,*b,*c;
     datatype x;
     
     a=CreatList_Seq();
     b=CreatList_Seq();
     c=CreatList_Seq();
     
    while(scanf("%d",&x),x!=-1)
        a->data[++a->last] = x;
    while(scanf("%d",&x),x!=-1)
        b->data[++b->last] = x;

     common(a,b,c);
     ShowList_Seq(c);
}

 四、顺序表应用——删除

 题目分析

学会顺序表删除操作

#include<stdio.h>
#include<stdlib.h>

typedef   int    datatype;
#define MAXLEN   100

typedef struct
{
    datatype data[MAXLEN];
    int last;
}SeqList;

SeqList *CreatList_Seq()
{
    SeqList *head=(SeqList *)malloc(sizeof(SeqList));
    head->last=-1;
    int x,i=0;
    while(scanf("%d",&x),x!=-1)
    {
        head->last++;
        head->data[head->last]=x;
    }
    return head;
}
 
int DelList(SeqList *L,int pos,int len)
{
        int k,i;
    if (len<0 || L->last==-1 || pos < 1 || pos > L->last+1|| len+pos-1 > L->last+1)   //下标越界
           return 0;
     
    for(k=pos;k<=L->last;k++)
        L->data[k-1]=L->data[k+len-1]; 
    L->last=L->last-len;
    return 1;
}
 
void ShowList_Seq(SeqList *L)
{
    for(int i=0;i<L->last;i++)
        printf("%d ",L->data[i]);
    if(L->last!=-1)
     printf("%d",L->data[L->last]);
}
 
int main()
{
    SeqList *L;
    int pos,len;
    L=CreatList_Seq();
    scanf("%d %d",&pos,&len);
    if(DelList(L,pos,len)==0) printf("删除失败\n");
    ShowList_Seq(L);
    return 0;
}

五、顺序表应用——合并顺序表

输入

1 3 5 7 -1

2 4 6 8 -1

 输出

1 2 3 4 5 6 7 8

 题目分析

掌握顺序表合并

#include<stdio.h>
#include <iostream>
using namespace std;
 
 
#define MAXLEN  100
 
typedef  int  datatype;
typedef struct
{
   datatype  data [MAXLEN];
     int     last;
} SeqList;
 
SeqList* CreatList_Seq()
{
 
    SeqList *head=new SeqList;
    head->last=-1;
    return head;
 
} 
int InsList_Seq (SeqList *Lq, int i, datatype x)
{
 
    int j;
    if(Lq->last == MAXLEN-1||i<1||i>Lq->last+2)
        return 0;
    for(j=Lq->last;j>=i-1;j--)
        Lq->data[j+1]=Lq->data[j];
    Lq->data[i-1]=x;
    Lq->last++;
    return 1;
 
} 
int SearchList_Seq (SeqList *Lq , datatype x )
{
    int i=0;
    while(i<=Lq->last && Lq->data[i]!=x)
        i++;
    if(i<=Lq->last) return i;
    else return -1;
}
 
void ShowList_Seq(SeqList *Lq)
{
    int i;
    for(i=0;i<=Lq->last;i++)
        cout<<Lq->data[i]<<' ';
}
datatype Getdata(SeqList *h,int i)
{ 
     return h->data[i];
}
 
void Union(SeqList *a,SeqList *b,SeqList *c)
{ 
    int i = 0, j = 0, k = 0;
    while( i <= a->last && j <= b->last)
    {
        if (a->data[i] <= b->data[j])
            c->data[k++] = a->data[i++];
        else
            c->data[k++] = b->data[j++];
    }
    while (i <= a->last) c->data[k++] = a->data[i++];
    while (j <= b->last) c->data[k++] = b->data[j++];
    c->last = k-1;
}
 
 
int main()
{
     SeqList *a,*b,*c;
     datatype x;
      
     a=CreatList_Seq();
     b=CreatList_Seq();
     c=CreatList_Seq();
 
     while(scanf("%d",&x),x!=-1)
        a->data[++a->last] = x;
     while(scanf("%d",&x),x!=-1)
        b->data[++b->last] = x;
           
     Union(a,b,c);
     ShowList_Seq(c);
}
 

五、多项式求和——顺序表实现

输入

2 2

1 1 

0 0

5 1

1 0

0 0

 输出

2 2 

6 1

1 0

 题目分析

掌握多项式求和

#include<stdio.h>
#include<malloc.h>
 
 
#define MAXLEN  100
typedef struct xznode 
{
     int m;       // 系数
     int n;       // 指数
}datatype; 
  
typedef struct
{
     datatype data[MAXLEN]; 
     int last;       
} SeqList; 
              
SeqList* InitSeqList() 
{ 
 
    SeqList *L;
    L=new SeqList;
    L->last=-1;
    return L;
 
} 
 
int InsSeqList(SeqList *L, int i, datatype x) 
{
	int j;
    for(j=L->last;j>=i;j--)
    {
        L->data[j+1]=L->data[j];
    }
    L->data[i]=x;
    L->last++;     
} 
 
int CreatSeqList(SeqList *L)
{ 
    int n,m,i=0;
    while(scanf("%d %d",&m,&n),m!=0||n!=0)
    {
        L->data[i].n=n;
        L->data[i].m=m;
        i++;
    }
    L->last=i-1;
}
 
int LenSeqList(SeqList *L)
{ 
    return L->last;
} 
     
int Add_Seq(SeqList *A,SeqList *B,SeqList *C)
{  
	int i=0,j=0,k=0;
    while(1)
    {
        if(i>LenSeqList(A)&&j>LenSeqList(B)) break;
        if(i>LenSeqList(A)&&j<=LenSeqList(B))
        {
            InsSeqList(C,k,B->data[j]);
            k++;j++;
        }
        if(i<=LenSeqList(A)&&j>LenSeqList(B))
        {
            InsSeqList(C,k,A->data[i]);
            k++;i++;
        }
        if(i<=LenSeqList(A)&&j<=LenSeqList(B))
        {
            if(A->data[i].n > B->data[j].n)
            {
                InsSeqList(C,k,A->data[i]);
                k++;i++;
            }
            else if(A->data[i].n < B->data[j].n)
            {
                InsSeqList(C,k,B->data[j]);
                k++;j++;
            }
            else if(A->data[i].n == B->data[j].n)
            {
                InsSeqList(C,k,B->data[j]);
                C->data[k].m=A->data[i].m+B->data[j].m;
                k++;j++;i++;
            }
        }
    }
 
}
 
int ShowSeqList(SeqList *L) 
{  
 
    int i;
    for(i=0;i<=LenSeqList(L);i++)
    {
        if(L->data[i].m!=0)
        printf("%d %d\n",L->data[i].m,L->data[i].n);
    }
 
} 
 
int main()
{
    SeqList *L1,*L2,*L3;
     
    L1=InitSeqList();    //初始化三个空表
    L2=InitSeqList();
    L3=InitSeqList();
     
    CreatSeqList(L1);    //创建两个顺序表  
    CreatSeqList(L2);
 
    Add_Seq(L1,L2,L3);   //L1 L2相加之后结果放入L3
    ShowSeqList(L3);
    return 1;
} 

猜你喜欢

转载自blog.csdn.net/qq_63761366/article/details/126899617