|
发表于 2015-6-22 15:32:41
|
显示全部楼层
本帖最后由 seii 于 2015-6-22 15:36 编辑
- /*****************************/
- /** By Seii **/
- /*****************************/
- #ifndef _LIST_H_
- #define _LIST_H_
- #include <stdio.h>
- #include <stdlib.h>
- typedef enum{
- false, true
- }bool;
- typedef enum{
- ERROR, OK
- }status;
- typedef int ElemType;
- typedef int CursorType;
- #define MAXSIZE 10
- typedef struct _list{
- ElemType data;
- CursorType cur;
- }Component, SLList[MAXSIZE];
- #define _IsEmpty(SSLptr) ((SSLptr)[MAXSIZE-1].cur==0)
- #define _IsFull(SSLptr) ((SSLptr[0].cur==0))
- status InitSLL(SLList list);//初始化
- void PrintSLL(const SLList list);//打印
- status InsertBackSLL(SLList list, ElemType data);//尾部插入数据
- void MoveToLast(SLList list, CursorType *lastCur);//移动到最后一个元素
- status InsertFrontSLL(SLList list, ElemType data);//头部插入数据
- size_t GetRestSpace(const SLList list);//获取剩余的存储空间
- status MoveToPrevPos(const SLList list, int pos, CursorType *target);//移动到pos处的前一个元素
- status InsertByPos(SLList list, int pos, ElemType data);//根据位置插入数据
- status RemoveByPos(SLList list, int pos);//根据位置删除元素
- #endif //_LIST_H_
复制代码
- #include "list.h"
- status InitSLL(SLList list){//初始化
- if (list == NULL)return ERROR;
- for (int i = 0; i < MAXSIZE - 1; i++){
- list[i].cur = i + 1;
- list[i].data = 0;
- }
- list[MAXSIZE - 1].data = 0;
- list[MAXSIZE - 1].cur = 0;
- list[MAXSIZE - 2].cur = 0;
- return OK;
- }
- void PrintSLL(const SLList list){//打印
- if (list == NULL)
- puts("***********************该链表无效********************\n");
- else if (_IsEmpty(list))
- puts("***********************该链表为空********************\n");
- else{
- puts("***********************链表数据START********************");
- for (CursorType i = list[MAXSIZE - 1].cur; i != 0; i = list[i].cur){
- printf("%d ", list[i].data);
- }
- putchar('\n');
- puts("***********************链表数据E N D********************\n");
- }
- }
- status InsertBackSLL(SLList list, ElemType data){//尾部插入数据
- if (list == NULL)return ERROR;
- CursorType last;
- if (_IsEmpty(list))
- list[MAXSIZE - 1].cur = 1;
- MoveToLast(list, &last);
- list[last].cur = list[0].cur;
- list[0].cur = list[list[last].cur].cur;
- list[list[last].cur].data = data;
- list[list[last].cur].cur = 0;
- return OK;
- }
- void MoveToLast(SLList list, CursorType *lastCur){//移动到最后一个元素
- if (list == NULL || _IsEmpty(list) || lastCur == NULL)
- *lastCur = 0;
- else{
- *lastCur = list[MAXSIZE - 1].cur;
- while (list[*lastCur].cur != 0)*lastCur = list[*lastCur].cur;
- }
- }
- status InsertFrontSLL(SLList list, ElemType data){//头部插入数据
- if (list == NULL || _IsFull(list))return ERROR;
- CursorType newnode = list[0].cur;
- list[newnode].data = data;
- list[0].cur = list[newnode].cur;
- list[newnode].cur = list[MAXSIZE - 1].cur;
- list[MAXSIZE - 1].cur = newnode;
- return OK;
- }
- size_t GetRestSpace(const SLList list){//获取剩余的存储空间
- if (list == NULL)return -1;
- size_t count = 0;
- for (CursorType cur = list[0].cur; cur != 0; cur = list[cur].cur, count++);
- return count;
- }
- status MoveToPrevPos(const SLList list, int pos, CursorType *target){//移动到pos处的前一个元素
- if (list == NULL)return ERROR;
- *target = MAXSIZE - 1;
- pos--;
- while (pos && *target != 0){
- *target = list[*target].cur;
- pos--;
- }
- if (pos == 0){
- return OK;
- }
- else{
- *target = -1;
- //puts("您的插入超出了范围");
- return ERROR;
- }
- }
- status InsertByPos(SLList list, int pos, ElemType data){//根据位置插入数据
- if (list == NULL || _IsFull(list))return ERROR;
- CursorType target;
- if (MoveToPrevPos(list, pos, &target)){
- CursorType next = list[target].cur;
- list[target].cur = list[0].cur;
- list[0].cur = list[list[target].cur].cur;
- list[list[target].cur].data = data;
- list[list[target].cur].cur = next;
- return OK;
- }
- return ERROR;
- }
- status RemoveByPos(SLList list, int pos){//根据位置删除元素
- if (list == NULL || _IsEmpty(list) || pos < 1)return ERROR;
- CursorType target;
- if (MoveToPrevPos(list, pos, &target)){
- CursorType curTarget = list[target].cur;
- list[target].cur = list[curTarget].cur;
- list[curTarget].cur = list[0].cur;
- list[0].cur = target;
- return OK;
- }
- return ERROR;
- }
复制代码
- #include <stdio.h>
- #include <stdlib.h>
- #include "list.h"
- int main(){
- SLList list;
- InitSLL(list);
- for (int i = 0; i < 5; i++){
- InsertFrontSLL(list, i + 1);
- }
- PrintSLL(list);
- printf("rest=%d\n", GetRestSpace(list));
- RemoveByPos(list, 3);
- PrintSLL(list);
- printf("rest=%d\n", GetRestSpace(list));
- system("pause");
- return 0;
- }
复制代码 |
|