简单线性表

cpp] view plaincopy
01.#define INITSIZE 100        //线性表的初始大小     
02.#define INCREACEMENT 10     //定义线性表的分配容量    
03.    
04.struct List    
05.{    
06.    int * firstElement  ; //首元素的地址     
07.    int length ;          //现有元素个数    
08.    int allocate_size ;   //当前的最大容量    
09.};    
10.    
11./* 初始化L     */    
12.void init_List(List * L) ;    
13.    
14./*在L中的i位置插入元素element*/     
15.void insertInto_List(List* L, int i, int element) ;    
16.    
17./*删除线性表中位序为elementIndex的元素, 用delectElement 返回 */     
18.void delect_list(List * L, int elementIndex, int * delectElement) ;    
19.    
20./*返回节点在线性表中的位序    */     
21.int local_List(const List * L , int element) ;    
22.    
23./*输出线性表中的元素 */     
24.void showElement(const List * L) ;    
25.  
26./*释放 */  
27.void free_list(List * L) ;  
01.#include <stdio.h>    
02.#include <malloc.h>    
03.#include <stdlib.h>  
04.#include "list_head.h"    
05.    
06./* 初始化L     */    
07.void init_List(List * L)    
08.{    
09.    L->firstElement = (int *)malloc(INITSIZE*sizeof(int));     
10.    if(!L->firstElement)    
11.    {    
12.        fprintf(stderr,"初始化错误");    
13.        exit(EXIT_FAILURE);     
14.    }    
15.    L->length = 0;    
16.    L->allocate_size = INITSIZE;    
17.}     
18.    
19./*在L中的i位置插入元素element*/     
20.void insertInto_List(List* L, int i, int element)    
21.{    
22.    if(i > L->length+1 || i<1)    
23.    {    
24.        fprintf(stderr,"插入错误");    
25.        exit(EXIT_FAILURE);     
26.    }    
27.    if(L->length >= L->allocate_size)    
28.    {    
29.        int * newbase = (int *)realloc(L->firstElement, (L->allocate_size + INCREACEMENT)*sizeof(int));    
30.        if(!newbase)    
31.        {    
32.            fprintf(stderr,"线性表扩大错误");    
33.            exit(EXIT_FAILURE);    
34.        }    
35.        L->allocate_size += INCREACEMENT;    
36.    }    
37.    int * p  ;    
38.    for( p = (L->firstElement+i-1); p <= L->firstElement+L->length+1; p++ )    
39.    {    
40.        *(p + 1) = *p;    
41.    }    
42.        
43.    *(L->firstElement + i - 1) = element;     
44.    L->length += 1 ;     
45.}    
46.    
47./*删除线性表中位序为elementIndex的元素, 用delectElement 返回 */     
48.void delect_list(List * L, int elementIndex, int * delectElement)    
49.{       
50.    if(elementIndex < 1 || elementIndex > L->length)    
51.    {    
52.        fprintf(stderr,"元素位序超出范围.\n");    
53.        exit(EXIT_FAILURE);     
54.    }    
55.    //elementIndex - 1的元素赋值给 delectElement    
56.    * delectElement = *(L->firstElement + elementIndex - 1);     
57.    //释放掉elementIndex - 1这块内存    
58.    free(L->firstElement + elementIndex -1);     
59.    //elementIndex后面的元素位序依次减一       
60.    for(int * i = (L->firstElement + elementIndex); i < (L->firstElement + L->length); i++)    
61.    {    
62.        *(i-1) = *i;     
63.    }    
64.    //L的长度减一     
65.    --L->length;    
66.}    
67.    
68./*返回节点在线性表中的位序    */     
69.int local_List(const List * L , int element)    
70.{       
71.    //定义返回的位序  默认此节点不在线性表中     
72.    int element_Int = -1;    
73.    //循环线性表    
74.    for(int i=0;i < L->length; i++)    
75.    {    
76.        //取线性表的数据与element比较    
77.        if(*(L->firstElement+i) == element)    
78.        {    
79.            //若相等i赋值给element_int     
80.            element_Int = i;    
81.        }    
82.    }     
83.    return element_Int;     
84.}     
85.    
86./*输出线性表中的元素 */     
87.void showElement(const List * L)    
88.{    
89.    for(int i = 0; i<L->length; i++)    
90.    {    
91.        printf("firstElement[%d]=%d\n",i,*(L->firstElement + i));    
92.    }    
93.}   
94.  
95./*释放 */  
96.void free_list(List * L)  
97.{  
98.    if(L->firstElement != NULL){  
99.        free(L->firstElement);  
100.        L->firstElement = NULL;  
101.    }   
102.    if(L != NULL){  
103.        free(L);   
104.        L = NULL;     
105.    }   
106.}  
107.  
108.   
01.#include "list.h"  
02.#include <stdio.h>      
03.#include <stdlib.h>       
04.    
05.int main(void)    
06.{    
07.    List list;    
08.        
09.    init_List(&list);    
10.    int delectElement;     
11.    insertInto_List(&list, 1, 5);    
12.    insertInto_List(&list, 2, 6);    
13.    showElement(&list);    
14.    //int index = local_List(&list, 7);    
15.    delect_list(&list,2,&delectElement);     
16.    printf("删除的元素为:%d\n", delectElement);    
17.    showElement(&list);    
18.    free_list(&list);  
19.      
20.    return 0;    
21.}    

 

posted @ 2013-03-16 15:41  chapterlin  阅读(169)  评论(0)    收藏  举报