IDE: VS2005 list.h #ifndef LIST_H #define LIST_H typedef char DataType; typedef struct node { DataType data; struct node *next; }ListNode; typedef ListNode *LinkList; LinkList CreateList(void); // 求链表长度 int ListLenght(const ListNode *head, int &length); int PrintList(const LinkList head); void ReleaseList(LinkList head); ListNode* GetNode(const ListNode *head, const int i); ListNode* GetNode(const ListNode *head, const DataType key); int InsertList(const ListNode *head, const DataType x, const int pos); int DeleteList(const ListNode *head, const int pos); int ReverseList(ListNode *&head); #endif list.cpp #include "list.h" #include <cstdio> #include <cstdlib> #include <cassert> LinkList CreateList(void) { char ch='1'; LinkList head = (LinkList)malloc(sizeof(ListNode)); ListNode *s=NULL; ListNode *r=NULL; r= head; while ('/n'!=(ch = getchar())) { s =(LinkList)malloc(sizeof(ListNode)); s->data = ch; r->next =s; r=s; } r->next = NULL; return head; } // 求链表长度 int ListLenght(const ListNode *head, int &length) { assert(head); if (NULL == head) { return 0; } length = 0; const ListNode *node = head->next; while (node) { length++; node = node->next; } return 1; } int PrintList(const LinkList head) { assert(head); if (head) { LinkList temp = head->next; if ( NULL == temp) { return 1; } while(NULL != temp) { printf("%c ", temp->data); temp = temp->next; } printf("/n"); return 1; } return 0; } void ReleaseList(LinkList head) { assert(head); LinkList temp = NULL; while(NULL != head) { temp = head->next; free(head); head = temp; } } ListNode* GetNode(const ListNode* head, int i) { assert(head); if (NULL == head) { return NULL; } ListNode* temp = const_cast<ListNode*>(head); int count = 0; for (; temp->next && count <i; count++) { temp = temp->next; } if (count == i) { return temp; } return NULL; } ListNode* GetNode(const ListNode* head, const DataType key) { assert(head); if (NULL == head) { return NULL; } ListNode *temp = head->next; while (temp && (temp->data!=key )) { temp = temp->next; } return temp; } int InsertList(const ListNode *head, const DataType x, const int pos) { if (NULL == head) { return 0; } ListNode *node = GetNode(head, pos-1); if (NULL == node) { // 插入位置有误 return 0; } ListNode *newnode = (LinkList)(malloc(sizeof(ListNode))); newnode->data = x; newnode->next = node->next; node->next = newnode; return 1; } int DeleteList(const ListNode *head, const int pos) { assert(head); if (NULL == head) { return 0; } ListNode *node = GetNode(head, pos-1); if (NULL == node|| NULL == node->next) { // 删除位置有误 return 0; } ListNode * deletenode = node->next; node->next = deletenode->next; free(deletenode); return 1; } int ReverseList(ListNode *&head) { assert(head); if (NULL == head) { return 0; } ListNode *p1 = head->next; if (NULL == p1) { return 1; } ListNode *p2 = p1->next; if (NULL == p2) { return 1; } ListNode *temp = NULL; p1->next = NULL; while(p2) { temp = p2->next; p2->next = p1; p1 = p2; p2 = temp; } head->next = p1; return 1; } main.cpp #include "list.h" void main(void) { // 创建链表 LinkList list = CreateList(); PrintList(list); // 逆转链表 ReverseList(list); PrintList(list); ListNode *node = GetNode(list, 'f'); // 插入元素 InsertList(list, 'r', 5); PrintList(list); // 删除元素 DeleteList(list, 2); PrintList(list); // 释放链表 ReleaseList(list); }