链表的结构定义
typedef struct LinkedList { char data; LinkedList *next; }Node;
链表的创建
Node *create(const char *ch) { int len = (int)strlen(ch); Node *head, *phead; phead = (Node *)malloc(sizeof(Node)); phead->data = ch[0]; head = phead; for(int i=1; i<len; i++) { Node *pNode; pNode=(Node *)malloc(sizeof(Node)); pNode->data=ch[i]; phead->next = pNode; phead = phead->next; } phead->next = NULL; return head; }
Node *create(int len) { Node *head, *phead; char ch; head = NULL; phead = NULL; while(len--) { cin>>ch; Node *pNode; pNode=(Node *)malloc(sizeof(Node)); pNode->data=ch; if(!phead) { phead = pNode; head = phead; } else { phead->next=pNode; phead = phead->next; } } phead->next = NULL; return head; }
链表结点的插入
/*结点的插入*/ Node *insert(Node *head, char ch) { Node *phead, *pNode, *preNode; phead = head; preNode = NULL; pNode = (Node *)malloc(sizeof(Node)); pNode->data = ch; while(pNode->data > phead->data && phead->next) { preNode = phead; phead = phead->next; } if(pNode->data <= phead->data) { //插入的是头结点 if(head == phead) { pNode->next=phead; head = pNode; } //插入的是中间 else { preNode->next = pNode; pNode->next = phead; } } //插入的是尾结点 else { phead->next = pNode; pNode->next = NULL; } return head; }链表结点的删除
/*结点的删除*/ Node *del(Node *head, char ch) { Node *phead, *preNode; phead = head; preNode = NULL; while(phead) { if(ch == phead->data) { if(phead == head) head=phead->next; else preNode->next = phead->next; } preNode = phead; phead=phead->next; } return head; }
链表结点的排序
int length(Node *head) { int len=0; Node *ptr=head; while(ptr) { ptr=ptr->next; len++; } return len; } /*链表排序*/ Node *sort(Node *head) { if(!head || !head->next) return head; Node *phead = head; char tmp; int len = length(head); for(int i=0; i<len; i++) { phead=head; for(int j=0; j<len-i-1; j++) { if(phead->data > phead->next->data) { tmp = phead->next->data; phead->next->data = phead->data; phead->data = tmp; } phead = phead->next; } } return head; }
链表结点的逆置
/*链表逆置*/ Node *reverse(Node *head) { Node *phead, *pNode; pNode = head; phead = NULL; while(pNode) { Node *pNext = pNode->next; if(pNext == NULL) head = pNode; pNode->next = phead; phead = pNode; pNode = pNext; } return head; }
测试用例Code:
#include <iostream> #include <string> #include <stdlib.h> #include <iomanip> #include <math.h> #include <vector> using namespace std; #define MAXLEN 10001
void printList(Node *head) { Node *phead; phead = head; while(phead) { cout<<phead->data<<" "; phead=phead->next; } cout<<endl; return ; }
int main(int argc, char** argv) { bool is_num=true; char ch[MAXLEN]; Node *phead; cin>>ch; int len = strlen(ch); while( len-- && is_num ) { if(!isdigit(ch[len])) is_num=false; } if(is_num) { cout<<"请输入长度为"<<atoi(ch)<<"的字符链表:"<<endl; phead = create(atoi(ch)); } else phead = create(ch); cout<<"链表创建时的数据"<<endl; printList(phead); phead = reverse(phead); cout<<"链表逆置后的数据"<<endl; printList(phead); phead = sort(phead); cout<<"链表排序后的数据"<<endl; printList(phead); phead = insert(phead, 'x'); cout<<"链表插入新数据x后的数据"<<endl; printList(phead); phead = del(phead, 'x'); cout<<"链表删除数据x后的数据"<<endl; printList(phead); phead = del(phead, 'a'); cout<<"链表删除数据a后的数据"<<endl; printList(phead); system("pause"); return 0; }