What is a sequence table
Order represents the sequential storage structure of linear table, that is, the storage structure of linear table constructed in sequential storage mode.
For the sequential table a with n elements, it can be expressed as A[0... N-1], with subscripts from 0 to n-1. A[0] is called the first element, A[1] is called the second element, and... A[n-1] is called the nth element.
Storage structure of sequential table
#define MaxLen 50 typedef int elemtype; //Define elemtype as int type typedef elemtype sqlist[MaxLen];
Creation and display of sequence table
#include<stdio.h> #define Maxlen 20 typedef int elemtype; typedef elemtype sqlist[Maxlen]; int create(sqlist A) { int i,n; printf("Create a sequence table:\n"); printf("Number of input elements:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Input No%d Elements:",i+1); scanf("%d",&A[i]); } return n; } void display(sqlist A,int n) { int i; printf("Output a sequence table:"); if(n==0){ printf("\n Empty table"); return; } for(i=0;i<n;i++) { printf("%4d",A[i]); } printf("\n"); }
Insertion of sequence table
Given that the elements in A sequence table A are non decreasing in order by value, write A function to insert an element x and keep the sequence table orderly.
#include "sqlist.cpp" int Insert(sqlist A,int n) { int m=n; int s; int j=0; printf("Please enter the insert element:"); scanf("%d",&s); while(j<1){ if(A[n-1]<s) { A[n]=s; j--; } else{ A[n]=A[n-1]; n--; } } n=m+1; } int main() { sqlist A; int n; n=create(A); display(A,n); n=Insert(A,n); display(A,n); }
Deletion of sequence table
Let A be the sequence table, and try to write an algorithm to delete k elements from the ith element in A.
#include "sqlist.cpp" int Delete(sqlist A,int n) { int j,k,i; printf("Please enter the location to delete j: "); scanf("%d",&j); printf("Please enter the number of deleted k:"); scanf("%d",&k); for(i=0;i<=n-k;i++) { A[j-1]=A[j+k-1]; j++; } n=n-k; return n; } int main() { sqlist A; int n; n=create(A); display(A,n); n=Delete(A,n); display(A,n); }
Consolidation of sequential tables
Write an algorithm to merge m (M > 2) ordered (from small to large) Order tables into an ordered order table. There is no new sequential table storage in the consolidation process.
#include"sqlist.cpp" int comb(sqlist A,int na,sqlist B,int nb) { int n=na,m=nb; if(na+nb > Maxlen) return 0; //Sequence table overflow while(nb>0) { if((na==0)||(A[na-1]<B[nb-1])) { //Note B[nb-1] is the element with the largest na+nb A[na+nb-1] = B[nb-1]; nb--; } else { A[na+nb-1] = A[na-1]; //Note that A[na-1] is the element with the largest na+nb na--; } } na = n + m; return na; } int main(){ sqlist A,B; int m; printf("Please enter the number of sequence tables:"); scanf("%d",&m); int na,nb; na = create(A); display(A,na); for(int i=1;i<m;i++) { nb = create(B); na = comb(A,na,B,nb); display(B,nb); } display(A,na); }
Arrangement of sequence table
Let A and B be two sequential tables, and their elements are arranged from small to large. Write an algorithm to form all elements in A and B into A new ordered table C from small to large, requiring the deletion of duplicate elements.
#include"sqlist.cpp" int comb(sqlist A,int na,sqlist B,int nb) { sqlist C; int n=na,m=nb; int temp; if(na+nb > Maxlen) return 0; //Sequence table overflow while(nb>0) { if((na==0)||(A[na-1]<B[nb-1])){ //Note B[nb-1] is the element with the largest na+nb A[na+nb-1] = B[nb-1]; nb--; } else{ A[na+nb-1] = A[na-1]; //Note that A[na-1] is the element with the largest na+nb na--; } } na = n + m; for(int i=0;i<na;i++) { C[i]=A[i]; if(A[i]==A[i+1]) { for(int j=i;j<=na-i;j++) { A[j+1]=A[j+2]; } na--; } } for(int i=0;i<na;i++){ A[i]=C[i]; } return na; } int main(){ sqlist A,B; int na,nb; na = create(A); display(A,na); nb = create(B); display(B,nb); na = comb(A,na,B,nb); display(A,na); }
Reverse order of sequence table
There is A sequential table A containing n elements. It is required to write an algorithm to reverse the table, and only one additional work unit is allowed to be added outside the storage space of the original table.
#include"sqlist.cpp" void invert(sqlist A,int n){ int m=n/2; int temp=0; for(int i=0;i<m;i++){ temp=A[i]; A[i]=A[n-i-1]; A[n-i-1]=temp; } } int main(){ sqlist A; int n; n = create(A); display(A,n); invert(A,n); display(A,n); return 0; }
Adjustment of sequence table
It is known that the element type of array A[0... n-1] is integer. Design the algorithm to adjust A so that all elements on the left are less than 0 and all elements on the right are greater than or equal to 0 (the time complexity and space complexity of the algorithm are required to be O(n))
#include<stdio.h> #include"sqlist.cpp" void adjust(sqlist A,int n) //Adjustment A { sqlist B; int i,x=0,y=n-1; for(i=0;i<n;i++) { if(A[i] < 0) { B[x] = A[i]; x++; }else{ B[y] = A[i]; y--; } } for(i=0;i<n;i++){ A[i] = B[i]; } } int main() { sqlist A; int n; n = create(A); display(A,n); adjust(A,n); display(A,n); return 0; }