码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构3_静态链表

时间:2020-07-15 01:21:11      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:16px   int   根据   存在   style   delete   add   sele   select   

#include<stdio.h>
#define maxsize 6

typedef int ElemType;
typedef struct
{
    ElemType data;
    int cur;
} component;

//1.创建备用链表
void reserverArr(component * array)
{
    for(int i=0; i<maxsize; ++i)
    {
        array[i].data = -1;
        array[i].cur = i+1;
    }
    
    array[maxsize-1].cur = 0;
}

//2.提取分配空间 
int mallocArr(component * array)
{
    int i = array[0].cur;
    
    if(array[0].cur)
    {
        array[0].cur = array[i].cur;
    }
    
    return i;
}

//3.初始化 
int initArr(component * array)
{
    reserverArr(array);
    
    int body = mallocArr(array);
    
    int temp = body;
    
    for(int i=0; i<3; ++i)
    {
        int j = mallocArr(array);
        
        array[j].data = i;
        array[temp].cur = j;
        
        temp = j;
    }
    
    array[temp].cur = 0;
    
    return body;    
} 

//4.遍历链表
void traverseArr(component * array, int body)
{
    int temp = body;
    
    while(array[temp].cur)
    {
        printf("%d    %d\n",array[temp].data,array[temp].cur);
        temp = array[temp].cur;
    }
    printf("%d    %d\n",array[temp].data,array[temp].cur);
    printf("\n\n");
}
//5.链表长度
int length_Arr(component * array, int body)
{
    int temp = body;
    
    int length = 0;
    
    temp = array[temp].cur;
    
    while(array[temp].cur)
    {
        length++;
        
        temp = array[temp].cur;
    }
    
    return ++length;
}
 
//6.插入元素
int insertElem(component * array, int body, int add, ElemType elem)
{
    int temp = body;
    
    if(add<1 || add>length_Arr(array, body)+1)
    {
        printf("插入位置不存在!\n");
        return body;
    }
    
    int j = mallocArr(array);
    
    array[j].data = elem;
    
    for(int i=1; i<add; ++i)
    {
        temp = array[temp].cur;
    }
    
    array[j].cur = array[temp].cur;
    
    array[temp].cur = j;
    
    return body;
    
}

//7.查找元素
int selectElem(component * array, int  body, ElemType elem)
{
    int temp = body;
    
    temp = array[temp].cur;
    
    int add = 1;
    
    while(array[temp].cur)
    {
        if(array[temp].data == elem)
            return add;
        temp = array[temp].cur;
        ++add;
    }
    
    if(array[temp].data == elem)
        return add;
        
    return 0; 
}

//8.释放元素空间 
void freeElem(component * array, int k) 
{
    array[k].cur = array[0].cur;
    array[0].cur = k;
}

//9.删除元素
int deleteElem(component * array, int body, int add)
{
    int temp = body;
    
    if(add<1 || add>length_Arr(array, body))
    {
        printf("删除的元素不存在!\n");
        return body;
    }
    
    for(int i=1; i<add; ++i)
    {
        temp = array[temp].cur;
    }
    
    int del;
    
    del = array[temp].cur;
    
    array[temp].cur = array[del].cur;
    
    freeElem(array, del);
    
    return body;
    
}

//修改元素
void modifyElem(component * array, int body, int add, ElemType elem)
{
    int temp = body;
    
    if(add<1 || add>length_Arr(array, body))
    {
        printf("该元素不存在!\n");
    }
    
    for(int i=1; i<=add; ++i)
    {
        temp = array[temp].cur;
    }
    
    array[temp].data = elem;
    
}

 
int main(void)
{
    component array[maxsize];
    
    int body = initArr(array);
    
    traverseArr(array, body);
    
//    printf("%d\n",length_Arr(array, body));
    
//    body = insertElem(array, body,5, 23);
    
//    printf("%d\n\n",selectElem(array, body, 4));
    
//    body = deleteElem(array, body,selectElem(array, body, 2));
    
    modifyElem(array, body, selectElem(array, body, 1), 23);
    
    traverseArr(array, body); 
     
//    printf("%d    %d\n",array[array[0].cur].data,array[array[0].cur].cur);
     
    return 0;
}

/*
    小结:在静态链表中,删除元素和修改元素根据位置来改变最好 
*/

 

数据结构3_静态链表

标签:16px   int   根据   存在   style   delete   add   sele   select   

原文地址:https://www.cnblogs.com/hanfei123/p/13303131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!