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

线性表的删除(一个元素)

Posted on 2006-06-19 20:14 Enjoy Life 阅读(726) 评论(0)  编辑 收藏 引用 所属分类: DS study


 

//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 &e){

    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;

}

 

-------------------------本函数的重点----------------------- 
void main()                                             //main() function

{

   SqList L;

   int e;

   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 NO.i element of Sq_List to delete:  <eg. 3> ";

   cin>>i;

   ListDelete_Sq(L,i,e);

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

   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

 

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