//list.h
#ifndef _List_H#define _List_H#include <stdio.h>#include <stdlib.h>#define NotFound NULL;typedef struct List {int value;struct Node * next;}Node;typedef struct List *pNode;typedef pNode pList;typedef pNode Position;pList creatList(int *array, int len);Position findNode(pList head, int x);//pList lastInsertList(pList head, int insertvalue);pList InsertList(pList head, int val, int insertvalue);pList deleteNode(pList head, int delValue);pList reversalList(pList head);pList freeList(pList head);void printList(pList head);#endif /* _List_H */
//list.c
#include "singleList.h"pList creatList(int *array, int len){pList head,ptr,tmp;int i;head = (pList)malloc(sizeof(Node));if(!head){printf("分配記憶體失敗!");return NULL;}head->next = NULL;head->value = array[0];tmp = head;for(i = 1; i < len; i++){ptr = (pList)malloc(sizeof(Node));if (!ptr){printf("分配記憶體失敗!");return NULL;}ptr->value = array[i];ptr->next = NULL;tmp->next = ptr;tmp = tmp->next;}ptr->next = head;//單迴圈增加return head;}Position findNode(pList head, int x){pList ptr = head;while(ptr){if(ptr->value == x)return ptr;ptr = ptr->next;if(ptr == head)//單迴圈增加break;}return NotFound;}//在值為val的節點前插入pList InsertList(pList head, int val, int insertvalue){pList temp = head;pList ptr = head;pList tmp;while(ptr){if (ptr->value == val){while(temp->next != ptr)temp = temp->next;tmp = (pList)malloc(sizeof(Node));tmp->next = ptr;tmp->value = insertvalue;temp->next = tmp;return head;}ptr = ptr->next;if (ptr == head){printf("no such value can be insert!\n");break;}}return NotFound;}pList deleteNode(pList head, int delValue){pList ptr,temp;ptr = temp = head;while(ptr){if (ptr->value == delValue)//是“==”注意{while(temp->next != ptr)temp = temp->next;//找到ptr的上一個節點temptemp->next = ptr->next;free(ptr);if (head == ptr)//說明刪除的是前端節點{return temp->next;}return head;//當刪除前端節點時就不行}ptr = ptr->next;if (ptr == head)//說明迴圈了一周{printf("the delValue had not found!\n");break;}}return NotFound;}pList reversalList(pList head){pList ptr,temp;ptr = head;temp = head->next;while(temp->next){pList p = temp;temp = temp->next;p->next = ptr;ptr = p;if(temp->next == head)//單迴圈增加break;}temp->next = ptr;head->next = temp;//NULL改成tempreturn temp;}pList freeList(pList head){pList ptr;pList p = head;//單迴圈新增while(head){ptr = head;head = head->next;free(ptr);if(head == p)//單迴圈新增{head = NULL;break;}}return head;//注意記憶體配置問題}void printList(pList head){pList ptr = head;if(!ptr){printf("the list is empty!\n");return;}printf("the list is : ");while(ptr){printf("%d ",ptr->value);ptr = ptr->next;if(ptr == head)//單迴圈增加break;}printf("\n");}
//main.c
#include "singleList.h"int main(void){int array[] = {1,2,3,4,5,6,7,8,9,10};int len = sizeof(array)/sizeof(*array);int insertvalue = 100;Position p;pList head = creatList(array,len);printList(head->next); head = reversalList(head->next); printList(head); p = findNode(head,5); printf("the position value is : %d\n",p->value);//在元素為10前面插入100head = InsertList(head,10,insertvalue);printList(head);//刪除10000的節點head = deleteNode(head,10); printList(head);//刪除前端節點head = deleteNode(head,1);printList(head);//釋放鏈表head = freeList(head);printList(head);}
與單鏈表相比,在插入和刪除操作都簡化了。