3.顺序表的删除运算

 1 
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <malloc.h>
 5 
 6 #define OK   1
 7 #define ERROR  0
 8 #define TRUE 1
 9 #define FALSE 0
10 
11 #define ElemType int
12 #define    MAXSIZE  100      /*此处的宏定义常量表示线性表可能达到的最大长度*/
13 typedef  struct
14 
15     ElemType  elem[MAXSIZE];   /*线性表占用的数组空间*/
16     int  last;        /*记录线性表中最后一个元素在数组elem[ ]中的位置(下标值),空表置为-1*/
17 }SeqList;
18 
19 int  DelList(SeqList *L,int i,ElemType *e)   /*在顺序表L中删除第i个数据元素,并用指针参数e返回其值。i的合法取值为1≤i≤L.last+1 */    
20 
21     int k;
22     if((i<1)||(i>L->last+1))   
23     { 
24         printf("delect a error place!");
25         return(ERROR);
26     }
27     *= L->elem[i-1];                 /* 将删除的元素存放到e所指向的变量中*/
28     for(k=i; i<=L->last; k++)
29         L->elem[k-1= L->elem[k]; /*将后面的元素依次前移*/
30     L->last--;
31     return(OK);
32 }
33 
34 void main()
35 {    
36     SeqList *l;
37     int p,r;
38     int *q;
39     int i;
40     l = (SeqList*)malloc(sizeof(SeqList));
41     q = (int*)malloc(sizeof(int));
42      printf("please enter the length of stack:");
43 
44     scanf("%d",&r);
45     l->last = r-1;
46     printf("please insert the menbers of the stack:\n");
47 
48     for(i=0; i<=l->last; i++)
49     {
50         scanf("%d",&l->elem[i]);
51     }
52     printf("please input the place of number which you want to delect :\n");
53 
54     scanf("%d",&p);
55     DelList(l,p,q);
56     printf("the stack was delected is :%d",*q);
57         printf("删除的元素值为:%d\n",*q);
58 }
59 

 

posted @ 2009-07-15 00:22  清山博客  阅读(1007)  评论(0编辑  收藏  举报