⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 052335_chapter2(1).c

📁 在Visual C++6.0下编译通过 完成表的建立、插入、查找、删除操作
💻 C
字号:
/*
**052335 周觅源 第二章第一次作业  在Visual C++6.0下编译通过
  完成表的建立、插入、查找、删除操作
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 20
#define LISTINCREMENT 10
#define N 10			
typedef int Status;

struct LIST{
	int *elem;
	int length;
	int listsize;
};

typedef struct LIST sqList;

Status Createlist_Sq(sqList *L,int n){
	int i;
	L->elem=(int*) malloc (LIST_INIT_SIZE*sizeof(int));
	if (!L->elem) exit(OVERFLOW);
	L->length=0;
	L->listsize=LIST_INIT_SIZE;
	for (i=0;i<n;i++) {scanf("%d",&L->elem[i]);L->length++;}
	printf("\n");
	for (i=0;i<n;i++) printf("%d, ",L->elem[i]);
	printf("\n");
	return OK;
}

Status ListInsert_Sq (sqList *L,int i,int e){
	int* p; int* q; int* newbase;
	if (i<1||i>L->length+1) return ERROR;
	if (L->length>=L->listsize){
		newbase=(int *) realloc (L->elem,L->listsize+LISTINCREMENT*sizeof(int));
		if (!L->elem) exit (OVERFLOW);
		L->elem=newbase;
		L->listsize+=LISTINCREMENT;
	}
	q=&(L->elem[i-1]);
	for (p=&(L->elem[L->length-1]);p>=q;p--)
		*(p+1)=*p;
	*q=e;
	++L->length;
	return OK;
}

Status ListDelete_Sq (sqList *L,int i){
	int* p; int* q;
	if (i<1||i>L->length) return ERROR;
	p=&(L->elem[i-1]);
	q=(L->elem)+(L->length)-1;
	for (p++; p<=q; p++) *(p-1)=*p;
	--L->length;
	return OK;
}

Status LocateList_Sq (sqList *L,int e){
	int i=1;
	while (i<=L->length && e!=L->elem[i-1]) ++i;
	if (i<=L->length) return i;
	else return 0;
}

main(){
	sqList Lista;
	int num=N;
	int choice;
	int In_num,In_place,Del_place,Find_num,i;
	printf("Please enter 10 numbers separated by space.\n");
	Createlist_Sq(&Lista,num);
	for (;;)
	{printf("*******************************\n");
	printf("Please select an operation you want: \n");
	printf("1. Insert a number\n");
	printf("2. Delete a number\n");
	printf("3. Find a number\n");
	printf("4. Print the List\n");
	printf("5. Exit\n");
	printf("*******************************\n\n");
	scanf("%d",&choice); getchar();
	switch (choice)
	{case 1:{
		printf("Please enter a number you want to insert:\n");
		scanf("%d",&In_num);
		printf("Please enter a number where want to insert:\n");
		scanf("%d",&In_place);
		ListInsert_Sq(&Lista,In_place,In_num);};break;
	case 2:{
		printf("Please enter a number where want to delete:\n");
		scanf("%d",&Del_place);
		ListDelete_Sq(&Lista,Del_place);};break;
	case 3:{
		printf("Please enter a number you want to find:\n");
		scanf("%d",&Find_num);
		if (LocateList_Sq(&Lista,Find_num)!=0)
		printf("the number \'%d\' you wanted is the  %dth number \n",Find_num,LocateList_Sq(&Lista,Find_num));
		   else printf("the number you wanted is not in the List. \n");}break;
	case 4:{
		for (i=0;i<=(Lista.length-1);i++) printf("%d, ",Lista.elem[i]); printf("\n");}break;
	case 5: return EXIT_SUCCESS;break;
    default:printf("Error\n");break;}
	;	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -