注册 登录
编程论坛 C++教室

链表的初始化,插入,删除,查找。只写了链表的插入,后面的求补充

一样的 发布于 2014-04-25 15:06, 418 次点击
#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;
}
1 回复
#2
TonyDeng2014-04-26 12:30
搞懂了“插入”的原理,自然知道怎麽刪除和查找,後面的不會,祗能表明你的插入是抄來的。
1