Data structure practice--Sequential table application

Source: Internet
Author: User

"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&LT;J) {while (i<j) && (L- Data[j]%2==0)] From right to left, find the first odd number (even ignore whatever) J--;while (I&LT;J) && (L- Data[i]%2==1)] From left to right, find the first even number (an odd number ignores whatever) i++;if(I&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.