Linear storage in C Language

Source: Internet
Author: User

In the data structure, the simplest data storage method is linear storage, while linear storage is classified into continuous storage and chain storage. Today we implement continuous storage.

In this program, the malloc function is used to construct a continuously stored memory space. Then, the function such as adding, deleting, modifying, querying, appending, and inserting is implemented, and the menu interface is implemented, users can freely choose how to operate on the requested space

On the menu interface, you can enter q to exit the program.

Source code:

/*************************************** * *********************************> File Name: line_bak.c> Author: Baniel Gao> Mail: createchance@163.com> Blog: blog.csdn.net/createchance> Created Time: mon 16 Dec 2013 20:04:58 CST ********************************** **************************************/# include
 
  
# Include
  
   
# Include
   
    
Void menu (void); int * create_array (void); void init_array (int * array, int n); int check_lenth (int const * array ); void add (int * array); void insert (int * array); void modify (int * array); void delete (int * array ); void quary (int const * array); void show_array (int const * array); int size; int main (void) {int * array = NULL; char choice = '\ 0'; int flag; array = create_array (); if (array = NULL) {return-1;} init_array (arr Ay, size); while (choice! = 'Q') {flag = 0; system ("clear"); printf ("Here are some choice: \ n"); menu (); printf ("Make your choice (press q to quit):"); while (scanf ("% c", & choice )) {if (choice <'1' | choice> '5' | flag = 1) & choice! = 'Q') {if (choice! = '\ N' & getchar ()! = '\ N') flag = 1; printf ("Make your choice (press q to quit):"); continue;} elsebreak; while (getchar ()! = '\ N');} while (flag & getchar ()! = '\ N'); if (choice = 'q') break; switch (choice) {case '1': add (array); show_array (array); break; case '2': show_array (array); delete (array); break; case '3': show_array (array); modify (array); break; case '4 ': show_array (array); insert (array); break; case '5': quary (array); break;} printf ("Press any key to continue ..... \ n "); getchar () ;}free (array); return 0 ;}void menu (void) {printf (" 1 ). add a number to the array. \ n "); printf ("2 ). delete a number from the array. \ n "); printf (" 3 ). modify a number of the array. \ n "); printf (" 4 ). insert a number into the array. \ n "); printf (" 5 ). quary a number from the array. \ n ");} int * create_array (void) {int * array = NULL; printf (" Please input the size of the array :"); while (scanf ("% d", & size )! = 1) {while (getchar ()! = '\ N'); printf ("The size must be a intger number! Please input again: \ n ");} while (getchar ()! = '\ N'); if (array = (int *) malloc (sizeof (int) * size) = NULL) {printf ("Memory allocate failed! \ N ") ;}return array;} void init_array (int * array, int n) {memset (array, 0, n);} int check_lenth (int const * array) {int length = 0; while (array [length ++]! = 0); return length-1;} void add (int * array) {int last = check_lenth (array); if (last = size) {printf ("The array is full! \ N "); return;} printf (" Please input the number you want to add: "); while (scanf (" % d ", array + last )! = 1) {while (getchar ()! = '\ N'); printf ("You must input a intger number! Try again: ");} while (getchar ()! = '\ N');} void insert (int * array) {int pos = 0, num, I; int length = check_lenth (array); if (length = size) {printf ("The array is full! \ N ");} if (length = 0) {printf (" The length of the array is 0, you can just add number to it! "); Add (array); return;} else {printf (" Which place do you want to insert?: "); While (scanf (" % d ", & pos )! = 1 | pos <1 | pos> length) {while (getchar ()! = '\ N'); printf ("Wrong position! Please input again: \ n ") ;}printf (" Please input the number you want to insert: "); while (scanf (" % d ", & num )! = 1) {while (getchar ()! = '\ N'); printf ("Invalid input! Please input again: \ n ");} while (getchar ()! = '\ N'); for (I = length-1; I> = pos-1; I --) {array [I + 1] = array [I];} array [pos-1] = num;} void modify (int * array) {int pos = 0; int length = check_lenth (array ); printf ("Which place do you want to modify?: "); While (scanf (" % d ", & pos )! = 1 | pos <1 | pos> length) {while (getchar ()! = '\ N'); printf ("Wrong position! Please input again: \ n ");} printf (" Please input the number you want to change: "); while (scanf (" % d ", array + pos-1 )! = 1) {while (getchar ()! = '\ N'); printf ("Invalid input! Please input again: \ n ");} while (getchar ()! = '\ N');} void delete (int * array) {int pos = 0, I; int length = check_lenth (array); if (length = 0) {printf ("The length is 0, you can not delete! \ N "); return;} printf (" Which place do you want to delete?: "); While (scanf (" % d ", & pos )! = 1 | pos <1 | pos> length) {while (getchar ()! = '\ N'); printf ("Wrong position! Please input again: \ n ");} while (getchar ()! = '\ N'); for (I = pos-1; I
    
     
Length) {while (getchar ()! = '\ N'); printf ("Invalid position! Please input again! \ N ") ;}while (getchar ()! = '\ N'); printf ("The positon % d you quary is: % d! \ N ", pos, array [pos-1]) ;}} void show_array (int const * array) {int length = check_lenth (array); int I = 0; if (length = 0) {printf ("The array is NULL! \ N "); return;} else {for (I = 0; I
     
      

Finally, due to my limited capabilities, there will certainly be bugs. If the high person finds out, please give a comment in time !! Thank you !!!

Related Article

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.