尾插法建立链表分解

来源:互联网 发布:听戏曲的软件 编辑:程序博客网 时间:2024/05/21 16:58

尾插法建立链表可以看成下面步骤(add函数)的循环:

struct LineNode{    char line[256];    LineNode* next;};LineNode LineHead;void add(const char* line){    LineNode* obj=new LineNode;    strcpy(obj->line,line);    obj->next=NULL;    LineNode* p=&LineHead;    while(p->next)    {        p=p->next;    }    p->next=obj;}


下面这种在插入的时候就能实现排序:

struct node{    int data;    node* next;};node head;void add(){    int x;    node* p=new node;    cin>>x;    p->data=x;    node* r=&head;    while(r->next)    {            if(r->next->data>p->data)                break;        r=r->next;    }    p->next=r->next;    r->next=p;}


原创粉丝点击