posts - 63, comments - 37, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 ::  :: 管理

//ListDelete_Sq.cpp

//Delete the NO.i Element of Sq_List and get the value

 

#include <stdlib.h>

#include <iostream.h>

#include <conio.h>

#include <stdio.h>

 

 

#define ElemType int

#define LIST_INIT_SIZE 100

#define LISTINCREMENT 10

typedef struct

{   int *elem;

    int length;

    int listsize;

}SqList;

 

int InitList_Sq(SqList &L)                                    //InitList_Sq() function

{   //Inititial a Sq_List

    L.elem=(int *)malloc(LIST_INIT_SIZE *sizeof(int));

    if (!L.elem) return(0);

    L.length=0;

    L.listsize=LIST_INIT_SIZE;

    return(1);

}//end of InitList_Sq() function

 

 

int ListDelete_Sq(SqList &L, int i){

    ElemType *p,*q;

    if( i<1 || i>L.length){

       printf("The Entry you want to Delete is not exsit\n");

       return 0;

    }

    //e=L.elem[i-1];          // 提取要删除项的内容

    q=&L.elem[L.length-1];   // 最后一项的地址

    for(p=&L.elem[i];p<=q;++p)  // 从要删除项的后一项开始每项都往前放一个位置

       *(p-1)=*p;

    --L.length;

    return 1;

}

 

// 输入的是 Sqlist 中的一个表项 , 所以应该先查找再删除 , 如果找到就删除 , 没有找到返回

int LocateElem_Sq(SqList L, ElemType e){

    int i;

    for(i=0; i<=L.length-1;i++){

       if(L.elem[i] == e)

           return i+1; //e 的位置为第 i+1 的位置

    }

    return 0; // 没有找到

}

 

void ListDelete_Sq_bye(SqList &L, ElemType e){

    int i;

    i=LocateElem_Sq(L,e);

    if(i != 0){

       printf("\n%d is one of the SqList Entry\n",e);

       ListDelete_Sq(L,i);

    }

    else

       printf("Can't found the %d\n",e);

 

}

 

 

 

 

void main()                                             //main() function

{

   SqList L;

  

   int i,j;

   //L.elem=array;

   //L.length=LIST_INIT_LENGTH;

   cout<<"ListDelete_Sq.cpp"<<endl<<"================="<<endl<<endl;

  

   InitList_Sq(L);

   cout<<"Please input the Length of Demo SqList L: <eg. 5> ";

   cin>>L.length;

   cout<<"Please input the data of Demo SqList L: <eg. {34,54,30,2,40,...}> "<<endl;

   for(j=0;j<L.length;j++)

     cin>>L.elem[j];

     

   cout<<endl;

   cout<<"Success to create a SqList:"<<endl;

   cout<<"Please input the  element of Sq_List to delete:  <eg. 3> ";

   cin>>i;

 

   ListDelete_Sq_bye(L,i);

   printf("---the data you delete is----------%d \n",i);

   cout<<"The SqList After Delete is:  ";

   for(j=0;j<L.length;j++)

      cout<<L.elem[j]<<"  ";

   cout<<endl<<"...OK...!"<<endl;

   getch();

}//end of main() function

只有注册用户登录后才能发表评论。