|
发表于 2003-9-13 23:34:56
|
显示全部楼层
- /*线性表的插入与删除,排序操作. */
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h> /*为toupper();提供原型*/
- #define LIST_INIT_SIZE 100
- #define LISTINCREMENT 10
- typedef int ElemType;
- typedef struct
- {
- ElemType *elem; /*存储空间基址*/
- int length; /*当前长度*/
- int listsize; /*当前分配的存储容量*/
- }List;
- void Show_Choose_Menu ( void); /*显示菜单*/
- void Display_List ( List L); /*显示线性表内容*/
- List Construct_List ( List L); /*初始化*/
- List Insert_List ( List L); /*线性表插入操作*/
- List Delete_List ( List L); /*线性表删除操作*/
- void Sort_List ( List L); /*线性表排序操作*/
- List Sort_Method_A(List L); /*升序排列*/
- List Sort_Method_D(List L); /*降序排列*/
- List To_Empty_List ( List L); /*清空线性表*/
- void swap(ElemType *a,ElemType *b);
- int main (void)
- {
- char ch;
- List L;
- Show_Choose_Menu();
- printf("Please Input The First Letter Of The Word: ");
- scanf(" %c",&ch); /*特别注意这里的输入情况*/
- while ( (ch = toupper(ch)) != 'Q')
- {
- switch (ch)
- {
- case 'C': L= Construct_List(L); break;
- case 'I': L= Insert_List(L); break;
- case 'D': L= Delete_List(L); break;
- case 'S': Sort_List(L); break;
- case 'T': L= To_Empty_List (L); break;
- default : printf("\nYour Input Has Error!Again!\n"); break;
- }
- Show_Choose_Menu();
- printf("Please Choose The Operation By The First Letter: ");
- scanf(" %c",&ch);
- }
- printf("Thank You For Using My List Program!\n");
- printf("BYE!\n");
- return 0;
- }
- void Show_Choose_Menu(void)
- {
- printf(" \n Construt_List To_Empty_List Quit\n");
- printf(" Insert_List Delete_List Sort_List\n");
- }
- List Construct_List ( List L)
- {
- L.elem=(ElemType *) malloc (LIST_INIT_SIZE * sizeof(ElemType));
- if (!L.elem) exit(1);
- L.length = 0;
- L.listsize = LIST_INIT_SIZE;
- Display_List(L);
- return L;
- }
- void Display_List(List L)
- {
- int i;
- if(L.length == 0) printf("Now. No Value Exist In The List.Please Insert!");
- else {
- printf("Now After Your Operation.The List Is :\n ");
- for(i=0;i<L.length;i++)
- printf("%d ",*(L.elem+i));
- }
- }
- List Insert_List(List L)
- {
- int i;
- ElemType value;
- ElemType *newbase,*q,*p;
- printf("Please Input The Insert Number And Value: ");
- while(2 != scanf("%d%d",&i,&value) || i<1 || i>L.length+1)
- {
- printf("Input Error! Input Again Please: ");
- continue;
- }
- if(L.length>=L.listsize){
- newbase=(ElemType *) realloc(L.elem,
- (L.listsize+LISTINCREMENT) * sizeof(ElemType));
- if (!newbase) exit(1);
- 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 = value;
- ++L.length;
- printf("The Value %d Had Insert At %d \n",value,i);
- Display_List(L);
- return L;
- }
- List Delete_List(List L)
- {
- int i;
- ElemType value;
- ElemType *p,*q;
- printf("Please Input You Want Delete's Value's Number: ");
- while(1 != scanf("%d",&i) ||(i<1) || (i>L.length))
- {
- printf("Your Input Error. Input Again: ");
- continue;
- }
- p = &(L.elem[i-1]);
- value = *p;
- q = L.elem+L.length-1;
- for(++p;p<=q;++p)
- *(p-1) = *p;
- --L.length;
- printf("The Value %d Had Delete At %d \n",value,i);
- Display_List(L);
- return L;
- }
- void Sort_List(List L)
- {
- char c;
- printf("Please Choose Sort Method, A or D: ");
- scanf(" %c",&c);
- if(c == 'a') L= Sort_Method_A(L);
- else L=Sort_Method_D(L);
- Display_List(L);
- }
- List Sort_Method_A(List L)
- {
- int i,j;
- for (i=0;i<L.length;i++)
- for(j=0;j<i;j++)
- if(*(L.elem+i) > *(L.elem+j))
- swap(L.elem+i,L.elem+j);
- return L;
- }
- List Sort_Method_D(List L)
- {
- int i,j;
- for (i=0;i<L.length;i++)
- for(j=0;j<i;j++)
- if(*(L.elem+i) < *(L.elem+j))
- swap(L.elem+i,L.elem+j);
- return L;
- }
- List To_Empty_List (List L)
- {
- free (L.elem);
- L.length=0;
- Display_List (L);
- return L;
- }
- void swap(ElemType *a,ElemType *b)
- {
- ElemType temp;
- temp = *b;
- *b = *a;
- *a = temp;
- }
复制代码
This is indented automatically by vim |
|