*Copyright (c)2017, School of computer and control engineering, Yantai University
*All rights reservrd.
*Author: Li Xinhao
*Completion time: December 7, 2017
*Version number: v1.0
*Problem Description: define a linear table stored in a sequential structure, and design the algorithm to complete the following work:
1. Delete all elements between [x, y]. The time complexity of the algorithm is O(n), and the space complexity is O(1);
2. Moving the odd number to the front of all even numbers requires that the time complexity of the algorithm is O(n) and the space complexity is O(1).
1, Solution to problem 1: create a new project, edit the header file list.h
#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #define MaxSize 50 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int length; } SqList; void CreateList(SqList *&L, ElemType a[], int n);//Creating linear tables with arrays void InitList(SqList *&L);//Initialize linear table initlist void DestroyList(SqList *&L);//Destroy linear table destroylist bool ListEmpty(SqList *L);//Determine whether it is an empty table listempty int ListLength(SqList *L);//Find the length of linear table listlength void DispList(SqList *L);//Output linear table displist bool GetElem(SqList *L,int i,ElemType &e);//Get a data element value GetElem(L,i,e) int LocateElem(SqList *L, ElemType e);//Find locateelem by element value (L, e) bool ListInsert(SqList *&L,int i,ElemType e);//Insert data element ListInsert(L,i,e) bool ListDelete(SqList *&L,int i,ElemType &e);//Delete the data element ListDelete (L, I, e) settings void delx2y(SqList *&L, ElemType x, ElemType y); void unionList(SqList *LA, SqList *LB, SqList *&LC); #endif
Define these functions in the source file list.cpp
#include <stdio.h> #include <malloc.h> #include "list.h" //Creating linear tables with arrays void CreateList(SqList *&L, ElemType a[], int n) { int i; L=(SqList *)malloc(sizeof(SqList)); for (i=0; i<n; i++) L->data[i]=a[i]; L->length=n; } //Initialize linear table initlist void InitList(SqList *&L) //Referential pointer { L=(SqList *)malloc(sizeof(SqList)); //Allocate space for linear tables L->length=0; } //Destroy linear table destroylist void DestroyList(SqList *&L) { free(L); } //Determine whether it is an empty table listempty bool ListEmpty(SqList *L) { return(L->length==0); } //Find the length of linear table listlength int ListLength(SqList *L) { return(L->length); } //Output linear table displist void DispList(SqList *L) { int i; if (ListEmpty(L)) return; for (i=0; i<L->length; i++) printf("%d ",L->data[i]); printf("\n"); } //Get a data element value GetElem(L,i,e) bool GetElem(SqList *L,int i,ElemType &e) { if (i<1 || i>L->length) return false; e=L->data[i-1]; return true; } //Find locateelem by element value (L, e) int LocateElem(SqList *L, ElemType e) { int i=0; while (i<L->length && L->data[i]!=e) i++; if (i>=L->length) return 0; else return i+1; } //Insert data element ListInsert(L,i,e) bool ListInsert(SqList *&L,int i,ElemType e) { int j; if (i<1 || i>L->length+1) return false; //Return false when parameter error i--; //Convert logical sequence number of sequence table to physical sequence number for (j=L->length; j>i; j--) //Move the data[i..n] element back one position L->data[j]=L->data[j-1]; L->data[i]=e; //Insert element e L->length++; //Sequence table length increased by 1 return true; //Insert successfully returns true } //Delete data element ListDelete(L,i,e) bool ListDelete(SqList *&L,int i,ElemType &e) { int j; if (i<1 || i>L->length) //Return false when parameter error return false; i--; //Convert logical sequence number of sequence table to physical sequence number e=L->data[i]; for (j=i; j<L->length-1; j++) //Move the data[i..n-1] element forward L->data[j]=L->data[j+1]; L->length--; //Sequence table length minus 1 return true; //Delete successfully return true } void unionList(SqList *LA, SqList *LB, SqList *&LC) { int lena,i; ElemType e; InitList(LC); for (i=1; i<=ListLength(LA); i++) //Insert all elements of LA into Lc { GetElem(LA,i,e); ListInsert(LC,i,e); } lena=ListLength(LA); //Find the length of linear table LA for (i=1; i<=ListLength(LB); i++) { GetElem(LB,i,e); //Assign the ith data element in LB to e if (!LocateElem(LA,e)) //LA does not exist the same as e, insert into LC ListInsert(LC,++lena,e); } } void delx2y(SqList *&L, ElemType x, ElemType y) { int k=0,i; //k records the number of non-x elements ElemType t; if(x>y) { t=x; x=y; y=t; } for (i=0; i<L->length; i++) if (L->data[i]<x || L->data[i]>y ) //Copy elements not between [x, y] { L->data[k]=L->data[i]; k++; } L->length=k; }
Implement the test in the main.cpp file:
#include <stdio.h> #include <malloc.h> #include "list.h" int main() { SqList *sq; ElemType a[10]= {5,8,7,0,2,4,9,6,7,3}; CreateList(sq, a, 10); printf("Before deleting "); DispList(sq); delx2y(sq, 4, 7); printf("After deletion "); DispList(sq); return 0; }
The screenshot of the test results is as follows:
2, Solution to problem 2:
Add a function declaration in the header file list.h of question 1:
void move(SqList *&L);
Add the definition of this function in the source file list.cpp:
void move(SqList *&L) { int i=0,j=L->length-1; ElemType tmp; while (i<j) { while ((i<j) && (L->data[j]%2==0)) //From right to left, find the first odd number (ignore even numbers) j--; while ((i<j) && (L->data[i]%2==1)) //From left to right, find the first even number (ignore odd number) i++; if (i<j) //If the boundary line is not reached, exchange the odd number on the right and the even number on the left { tmp=L->data[i]; L->data[i]=L->data[j]; L->data[j]=tmp; } } //After cycling up, continue searching and exchange if necessary }
Re edit the main function:
int main() { SqList *sq; ElemType a[10]= {5,8,7,0,2,4,9,6,7,3}; CreateList(sq, a, 10); printf("Before operation "); DispList(sq); move(sq); printf("After operation "); DispList(sq); return 0; }
The screenshot of the test results is as follows: