|

楼主 |
发表于 2012-8-25 11:47:28
|
显示全部楼层
继续贴上代码:
main.c
================================================================
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
int main()
{
SqList list;
int i;
for(i=0;i<MAXSIZE-5;i++){ //数组长度为MAXSIZE,实际存储的线性表长度MAXSIZE-5
list.data[i] = i+1;
}
list.length = MAXSIZE-5;
ElementType e;
ElementType newValue;
int position;
Status result;
char choice;
printf("Enter 'G'(Query) 'I'(Insert) 'D'(Delete) 'U'(Update) to operate list:\n");
scanf("%c",&choice);
if(choice>='a' && choice<='z'){
choice = choice - 32;
}
switch(choice){
case 'G':
printf("Please enter a integer to find element !\n");
scanf("%d",&position);
result = getElement(list,position,&e);
if(result == SUCCESS){
printf("Element found: data[%d] = %d",position-1,e);
}
if(result == FAILURE){
printf("Element not found: data[%d]",position-1);
}
break;
case 'I':
printf("Please enter two numbers,the 1st one is postion,the 2nd is data :\n");
scanf("%d%d",&position,&e);
printf("position is: %d ; element is: %d\n\n",position,e);
printf("The data before inserting is: \n");
for(i=0;i<list.length;i++){
printf("%d ",list.data[i]);
}
printf("\n");
result = insertElement(&list,position,e);
if(result == SUCCESS){
printf("The data after inserting is: \n");
for(i=0;i<list.length;i++){
printf("%d ",list.data[i]);
}
printf("\n");
}
if(result == FAILURE){
printf("Inserting failed !\n");
}
break;
case 'D':
printf("Please enter a integer to delete element:\n");
scanf("%d",&position);
printf("The data before deleting is: \n");
for(i=0;i<list.length;i++){
printf("%d ",list.data[i]);
}
printf("\n");
result = deleteElement(&list,position,&e);
if(result == SUCCESS){
printf("The data after inserting is: \n");
for(i=0;i<list.length;i++){
printf("%d ",list.data[i]);
}
printf("\n");
}
if(result == FAILURE){
printf("Deleting failed !\n");
}
break;
case 'U':
printf("Please enter two numbers,the 1st one is postion,the 2nd is new data :\n");
scanf("%d%d",&position,&newValue);
printf("The data before updating is: \n");
for(i=0;i<list.length;i++){
printf("%d ",list.data[i]);
}
printf("\n");
result = updateElement(&list,position,newValue,&e);
if(result == SUCCESS){
printf("The data after updating is: \n");
for(i=0;i<list.length;i++){
printf("%d ",list.data[i]);
}
printf("\n");
}
if(result == FAILURE){
printf("Updating failed !\n");
}
break;
default:
printf("Operation undefined !");
break;
}
return 0;
}
|
|