数据结构之静态链表

    科技2025-06-11  43

    静态链表

    基本概念

    /*删除在L中的第i个数据元素*/ Status ListDelete(StaticLinkList L,int i) { int j,k; if(i<1 || i > ListLength(L)) { return 0; } k=MAX_SIZE-1; for(j=1;j<i-1;j++) { k=L[K].cur; } j=L[k].cur; L[k].cur=L[j].cur; Free_SLL(L,j); return 1; } /*将下标为k的空闲结点回收到备用链表*/ void Free_SLL(StaticLinkList space ,int k) { space[k].cur=space[0].cur; space.cur=k; } /*返回L中数据元素个数*/ int ListLength(StaticLinkList L) { int j=0; int i= L[MAXSIZE-1].cur; while(i) { i=L[i].cur; } return i; }

    #include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct node//说明一个结构体 { int data; struct node *next; }LinkNode; LinkNode *create();//创建一个非循环单链表,并将该链表的头结点地址赋给head void add(LinkNode *head, int x);//尾部插入 void print2(LinkNode *head);//遍历,非递归 int getMidNode(LinkNode *head);//用快慢指针快速查找中间结点的值并显示 int main() { time_t ts; srand((unsigned int)time(&ts)); int i = 0; int num = 0;//插入值 LinkNode *head = NULL;//头指针 head = create();//创建一个非循环单链表,并将该链表的头结点地址赋给head for (i = 0; i < 20; i++) { num = rand() % 100 + 1; add(head, num);//尾部插入 } print2(head);//遍历,非递归 printf("中间结点的值是%d\n", getMidNode(head));//用快慢指针快速查找中间结点的值并显示 return 0; } LinkNode *create()//创建一个非循环单链表,并将该链表的头结点地址赋给head { LinkNode *head = (LinkNode *)malloc(sizeof(LinkNode)); if (!head) { printf("创建链表失败,内存分配失败\n"); return NULL; } else { head->next = NULL; return head; } } void add(LinkNode *head, int x)//尾部插入 { LinkNode *n = (LinkNode *)malloc(sizeof(LinkNode));//第1步,创建指针new,用于存储新数据 if (!n) { printf("尾部插入失败,内存分配失败\n"); return; } else { n->data =x; n->next = NULL; } LinkNode *p = head;//第2步,创建指针p,用于移动 if (!p) { printf("头指针为空\n"); return; } else { while (p->next)//遍历 { p = p->next; } p->next = n; //p=new; //第3步,指针p指向指针new } } void print2(LinkNode *head)//遍历,非递归 { LinkNode *p = head; while (p->next) { printf("%d ", p->next->data); p = p->next; } printf("\n"); } int getMidNode(LinkNode *head)//用快慢指针快速查找中间结点的值并显示 { LinkNode *fast = head;//快指针 LinkNode *mid = head;//一般指针 while (fast->next)//当fast下一个的结点不为空 { if (fast->next->next)//如果fast下一个的下一个的结点不为空 { fast = fast->next->next;//fast移动两个结点 } else//如果fast下一个的下一个的结点为空 { fast = fast->next;//fast移动一个结点 } mid = mid->next;//mid移动一个结点 } return mid->data;//返回 }

    #include <stdio.h> #define maxSize 7 typedef struct { char data; int cur; }component; //将结构体数组中所有分量链接到备用链表中 void reserveArr(component *array); //初始化静态链表 int initArr(component *array); //向链表中插入数据,body表示链表的头结点在数组中的位置,add表示插入元素的位置,a表示要插入的数据 void insertArr(component * array,int body,int add,char a); //删除链表中含有字符a的结点 void deletArr(component * array,int body,char a); //查找存储有字符elem的结点在数组的位置 int selectElem(component * array,int body,char elem); //将链表中的字符oldElem改为newElem void amendElem(component * array,int body,char oldElem,char newElem); //输出函数 void displayArr(component * array,int body); //从备用链表中摘除空闲节点的实现函数 int mallocArr(component * array); //将摘除下来的节点链接到备用链表上 void freeArr(component * array,int k); int main() { component array[maxSize]; int body=initArr(array); printf("静态链表为:\n"); displayArr(array, body); printf("在第3的位置上插入结点‘e’:\n"); insertArr(array, body, 3,'e'); displayArr(array, body); printf("删除数据域为‘a’的结点:\n"); deletArr(array, body, 'a'); displayArr(array, body); printf("查找数据域为‘e’的结点的位置:\n"); int selectAdd=selectElem(array,body ,'e'); printf("%d\n",selectAdd); printf("将结点数据域为‘e’改为‘h’:\n"); amendElem(array, body, 'e', 'h'); displayArr(array, body); return 0; } //创建备用链表 void reserveArr(component *array){ for (int i=0; i<maxSize; i++) { array[i].cur=i+1;//将每个数组分量链接到一起 array[i].data=' '; } array[maxSize-1].cur=0;//链表最后一个结点的游标值为0 } //初始化静态链表 int initArr(component *array){ reserveArr(array); int body=mallocArr(array); //声明一个变量,把它当指针使,指向链表的最后的一个结点,因为链表为空,所以和头结点重合 int tempBody=body; for (int i=1; i<5; i++) { int j=mallocArr(array);//从备用链表中拿出空闲的分量 array[tempBody].cur=j;//将申请的空线分量链接在链表的最后一个结点后面 array[j].data='a'+i-1;//给新申请的分量的数据域初始化 tempBody=j;//将指向链表最后一个结点的指针后移 } array[tempBody].cur=0;//新的链表最后一个结点的指针设置为0 return body; } void insertArr(component * array,int body,int add,char a){ int tempBody=body; for (int i=1; i<add; i++) { tempBody=array[tempBody].cur; } int insert=mallocArr(array); array[insert].cur=array[tempBody].cur; array[insert].data=a; array[tempBody].cur=insert; } void deletArr(component * array,int body,char a){ int tempBody=body; //找到被删除结点的位置 while (array[tempBody].data!=a) { tempBody=array[tempBody].cur; //当tempBody为0时,表示链表遍历结束,说明链表中没有存储该数据的结点 if (tempBody==0) { printf("链表中没有此数据"); return; } } //运行到此,证明有该结点 int del=tempBody; tempBody=body; //找到该结点的上一个结点,做删除操作 while (array[tempBody].cur!=del) { tempBody=array[tempBody].cur; } //将被删除结点的游标直接给被删除结点的上一个结点 array[tempBody].cur=array[del].cur; freeArr(array, del); } int selectElem(component * array,int body,char elem){ int tempBody=body; //当游标值为0时,表示链表结束 while (array[tempBody].cur!=0) { if (array[tempBody].data==elem) { return tempBody; } tempBody=array[tempBody].cur; } return -1;//返回-1,表示在链表中没有找到该元素 } void amendElem(component * array,int body,char oldElem,char newElem){ int add=selectElem(array, body, oldElem); if (add==-1) { printf("无更改元素"); return; } array[add].data=newElem; } void displayArr(component * array,int body){ int tempBody=body;//tempBody准备做遍历使用 while (array[tempBody].cur) { printf("%c,%d ",array[tempBody].data,array[tempBody].cur); tempBody=array[tempBody].cur; } printf("%c,%d\n",array[tempBody].data,array[tempBody].cur); } //提取分配空间 int mallocArr(component * array){ //若备用链表非空,则返回分配的结点下标,否则返回0(当分配最后一个结点时,该结点的游标值为0) int i=array[0].cur; if (array[0].cur) { array[0].cur=array[i].cur; } return i; } //将摘除下来的节点链接到备用链表上 void freeArr(component * array,int k){ array[k].cur=array[0].cur; array[0].cur=k; }
    Processed: 0.017, SQL: 8