An array implementation of linear structures

Source: Internet
Author: User

#include <stdio.h>

#include <stdlib.h>

typedef struct

{

int *parr;

int Length;//array Maximum Capacity

int cnt;//current Array valid number

}sqlist;

void Init_arr (sqlist *,int length);

void Destroy_arr ();

void Remove_arr (sqlist *,int,int *);

void Append_arr (sqlist *,int);

void Insert_arr (sqlist *,int,int);

void Get_arr ();

void Replace_arr ();

void Travel_arr (sqlist *);

void Reverse_arr (sqlist *);

void Sort_arr (sqlist *);

int size ();

BOOL IsEmpty (sqlist *);

BOOL Isfull (sqlist *);

void Main ()

{

When we first started, this was written sqlist arr, instead of SqList * arr,

Because the benefit is to use the ARR variable at the time, you can use *arr here, otherwise there is no room.

SqList arr;

Initialize this linear table.

Init_arr (&arr,6);

Traverse this linear table.

Travel_arr (&arr);

Appends an element to a linear table.

printf ("start appending elements to the linear table \ n");

Append_arr (&arr,2);

Append_arr (&arr,4);

Append_arr (&arr,0);

Traverse this linear table

Travel_arr (&arr);

Inserting elements into a linear table

Insert_arr (&arr,1,3);

Traverse this linear table

Travel_arr (&arr);

Delete Element

int val;

Remove_arr (&arr,2,&val);

Traverse this linear table

Travel_arr (&arr);

Invert this linear table

Reverse_arr (&arr);

Traverse this linear table

Travel_arr (&arr);

printf ("start sorting \ n");

Sort this linear table

Sort_arr (&arr);

Traverse this linear table

Travel_arr (&arr);


}

void Init_arr (sqlist *arr,int Length)

{

arr->parr= (int *) malloc (sizeof (int) *length);

If (null==arr->parr)

{

printf ("dynamic memory allocation failed");

Exit (-1);

}

Else

{

arr->length=length;

arr->cnt=0;

}

}

BOOL IsEmpty (sqlist *arr)

{

If (arr->cnt==0)

Return true;

Else

Return false;

}

void Travel_arr (sqlist * arr)

{

Determine if the current table is empty, prompting the user to indicate that the linear table is empty

If (isEmpty (arr))

printf ("the Current Linear table is empty");

Else

{

Traversing a linear table

For (int I=0;i<arr->cnt;i++)

printf ("%d", arr->parr[i]);

printf ("\ n");

}

}

void Append_arr (sqlist *arr,int temp)

{

If (isfull (arr))

{

printf ("the Linear table is full and cannot continue appending");

}

Else

{

arr->parr[arr->cnt]=temp;

arr->cnt++;

}


}

BOOL Isfull (sqlist *arr)

{

If (arr->cnt==arr->length)

Return true;

Else

Return false;

}

POS refers to subscript

void Insert_arr (sqlist *arr,int pos,int Temp)

{

If (isfull (arr))

{

printf ("the Linear table is full and cannot be inserted");

}

Else

{

For (int i=arr->cnt-1;i>=pos;i--)

{

arr->parr[i+1]=arr->parr[i];

}

arr->parr[pos]=temp;

arr->cnt++;

}

}

void Remove_arr (sqlist *arr,int pos,int *val)

{

If (isEmpty (arr))

{

printf ("the element in the linear table is empty, there is no element");

}

Else

{

*val=arr->parr[pos];

For (int I=pos;i<arr->cnt-1;i++)

{

arr->parr[i]=arr->parr[i+1];

}


arr->cnt--;

}

}

symmetry, find the middle of the appropriate middle point, replacement, can Be. generally, The total length of the linear table/and the appropriate intermediate points are calculated.

This algorithm may not be Appropriate.

void Reverse_arr (sqlist *arr)

{

If (isEmpty (arr))

{

printf ("linear table is empty, does not produce inversion");

}

Else

{

/*

int temp;

For (int I=0;i<arr->cnt/2;i++)

{

temp=arr->parr[arr->cnt-1-i];

arr->parr[arr->cnt-1-i]=arr->parr[i];

arr->parr[i]=temp;

}

*/

int temp;

int i=0;

int j=arr->cnt-1;

While (i<j)

{

temp=arr->parr[j];

arr->parr[j]=arr->parr[i];

arr->parr[i]=temp;

i++;

j--;

}

}

}

Bubble sort

void Sort_arr (sqlist *arr)

{

int temp;

For (int I=0;i<arr->cnt-1;i++)

For (int J=0;j<arr->cnt-1-i;j++)

If (arr->parr[j]>arr->parr[j+1])

{

temp=arr->parr[j+1];

arr->parr[j+1]=arr->parr[j];

arr->parr[j]=temp;

}


}


This article is from the "jane's life" blog, please be sure to keep this source http://1464490021.blog.51cto.com/4467028/1863862

An array implementation of linear structures

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.