Linear table of data structures (C language)

Source: Internet
Author: User

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
/*
Defines a struct that contains
Holds the pointer field base to the dynamic array pointer,
The effective length of the dynamic array is cent,
The total length of the dynamic array
*/
struct Student
{
int* Base;
int cent;
int length;
};
Declaration of a function
void Init_list (struct student* str,int len);
BOOL Append_list (struct student* str,int val);
BOOL Insert_list (struct student* str,int pos,int val);
BOOL Delete_list (struct student* str,int pos,int* val);
void show_list (struct student* str);

Int main () {
struct Student str;//defines the struct-body variable str
int val;
Init_list (&str,6);//Call the Init_list function to assign a value to the variable in str
append_list (&str,10);//Add elements to a dynamic array
Append_list (& STR,20);
Append_list (&str,30);
Append_list (&str,40);
Append_list (&str,50);
//append_list (&str,60);
//append_list (&str,70);
printf ("Linear table:");
Show_list (&STR);//traverse Linear table
Putchar (' \ n ');
if (Insert_list (&str,2,5))//determine if the insertion element succeeds, the insertion succeeds in outputting a linear table, or the reason for the output insertion failure is
{
printf ("linear table after inserting element:");
Show_list (&STR);
}
Putchar (' \ n ');
if (Delete_list (&str,2,&val))//To determine whether the delete element succeeds, the deletion succeeds then the output deletes the element and the linear table, otherwise the output deletion fails because of
{
printf ("deleted element:%d\n", Val);
printf ("Linear table after deleting elements:");
Show_list (&STR);
}
Putchar (' \ n ');
return 0;
}

/*
The address of the passed-in struct variable and the length of the dynamic array,
Initialize struct variable str,
The effective length of STR is assigned to Len, which is the total length of the 0,str value.
The base of Str points to the array of malloc dynamically allocated
*/
void Init_list (struct student* str,int len)
{
Str->base= (int*) malloc (sizeof (int) *len);//Dynamic allocation array
if (str->base==null)//Determine if memory is allocated successfully, failure exits
{
printf ("Memory allocation failed!") \ n ");
Exit (-1);
}
str->cent=0;
str->length=len;
}

/*
The address and appended value of the passed-in struct variable,
Assigns Val to the position labeled cent in the array,
Then cent++
*/
BOOL Append_list (struct student* str,int val)
{
if (str->cent>=str->length)//Determines whether valid lengths in the dynamic array are equal to the total length, and returns false if they are equal
{
printf ("!\n of Dynamic Array Space");
return false;
}
str->base[str->cent]=val;//append Val to the array
Cent+1 in the str->cent++;//structural body
return true;
}

/*
The address passed into the struct variable,
Traversing a dynamic array with a For loop
*/
void show_list (struct student* str)
{
if (str->cent<=0)//Determine if the array is empty, if it is empty, then do not execute for loop, return directly
{
printf ("Dynamic array is empty!\n");
Return
}
for (int i=0;i<str->cent;i++)//traversing dynamic array with for loop
{
printf ("%d\t", Str->base[i]);
}
Putchar (' \ n ');
Return
}

/*
The address, insertion position, and inserted value of the passed-in struct variable,
First find the inserted position Pos, back up the POS elements once,
Assigns Val to the position of the array labeled Pos-1
Then cent++
*/
BOOL Insert_list (struct student* str,int pos,int val)
{
if (str->cent>=str->length)//Determines if the array is full and returns False if it is full
{
printf ("Dynamic array is full!\n");
return false;
}
if (pos<1| | Pos>str->cent)//Determine if the inserted position is appropriate and, if not, return false
{
printf ("Insertion position unsuitable!\n");
return false;
}
for (int i=str->cent-1;i>=pos-1;i--)//With the For loop, the following table is pos-1 after the element is moved one position
{
str->base[i+1]=str->base[i];
}
str->base[pos-1]=val;//assigns the value of Val to the position labeled Pos-1
str->cent++;
return true;
}

/*
The address of the incoming struct variable, the location to be deleted, and the address of Val,
Find the location POS that needs to be deleted, assign the value of subscript pos-1 to *val,
Then the elements after the POS are all going forward once,
Last cent--
*/
BOOL Delete_list (struct student* str,int pos,int* val)
{
if (str->cent<=0)//To determine if the array is empty, and if it is empty, returns false directly
{
printf ("Dynamic array is empty!\n");
return false;
}
if (pos<1| | pos>str->cent)//To determine whether the location of the deletion is appropriate, if not appropriate, then return False
{
printf ("The location of the deleted element does not exist!\n");
return false;
}
*val=str->base[pos-1];//Store the value of subscript pos-1 in *val
for (int i=pos;i<str->cent;i++)//With the For loop, the elements after the subscript pos-1 are advanced once
{
str->base[i-1]=str->base[i];
}
str->cent--;
return true;
}

Linear table of data structures (C language)

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.