静态链表2.0

本文详细介绍了静态链表的概念、创建、初始化、打印、插入、删除、查找等操作。通过代码演示了如何在C语言中实现静态链表,并提供了测试用例展示其功能。
摘要由CSDN通过智能技术生成

一、静态链表的意义

静态链表,就是用数组描述的链表。静态链表在初始化时要确定数组的长度,所以需要预先分配空间,其空间大小一般是静态的,故名静态链表

二、创建静态链表

代码演示:

typedef struct StaticLinkedNode{
    char data;
    int next;      //游标
}*NodePtr; 

typedef struct StaticLinkedList{
    NodePtr nodes;!(https://img-blog.csdnimg.cn/8fc4adde65c245c28eda9bbd8f1dbfd7.png)

    int *used;
}*ListPtr;
1
2
3
4
5
6
7
8
9
10
*1、next代表游标
2、用used指针判断空间是否已经分配 *

三、初始化静态链表


 

代码演示

ListPtr initLinkedList(){
    ListPtr tempPtr = (ListPtr)malloc(sizeof (struct StaticLinkedList));
    tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*DEFAULT_SIZE);
    tempPtr->used=(int*)malloc(sizeof(int)*DEFAULT_SIZE);
    tempPtr->nodes[0].data='\0' ;
    tempPtr->nodes[0].next=-1;
    tempPtr->used[0]=1;
    for(int i=0;i<DEFAULT_SIZE;i++){
        tempPtr->used[i]=0;
    }
    return tempPtr;
}

注:这里要注意分配的空间需要乘最大结点数

四、打印

代码演示:

void printList(ListPtr paraListPtr){
    int p=0;
    while(p!=-1){
        printf("%c",paraListPtr->nodes[p].data);
        p=paraListPtr->nodes[p].next;
    }
    printf("\r\n");
}

注:需要理解 p=paraListPtr->nodes[p].next ,非常重要!!!

五、插入

void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){
    int p,q,i;
    p=0;
    for(i=0 ;i<paraPosition;i++){
        p=paraListPtr->nodes[p].next;
    if(p==-1){
        printf("The position %d is beyond the scope of the list.\r\n",paraPosition);
        return;
    }
}
    for(i=1;i<DEFAULT_SIZE;i++){
        if(paraListPtr->used[i]==0){
            printf("Space at %d allocated.\r\n", i);
            paraListPtr->used[i]=1;
            q=i; 
            break;
        }
    }
    if(i==DEFAULT_SIZE){ 
        printf("No space\r\n"); 
        return;
    }
    paraListPtr->nodes[q].data=paraChar;
    paraListPtr->nodes[q].next=paraListPtr->nodes[p].next;
    paraListPtr->nodes[p].next=q;
}


六、删除

代码演示:

void deleteElement(ListPtr paraListPtr,char paraChar){
    int p , q;
    p=0;
    while((paraListPtr->nodes[p].next!=-1)   &&  (paraListPtr->nodes[paraListPtr->nodes[p].next].data!=paraChar)){
        p=paraListPtr->nodes[p].next;
    }
    if(paraListPtr->nodes[p].next==-1){
        printf("can not delete %c\r\n",paraChar);
        return;
    }
    q=paraListPtr->nodes[p].next;
    paraListPtr->nodes[p].next=paraListPtr->nodes[paraListPtr->nodes[p].next].next;
    paraListPtr->used[q]=0;
}

 

七、查找第x位元素

代码演示:

void getElement(ListPtr paraListPtr,int paraPosition){
    int p=0;
    char q;
    for(int i=0;i<paraPosition;i++){
        p=paraListPtr->nodes[p].next;
    }
    q=paraListPtr->nodes[p].data;
    printf("第%d位是%c",paraPosition,q);
}

八、查找元素x在第几位

代码演示:

void locateElement(ListPtr paraListPtr,char paraChar){
    int p=0,num = 0;
    while(paraListPtr->nodes[p].data!=paraChar){
        num++;
        p=paraListPtr->nodes[p].next;
    }
    printf("%c在第%d位",paraChar,num);
}

 

九、测试

void Test(){
    ListPtr List=initLinkedList();
    insertElement(List,'H',0);
    insertElement(List,'E',1);
    insertElement(List,'L',2);
    insertElement(List,'a',3);
    insertElement(List,'O',4);
    printList(List);
    deleteElement(List,'E');
    printList(List);
    getElement(List,2);
    printf("\r\n");
    locateElement(List,'L');
}

十、此时链表

 

十一、总代码

#include<stdio.h>
#include<malloc.h>

#define DEFAULT_SIZE 5

typedef struct StaticLinkedNode{
    char data;
    int next;      //游标
}*NodePtr; 

typedef struct StaticLinkedList{
    NodePtr nodes;
    int *used;
}*ListPtr;

ListPtr initLinkedList(){
    ListPtr tempPtr = (ListPtr)malloc(sizeof (struct StaticLinkedList));
    tempPtr->nodes=(NodePtr)malloc(sizeof(struct StaticLinkedNode)*DEFAULT_SIZE);
    tempPtr->used=(int*)malloc(sizeof(int)*DEFAULT_SIZE);
    tempPtr->nodes[0].data='\0' ;
    tempPtr->nodes[0].next=-1;
    tempPtr->used[0]=1;
    for(int i=0;i<DEFAULT_SIZE;i++){
        tempPtr->used[i]=0;
    }
    return tempPtr;
}

void printList(ListPtr paraListPtr){
    int p=0;
    while(p!=-1){
        printf("%c",paraListPtr->nodes[p].data);
        p=paraListPtr->nodes[p].next;
    }
    printf("\r\n");
}

void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){
    int p,q,i;
    p=0;
    for(i=0 ;i<paraPosition;i++){
        p=paraListPtr->nodes[p].next;
    if(p==-1){
        printf("The position %d is beyond the scope of the list.\r\n",paraPosition);
        return;
    }
}
    for(i=1;i<DEFAULT_SIZE;i++){
        if(paraListPtr->used[i]==0){
            printf("Space at %d allocated.\r\n", i);
            paraListPtr->used[i]=1;
            q=i; 
            break;
        }
    }
    if(i==DEFAULT_SIZE){ 
        printf("No space\r\n"); 
        return;
    }
    paraListPtr->nodes[q].data=paraChar;
    paraListPtr->nodes[q].next=paraListPtr->nodes[p].next;
    paraListPtr->nodes[p].next=q;
}

void deleteElement(ListPtr paraListPtr,char paraChar){
    int p , q;
    p=0;
    while((paraListPtr->nodes[p].next!=-1)   &&  (paraListPtr->nodes[paraListPtr->nodes[p].next].data!=paraChar)){
        p=paraListPtr->nodes[p].next;
    }
    if(paraListPtr->nodes[p].next==-1){
        printf("can not delete %c\r\n",paraChar);
        return;
    }
    q=paraListPtr->nodes[p].next;
    paraListPtr->nodes[p].next=paraListPtr->nodes[paraListPtr->nodes[p].next].next;
    paraListPtr->used[q]=0;
}

void getElement(ListPtr paraListPtr,int paraPosition){
    int p=0;
    char q;
    for(int i=0;i<paraPosition;i++){
        p=paraListPtr->nodes[p].next;
    }
    q=paraListPtr->nodes[p].data;
    printf("第%d位是%c",paraPosition,q);
}

void locateElement(ListPtr paraListPtr,char paraChar){
    int p=0,num = 0;
    while(paraListPtr->nodes[p].data!=paraChar){
        num++;
        p=paraListPtr->nodes[p].next;
    }
    printf("%c在第%d位",paraChar,num);
}

void Test(){
    ListPtr List=initLinkedList();
    insertElement(List,'H',0);
    insertElement(List,'E',1);
    insertElement(List,'L',2);
    insertElement(List,'a',3);
    insertElement(List,'O',4);
    printList(List);
    deleteElement(List,'E');
    printList(List);
    getElement(List,2);
    printf("\r\n");
    locateElement(List,'L');
}

int main(){
    Test();
}

十二、测试结果

Space at 1 allocated.
Space at 2 allocated.
Space at 3 allocated.
Space at 4 allocated.
No space
 HELa
 HLa
第2位是L
L在第2位
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值