線性表是資料結構中最基礎的結構之一,其儲存方式分為連續儲存(順序表)與鏈式儲存(鏈表)。本文示範如何以 C 語言動態配置記憶體建立順序表,並實作新增、刪除、修改、查詢、插入、追加等核心操作,搭配選單式介面供使用者互動測試。
採用動態陣列 int *array 搭配全域變數 size 記錄總容量。實際元素個數由 check_length() 掃描陣列中非零值計算(預設以 0 為空值標記)。
int *create_array(void) {
printf("請輸入陣列大小:");
while (scanf("%d", &size) != 1) { while(getchar()!='\n'); printf("請輸入整數:"); }
while(getchar()!='\n');
int *arr = (int*)malloc(sizeof(int)*size);
if (!arr) printf("記憶體配置失敗\\n");
return arr;
}
void init_array(int *arr, int n) { memset(arr, 0, n*sizeof(int)); }
int main(void) {
int *array = create_array(); if (!array) return -1;
init_array(array, size);
char choice = '\0';
while (choice != 'q') {
system("clear");
menu();
printf("請選擇 (q 退出):");
while (scanf("%c", &choice)) {
if ((choice<'1' || choice>'5') && choice!='q' && choice!='\n') {
if (getchar()!='\n') flag=1;
printf("輸入錯誤,重新選擇:"); continue;
} else break;
}
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': query(array); break;
}
printf("按任意鍵繼續...\\n"); getchar();
}
free(array); return 0;
}
length 變數追蹤有效長度。scanf 回傳值檢查與緩衝區清理已處理,但可進一步封裝為通用輸入函式。realloc 實作向量擴容。void,建議回傳 int 狀態碼便於上層判斷。/*************************************************************************
> 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
void menu(void);
int *create_array(void);
void init_array(int *array, int n);
int check_length(int const *array);
void add(int *array);
void insert(int *array);
void modify(int *array);
void delete(int *array);
void query(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(array,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;
} else break;
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': query(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).query 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 integer 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*sizeof(int)); }
int check_length(int const *array) {
int length = 0;
while(array[length++] != 0) ; return length-1;
}
void add(int *array) {
int last = check_length(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 integer number!Try again: "); }
while(getchar() != '\n') ;
}
void insert(int *array) {
int pos = 0, num, i;
int length = check_length(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_length(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_length(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 position %d you query is : %d!\\n",pos,array[pos-1]);
}
}
}
void show_array(int const *array) {
int length = check_length(array);
int i = 0;
if(length == 0) { printf("The array is NULL!\\n"); return; }
else { for(i=0; i