程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> C語言實現線性存儲之連續存儲

C語言實現線性存儲之連續存儲

編輯:關於C

在數據結構中,數據的存儲方式最簡單的是線性存儲,而線性存儲中分為連續存儲和鏈式存儲,今天我們實現連續存儲

在本程序中使用malloc函數構造一個連續存儲的內存空間,接著實現對其增,刪,改,查,追加,插入等功能,並實現了菜單式界面,由用戶自由選擇如何對申請到的空間操作

在菜單界面下,用戶可以輸入q來退出程序

源代碼:

/*************************************************************************
	> File Name: line_bak.c
	> Author: Baniel Gao 
	> Mail: [email protected] 
	> 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(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':
				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

最後,由於本人能力有限,其中肯定會有BUG,如果那位高人發現了,請及時給與評論指出啊!!謝謝啦!!!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved