PTA 6-1 sequence table creation and in-place inverse __PTA data structure

Source: Internet
Author: User
6-1 sequential table creation and in-place inversion (10 points)

This requires the implementation of sequential table creation and in-place inverse operation functions. L is a sequential table, the function listcreate_sq (sqlist &l) is used to create a sequential table, and the function listreverse_sq (SqList &l) is to invert the elements in the order table without introducing a secondary array, If the original order table element is 1,2,3,4, then the inverse is 4,3,2,1. function Interface Definition:

Status listcreate_sq (SqList &l);
void listreverse_sq (SqList &l);
Sample Referee Test procedure:
The library function header file contains #include <stdio.h> #include <malloc.h> #include <stdlib.h>//Function status Code definition #define TRUE 1 #def INE FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE-1 #define OVERFLOW-2 typedef int ST

ATUs;  The storage structure of the sequential table is defined #define LIST_INIT_SIZE #define Listincrement elemtype;   It is assumed that the elements in the linear table are integral type typedef struct{elemtype* elem;       Storage space base address int length;     The number of elements in the table int listsize;    Table capacity size}sqlist;
The sequential table type defines the Status listcreate_sq (SqList &l);

void listreverse_sq (SqList &l);
    int main () {sqlist L;

    Elemtype *p; if (listcreate_sq (L)!= OK) {printf (listcreate_sq: Creation failed ...)
        \ n ");
    return-1;

    } listreverse_sq (L);
	if (l.length) {for (p=l.elem;p<l.elem+l.length-1;++p) {printf ("%d", *p); 
    printf ("%d", *p);
return 0;
 /* Please fill in the answer here/*

Input format: The first line enters an integer n, which indicates the number of elements in the order table, and the next n integers are table elements, separated by spaces in the middle. Output format: Output after the inverted sequence table elements, two elements separated by a space, the last element followed by no space. Input Sample:

4
1 2 3 4
Output Sample:
4 3 2 1

Status listcreate_sq (sqlist &l)
{
    l.elem= (elemtype*) malloc (list_init_size*sizeof (Elemtype));
    Create a single linked list and allocate memory
    if (! L.elem)
        exit (OVERFLOW);
    l.length=0;
    l.listsize=list_init_size;//allocation single linked list maximum capacity
    int n;
    scanf ("%d", &n);
    for (int i=0;i<n;i++)
    {
        scanf ("%d", l.elem+i);
        l.length++;//Write data, length Gaga
    } return
    OK;
}
void listreverse_sq (SqList &l)
{
    Elemtype temp;
    for (int i=0;i<l.length/2;i++)//Kinsoku trilogy
    {
        temp=* (l.elem+i);
        * (l.elem+i) =* (l.elem+l.length-i-1);
        * (l.elem+l.length-i-1) =temp
    }
}

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.