📄 list.c
字号:
#include "list.h"#include "../common.h"#include <string.h>void CreateList(List *list){ if(list) { list->head = list->current = NULL; list->count = 0; list->currentpos = 0; }else Error("Attempt to create a list not existed.");}void DropList(List *list){ if(list) { ListNode *p,*following; following = list->head; for(;following!=NULL;) { p = following; following = following->next; free(p); } } free(list);}void SetPosition(Position p,List *list){ if(p < 0 || p >= list->count) Error("Attempt to set a position not in the list."); if(p > list->currentpos) for(; list->currentpos!=p; (list->currentpos)++) list->current = list->current->next; else if(p < list->currentpos) for(;list->currentpos!=p;(list->currentpos)--) list->current = list->current->previous; //printf("SetPosition success.\n");}ListNode *MakeListNode(ListEntry x){ ListNode *p = (ListNode *) malloc (sizeof(ListNode)); ListEntry px = (ListEntry) malloc (sizeof(ListEntry) + sizeof(char) * (strlen(x->value) + 1)); if(p && px) { strcpy((char *)px.value,(char *)x.value); p->entry = px; p->next = NULL; p->previous = NULL; }else Error("No space for additional node can be obtained."); //printf("MakeListNode success.\n"); return p;}void InsertListNode(Position p,ListEntry x,List *list){ if(*x) { ListNode *newnode,*following; if(p < 0 || p > list->count) Error("Attempt to insert in a position not in the list."); else { newnode = MakeListNode(x); if(p == 0) { list->head = newnode; newnode->previous = NULL; if(list->count == 0) newnode->next == NULL; else { SetPosition(0,list); newnode->next = list->current; list->current->previous = newnode; } }else { SetPosition(p-1,list); following = list->current->next; newnode->next = following; newnode->previous = list->current; list->current->next = newnode; if(following) following->previous = newnode; } list->current = newnode; list->currentpos = p; list->count++; } } //printf("InsertListNode success.\n");}void DeleteListNode(Position p,ListEntry x,List *list){ if(p < 0 || p >= list->count) Error("Attempt to Delete in a position not in the list."); else SetPosition(p,list); if(strlen((char *)list->current->entry) >= strlen((char *) x)) Error("The ListEntry memory of parameter is not enough."); strcpy((char *)x,(char *)list->current->entry); ListNode *following; following = list->current; if(list->count == 1) { list->currentpos = 0; list->current = list->head = NULL; }else { if(following->previous) following->previous->next = following->next; else list->head = following->next; if(following->next) following->next->previous = following->previous; list->currentpos = 0; list->current = list->head; } list->count--; free(following); //printf("DeleteListNode success.\n");}void RetrieveListNode(Position p,ListEntry x,List *list){ if(p < 0 || p >= list->count) Error("Attempt to insert in a position not in the list."); else SetPosition(p,list); if(strlen((char *)list->current->entry) >= strlen((char *) x)) Error("The ListEntry memory of parameter is not enough."); strcpy((char *)x,(char *)list->current->entry); //printf("RetrieveListNode success.\n");}void ReplaceList(Position p,ListEntry x,List *list){ if(p < 0 || p >= list->count) Error("Attempt to insert in a position not in the list."); else SetPosition(p,list); ListNode *following = MakeListNode(x); following->next = list->current->next; following->previous = list->current->previous; if(following->previous) following->previous->next = following; if(following->next) following->next->previous = following; free(list->current); list->current = following;}void ShowListEntry(List *list){ int i; ListNode *following; following = list->head; for(i=0;i<list->count;i++) { printf("list[%d]= %s \n",i,following->entry); following = following->next; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -