• 2007-03-23

    链表 - [C++]

    Tag:

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://lwyf.blogbus.com/logs/4838227.html

    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    typedef struct student
    {
        char name[10];
        student *next;
    };

    student *creatlink(int n)
    {
        student *head,*p,*q;
        int i;
        head = q = new student;
        q->next = NULL;
        cout<<"Input the name:"<<endl;
        cin>>q->name;
        
        for(i = 1;i <= n-1;i++)
        {
            p = new student;
            q->next = p;
            p->next = NULL;
            q = p;
            cout<<"Input the name:"<<endl;
            cin>>p->name;
        }
        return head;
    }

    void display(student *head)
    {
        int i;
        student *p,*ptr;
        ptr = p = head;
        while(p->next!=NULL)
        {
            cout<<ptr->name<<endl;
            p = ptr;
            ptr = ptr->next;
        }
    }

    student *insert(student *head,int inspos)
    {
        student *p,*q,*temp;
        int i = 0;
        cout<<"Please input the insert position(0~n):"<<endl;
        cin>>inspos;
        p = new student;    
        cout<<"Input insert name:"<<endl;
        cin>>p->name;
        
        if(inspos == 0)
        {
            temp = head;
            p->next = temp;
            head = p;
        }
        else
        {
            q = head;
            while(++i!=inspos)
            {
                q = q->next;
            }
            temp = q->next;
            q->next = p;
            p->next = temp;
            
        }
        cout<<"The inserted linklist is:"<<endl;
        display(head);
        return head;    
    }

    student *del(student *head,int delpos)
    {
        student *p,*q,*ptr,*temp;
        int i = 0;
        cout<<"Please input the delete position:"<<endl;
        cin>>delpos;
        
        ptr = head;
        if(0 == delpos)
        {
            head = head->next;
            delete ptr;
        }
        else
        {
            while(i++!=delpos)
            {
                q = ptr;
                ptr = ptr->next;            
            }
            temp = q->next;
            q->next = ptr->next;
            delete temp;
        }
        display(head);
        return head;
        
    }
    int main(int argc, char *argv[])
    {
        student *sam,*ptr,*temp,*head;
        int n,inspos,delpos;
        cout<<"Input n:"<<endl;
        cin>>n;    

        sam = creatlink(n);    
        cout<<"The original linklist is:"<<endl;
        display(sam);    
        head = insert(sam,inspos);
        ptr = del(head,delpos);
            
        while(ptr->next!=NULL)
        {
            temp = ptr;
            ptr = ptr->next;
            delete temp;    
        }            

        return EXIT_SUCCESS;
    }

    随机文章:

    上班这点事 2007-03-20
    移动图片 2007-03-14
    宽带 2007-02-01
    COBOL 2007-01-09
    猪年快乐 2007-01-06

    收藏到:Del.icio.us




    评论

  • To神仙:憨豆先生,我现在可是你的直接客户啦,有什么意见我就直接跟你反映,怎样
  • To小强:这是赢得掌声的代价,呵呵,因为你的扑救真的精彩,没白在他们面前夸你o(∩_∩)o
  • 我的手好痛啊!今天还在痛!
  • 好好学习,天天向上。