"Project-Sequential Table Application"
Defining a linear table with sequential structure storage, the design algorithm completes the following work:
1, the deletion of elements in [x, y] all elements, the algorithm requires the time complexity of O (n), the space Complexity of O (1);
2. Move the odd number to the front of all even numbers, requiring the algorithm to have a time complexity of O (n) and a space complexity of O (1).
Tips:
(1) Make full use of the previously established algorithm library to solve the problem of establishing the sequential table and outputting the linear table;
(2) To ensure the complexity of the requirements, design algorithms and special functions to implement the algorithm;
(3) write a program for each job and publish a blog post to showcase your practical results.
[refer to the answer] (Note: list.h and list.cpp required in the project see Click Reference ... )
1, the deletion of elements in [x, y] all elements, the algorithm requires the time complexity of O (n), the space Complexity of O (1);
#include "list.h"#include <stdio.h>//Remove elements from a linear table with element values between x and YvoidDELX2Y (SqList *&l, Elemtype x, elemtype y) {intk=0I//k record number of elements not xElemtype 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 in [X, Y]{l->data[k]=l->data[i]; k++; } l->length=k;}//write test code in mainintMain () {sqlist *sq; Elemtype a[Ten]= {5,8,7,0,2,4,9,6,7,3}; CreateList (Sq, A,Ten);printf("before deleting"); Displist (SQ); delx2y (Sq,4,7);printf("after delete"); Displist (SQ);return 0;}
2. Move the odd number to the front of all even numbers, requiring the algorithm to have a time complexity of O (n) and a space complexity of O (1).
#include "list.h"#include <stdio.h>After the move is over, odd numbers are left and even rightvoidMoveSqList*&L) {int i=0, j=L->length-1;Elemtypetmp while (I<J) {while (i<j) && (L- Data[j]%2==0)] From right to left, find the first odd number (even ignore whatever) J--;while (I<J) && (L- Data[i]%2==1)] From left to right, find the first even number (an odd number ignores whatever) i++;if(I<J)//If the "dividing line" is not reached, the odd and left-numbered interchange of the right is {tmp=L- Data[i]; L- Data[i]=L-data[j]; L- data[j]=tmp;}}//To continue searching after the loop, and Exchange}//if necessary write the test code with mainintMain () {SqList*SQ;Elemtypea[Ten]= {5,8,7,0,2,4,9,6,7,3};createlist(Sq, A,Ten); printf"before operation");displist(sq); Move (SQ); printf"after Operation");displist(sq); Return0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure practice--Sequential table application