因為數據結構考試的大題是這兩個,所以抽一點時間實現了一下:

運行的環境是這樣的
//創建一個節點struct Node{int a; //數據域struct Node *next; //指針域,指向數據的節點};//全局定義頭尾指針方便調用struct Node *head = NULL;struct Node *end = NULL;//創建一個鏈表,初始化的工作void AddListTill(int a){struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); //進行強制類型轉換// 創建一個節點temp->a = a;temp->next = NULL;// 連接分為兩種情況,一種是沒有節點,一種是有節點,將東西加到節點上面if (NULL == head){head = temp;}else{end->next = temp;// 尾節點應該指向最后一個}end = temp; //尾節點應該始終指向最后一個節點}//函數的功能是尾添加的方式在尾節點添加的方式增加一個節點,輸入的參數就是這個節點的數據。首先創建一個節點,并且申請一個節點的內存//,之后對傳入節點的數據進行賦值,尾添加的節點應該是指向NULL。此時就是也該判斷了,要考慮節點的存在問題//遍歷鏈表----查的操作void ScanList(){struct Node *temp = head; //定義一個臨時變量來指向頭while (temp != NULL){printf("%d\n", temp->a);temp = temp->next; //temp指向限一個地址,實現了++的操作}// 函數的作用是遍歷這個鏈表,首先定義一個用于遍歷的指針變量(是臨時的),用while循環遍歷輸出}struct Node *FindNode(int a){struct Node *temp = head;while (temp != NULL){if (a == temp->a){return (temp);}temp = temp->next;}// 沒找到return (NULL);}// 找到就返回節點,找不到就返回NULL//鏈表清空----不就是全部刪除void FreeList(){// 一個一個的NULLstruct Node *temp = head; //定義一個臨時變量來指向頭while (temp != NULL){struct Node *pt = temp;temp = temp->next; //temp指向下一個地址free(pt); //釋放當前}// 頭尾清空,不然下次的頭會出現別的情況head = NULL;end = NULL;}//使用遍歷的做法來逐個的釋放對應的節點,,在最后應該將頭尾節點變NULL,否則下次的鏈表會接著這次的鏈表的頭尾//指定的位置插入節點----在指定位置增加void AddListRand(int index, int a){if (NULL == head){printf("鏈表沒有節點\n");return;}struct Node *pt = FindNode(index);if (NULL == pt){printf("沒有指定的節點\n");return;}// 有此節點。創建臨時的節點,申請對應可以使用的內粗空間struct Node *temp = (struct Node *)malloc(sizeof(struct Node));// 節點成員間進行賦值temp->a = a;temp->next = NULL;// 連接到鏈表上1.找到節點在尾部,2.找到節點在中間if (pt == end){// 尾巴的下一個加上我們的要指向的下一個要插入的節點end->next = temp;// 新的尾巴end->next = temp;}else{// 先連后面(先將要插入的接待你指針指向原來找到節點的下一個temp->next = pt->next;// 后連前面pt->next = temp;}}// 難點在于找要插入的節點位置,在這樣的基礎上去判斷要插入的指針節點要怎么辦。// 尾部刪除----刪除的操作void DeleteListTail(){if (NULL == end){printf("鏈表為空,無需刪除\n");return;}// 鏈表不為空// 鏈表里面有一個節點if (head == end){free(head);head = NULL;end = NULL;}else{// 找到尾巴的一個節點struct Node *temp = head;while (temp->next != end){temp = temp->next;}// 找到了,刪除尾巴// 釋放尾巴free(end);// 尾巴的遷移end = temp;// 尾巴指針為NULLend->next = NULL;}}// 尾刪除的過程和前面的做法一樣,都是先去判斷相關的系欸但的問題。只有一個節點的時候,直接置尾NULL// 不為空的時候,要使用遍歷但是是從后去遍歷的,倒數第二個先找到,然后將最后的一個節點的內存釋放了// 再將第二個節點設置尾end然后將它的指針指向null// 刪除頭的操作void DeleteListHead(){// 記住舊頭struct Node *temp = head;// 鏈表監測if (NULL == head){printf("鏈表為空\n");return;}head = head->next; //頭的第二個節點變成新的頭free(temp);}// 先定義一個臨時變量指向舊的頭,將頭的第二個記為新的頭指針head,然后將舊的頭釋放// 刪除一個指定的節點void DeleteListRand(int a){// 鏈表判斷,是不是沒有什么東西if (NULL == head){printf("鏈表沒有東西\n");return;}// 鏈表有東西,找這個節點struct Node *temp = FindNode(a);if (NULL == temp){printf("查無此節點\n");return;}// 找到了,且只有一個節點if (head == end){free(head);head = NULL;end = NULL;}else if (head->next == end){if (end == temp){DeleteListTail();}else if (temp == head){DeleteListHead();}else// 多個節點{//看是刪除頭還是尾if (end == temp){DeleteListTail();}else if (temp == head){DeleteListHead();}else //刪除中間某一個節點{// 找要刪除temp的前一個,遍歷struct Node *pt = head;while (pt->next != temp){pt->next = temp->next;free(temp);}}}}}int main(){// printf("Hello, World!\n");struct Node *pFind;// 創建5個節點for (int i = 0; i < 6; i++)AddListTill(i);DeleteListRand(4);ScanList();FreeList();// return 0;}
鏈表
//順序表的實現typedef struct Table{int *head;//聲明一個名叫head的長度不確定的數組,動態數組int length;//記錄當前順序表的長度int size;//記錄順序表分配的存儲容量}table;//此處為初始化table initTable(){table t;t.head = (int*)malloc(Size*sizeof(int));if(!t.head){printf("初始化失敗!");exit(0);}t.length=0;t.size = Size;return (t);}table addTable(table t,int elem,int add){if(add>t.length+1||add<1){printf("插入的位置有問題\n");return (t);}if(t.length>=t.size){t.head=(int*)realloc(t.head,(t.size+1)*sizeof(int));if(!t.head){printf("存儲分配失敗\n");}t.size+=1;}for(int i=t.length-1;i>=add-1;i--){t.head[i+1]=t.head[i];}t.head[add-1]=elem;t.length++;return (t);}table delTable(table t,int add){if(add>t.length||add<1){printf("被刪除元素的位置有誤\n");return (t);}for(int i=add;i<t.length;i++){t.head[i-1]=t.head[i];}t.length--;return (t);}table selectTable(table t,int elem){for(int i=0;i<t.length;i++){if(t.head[i]==elem){return (i+1);}}return (-1)}table amendTable(table t,int )//輸出順序表中元素的函數void displayTable(table t){for(int i=0;i<t.length;i++){printf("%d",t.head[i]);}printf("\n");}//tableint main() {table t=initTable();// 向順序表里面添加元素for(int i=1;i<=Size;i++){t.head[i-1]=i;t.length++;}printf("順序表中的元素分別是:\n");return (0);}
順序表的實現
祝大家考試順利呀~