C++链表插入问题
程序代码:
源码如下 应该是在void search(spra *head)函数中出的问题 大虾指点下 #include <iostream> using namespace std; int NodeNumber = 0; struct spra { char name; spra *next; }; spra *creat(int &number);//创建链表 void search(spra *head); void show(spra *head);//遍历 int find(spra *head); int main() { spra *head; int number = 0; head = creat (number); cout <<"你输入的字符数是:" <<find(head) <<endl; show (head); search(head); show(head); return 0; } spra *creat(int &number) //创建链表 { spra *head = NULL; spra *pend = head; spra *ps; char temp; cout <<"请输入字符:"<<endl; do { cin >>temp; if (temp != '#') { ps = new spra; ps->name = temp; ps->next = NULL; if (head == NULL) { head = ps; } else { pend->next = ps; } pend = ps; number++; } }while (temp != '#'); return head; } void show (spra *head) //遍历 { cout <<"你输入的字符如下:"<<endl; spra *te = head; while (te != NULL) { cout <<te->name; te = te->next; } cout <<endl; } void search(spra *head) { spra *al = head; spra *NewNode = new spra; cout <<"->"<<endl; NewNode->name = '!'; while (al != NULL) { if (al->name == 'a') { NewNode->next = al->next; al->next = NewNode; search(NewNode->next); } al = al->next; } } int find(spra *head) { spra *s = head; while(s != NULL) { NodeNumber++; s = s->next; } return NodeNumber; }