C語言 數據結構 線性表的順序表示

已經大三下學期了,不準備考研,但是想把考研的相關專業課程複習複習,這段時間看看數據結構,看到第一章的線性表了,想到編一個程序實現一下子線性表的順序實現。本來剛開始想用c++的,後來想數據結構的書是c語言的版本,還是用turbo c做環境,這樣更富有挑戰。好啦,廢話少說,把代碼和大家分享把。
 如果代碼中有什麼錯誤希望您不吝賜教,有什麼問題發送到我的郵箱:[email protected]我會盡快回復的!

C語言 數據結構 線性表的順序表示


 

/****************************************/
/*Desciption:Sequence List*/
/*Email:[email protected]*/
/*Author:yi_landry Haerbin Normal University Computer Science*/
/*Date:2008-4-26*/
/*Copyright:HNU2008.cop*/
/*Environment:turbo c 2.01 English Version*/
/****************************************/

# include<stdlib.h>
# include<stdio.h>
# define LIST_INIT_SIZE 200/*distribute the memory size of the list*/
# define LIST_INCREMENT 20/*if the memory size overfolw ,everytime add LIST_INCREMENT*/
# define OVERFLOW 0
# define OK 1

struct SqList
{
  int * element;/*the base address of the sequece list*/
  int length;  /*current length*/
  int listsize;/*size of the sequence list*/
}L;

/****************************************/
/*Initial the list*/
/****************************************/
InitList(struct SqList * L)
{
  L->element=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
  if (! L->element) exit(OVERFLOW);
  L->length = 0;
  L->listsize = LIST_INIT_SIZE;
  printf("Initial the list successfuly!/n");
}

/****************************************/
/*Judge the list is empty or not*/
/****************************************/
ListEmpty(struct SqList * L)
{
  if (L->length ==0)
  {
    printf("The list is empty!/n");
  }
  else
  {
    printf("The list is not empty!/n");
  }
}

/****************************************/
/*Get the length of the list*/
/****************************************/
ListLength(struct SqList * L)
{
  printf("The length of the list is %d/n",L->length);
}

/****************************************/
/*Destroy the list and free the memory distributed at first*/
/****************************************/
DestroyList(struct SqList *L)
{
  free(L->element);
  L->length = 0;
  L->listsize = 0;
  printf("Destory the list successfully!/n");
}

/****************************************/
/*Clear all the elements the list*/
/****************************************/
ClearList(struct SqList * L)
{
  L->length = 0;
  printf("Clear the list successfully!/n");
}

/****************************************/
/*Get the element at tbe location i */
/****************************************/
GetElement(struct SqList * L,int i)
{
  int * p = L->element +i-1;
  printf("The element of Location %d is %d/n",i,(*p));
}

/****************************************/
/*Find a number if or not in the list*/
/****************************************/
FindElement(struct SqList * L,int elm)
{
  int *p,*q;
  int totalfind=0;
  for (p=L->element;p<=L->element+L->length-1;p++)
  {
    if ((*p)==elm)
    {
	printf("We can find  %d in the list at of the location of %d/n",elm,p-L->element+1);
	totalfind++;
    }
  }
  if (totalfind==0)
  {
     printf("We can not find %d in the list!/n",elm);
  }
  else
  {
     printf("We totally find %d <%d> in the list/n",totalfind,elm);
  }
}

/****************************************/
/*Get prior element of some element in the list*/
/****************************************/
PriorElement(struct SqList * L,int elm)
{
   int *p;
   int totalPrior=0;
   for (p = L->element+1;p<= L->element + L->length-1;p++)
   {
      if ((*p) == elm)
      {
     	printf("The prior of %d is %d/n",elm,*(p-1));
	    totalPrior++;
      }
   }
   if (totalPrior ==0)
   {
      printf("We can not find the prior of %d in the list!/n",elm);
   }
}

/****************************************/
/*Get next element of some element in the list*/
/****************************************/
NextElement(struct SqList * L,int elm)
{
   int *p;
   int totalNext=0;
   for (p = L->element;p<= L->element + L->length-2;p++)
   {
      if ((*p) == elm)
      {
     	printf("The prior of %d is %d/n",elm,*(p+1));
	    totalNext++;
      }
   }
   if (totalNext ==0)
   {
      printf("We can not find the next of %d in the list!/n",elm);
   }
}

/****************************************/
/*Insert a element into the list*/
/****************************************/
ListInsert(struct SqList * L,int i,int elm)
{
  int *p,*q;
  if (i < 1 || i >L->length+1)
  {
    printf("The location %d you input is not valid!/n",i);
  }
  else if (L->length >= L->listsize)
  {
    int * newbase = (int *)malloc((L->listsize+LIST_INCREMENT)*sizeof(int));
    if (!newbase) exit(OVERFLOW);
    L->element = newbase;
    L->listsize += LIST_INCREMENT;
  }
  else
  {
     q = L->element+i-1; /*The address of element i*/
     for (p = L->element+L->length-1;p >=q;p--)  *(p+1)= *p;
     * q = elm;
     L->length ++;
     printf("The element of %d insert into the list successfully!/n",elm);
  }
}

/****************************************/
/*Delete the element at the location i in the list*/
/****************************************/
ListDelete(struct SqList * L,int i)
{
   int *p,*q,deletedElm;
   if (i<0 || i>L->length+1)
   {
      printf("The location you input you want to delete is not valid!/n");
   }
   else
   {  q = L->element+i;
      deletedElm = *(q-1);
      for (p =q;p<=L->element+L->length;p++)
      {
	  *(p-1)=*p;
      }
      L->length--;
      printf("The element %d at location %d was deleted successfully!/n",deletedElm,i);
   }
}

/****************************************/
/*Print the list */
/****************************************/
PrintList(struct SqList * L)
{
  int *p,*q;
  if (L->length == 0)
  {
    printf("The list now is empty!/n");
  }
  else if (L->length == 1)
  {
    printf("The current list:/n%d",*(L->element));
  }
  else if (L->length == 2)
  {
     printf("The current list:/n%d->%d",*(L->element),*(L->element+1));
  }
  else if (L->length >2);
  {
    q = L->element +L->length-2;
    printf("The current list :/n");
    for (p = L->element;p<=q;p++)
    {
      printf("%d->",*p);
    }
    printf("%d/n",*(q+1));
  }
}

/****************************************/
/*Traverse the list*/
/****************************************/
ListTraverse(struct SqList * L)
{
   int *i,*j,*p,temp;
   i = L->element;
   j = L->element+ L->length-1;
   p = i+(L->length-1)/2;
   for (;i<p;i++,j--)
   {
      temp = * i;
      * i = * j;
      * j = temp;
   }
}

/****************************************/
/*Show a example to the user*/
/****************************************/
ListExample()
{
  InitList(&L);
  ListInsert(&L,1,1);
  ListInsert(&L,2,2);
  ListInsert(&L,3,3);
  ListInsert(&L,4,4);
  ListInsert(&L,5,5);
  ListInsert(&L,6,6);
  ListInsert(&L,5,50);
  ListInsert(&L,6,60);
  ListInsert(&L,7,7);
  ListInsert(&L,8,8);
  ListInsert(&L,9,9);
  FindElement(&L,6);
  FindElement(&L,7);
  FindElement(&L,10);
  PriorElement(&L,6);
  PriorElement(&L,10);
  GetElement(&L,3);
  PrintList(&L);
  ListTraverse(&L);
  PrintList(&L);
  ListDelete(&L,4);
  PrintList(&L);
  ListDelete(&L,6);
  PrintList(&L);
  ListInsert(&L,18,9);
  ListLength(&L);
  DestroyList(&L);
  ListLength(&L);
  return 0;
}

/****************************************/
/*Show the help information to the user*/
/****************************************/
void help()
{
  printf("      /*****************************************************************//n");
  printf("      /*****************************************************************//n");
  printf("      /*          The Sequence List Operation Version 1.0              *//n");
  printf("      /*             View the example information            1         *//n");
  printf("      /*             Insert a number into the list           2         *//n");
  printf("      /*             Delete a number from the list           3         *//n");
  printf("      /*          Find a number if or not in the list        4         *//n");
  printf("      /*       Get the prior of some number from the list    5         *//n");
  printf("      /*       Get the next  of some number from the list    6         *//n");
  printf("      /*                Get the size of list                 7         *//n");
  printf("      /*               Show the help information             8         *//n");
  printf("      /*             View current  list information          9         *//n");
  printf("      /*             View travesre list information          10        *//n");
  printf("      /*                  Exit Sequence List                 Q         *//n");
  printf("      /*****************************************************************//n");
}

main()
{
  char userInput;
  int insertItem,insertLocation,deleteLocation,findItem,priorItem,nextItem;
  help();
  InitList(&L);
  while(1)
  {
    printf("Slect the operation you want to do: input the correspond number!you can enter 1 to view the example./n");
    scanf("%c",&userInput);
    switch(userInput)
    {
      case '1':ListExample();break;
      case '2':
             for(;;)
             {
    	         printf("Please input a integer and the location you want to insert into the list:/n");
                 printf("Forexample 1 1 if you do not want to insert a number into the list again,you can input 0 0/n");
    	         scanf("%d %d",&insertItem,&insertLocation);
   	             if (insertItem == 0 && insertLocation == 0)
   	             break;
    	         ListInsert(&L,insertLocation,insertItem);
    	         PrintList(&L);
              }
	          break;
      case '3':
             for(;;)
             {
    	         printf("Please input  the location  of the element you want to delete from the list:/n");
                 printf("Forexample 2.if you do not want to delete a number from the list again,you can input -1/n");
    	         scanf("%d",&deleteLocation);
   	             if ( deleteLocation== -1)
   	             break;
    	         ListDelete(&L,deleteLocation);
    	         PrintList(&L);
              }
	          break;
      case '4':
             for(;;)
             {
    	         printf("Please input a integer  you want to find from the list:/n");
                 printf("Forexample 1 . if you do not want to find a number from the list again,you can input -1/n");
		 scanf("%d",&findItem);
   	             if (findItem == -1)
   	             break;
    	         FindElement(&L,findItem);
    	         PrintList(&L);
              }
	          break;
      case '5':
             for(;;)
             {
		       printf("Please input a integer  you want to get the prior from the list:/n");
		       printf("Forexample 1. if you do not want to get the prior form the list again,you can input -1/n");
		       scanf("%d",&priorItem);
		       if (priorItem == -1) break;
		       PriorElement(&L,priorItem);
    	       PrintList(&L);
              }
	          break;
      case '6':
            for(;;)
             {
		       printf("Please input a integer  you want to get the next from the list:/n");
		       printf("Forexample 1. if you do not want to get the next form the list again,you can input -1/n");
		       scanf("%d",&nextItem);
		       if (nextItem == -1) break;
		       NextElement(&L,nextItem);
    	       PrintList(&L);
              }
	          break;
      case '7':ListLength(&L);break;
      case '8':help();break;
      case '9':PrintList(&L);break;
      case '10':ListTraverse(&L);break;
      case 'Q':break;
      case 'q':break;

    }
    if (userInput == 'q'|| userInput == 'Q')
    break;
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章