华为机考3

链表操作

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;

typedef struct student
{
    int data;
    struct student *next;
}node;

/* 链表的创建*/
node *creat()
{
    node *head,*p,*s;
    int x;
    int cycle=1;
    head=(node*)malloc(sizeof(node));
    p=head;
    while(cycle)
    {
        cout<<"please enter a number"<<endl;
        cin>>x;
        if(x!=0)
        {
            s=(node*)malloc(sizeof(node));
            s->data=x;
            p->next=s;
            p=s;
        }
        else
            cycle=0;
    }
    head=head->next;
    p->next=NULL;
    return (head);
}

/*链表的测长*/        
int length(node *head)
{
    int n=0;
    node *p;
    p=head;
    while(p!=NULL)
    {
        p=p->next;
            n++;
    }
    return n;


}  
  
/*链表的排序*/  

node *sort(node* head, int n)
{
    int i;
    int j;
    int temp;
    node *p;
    if(head==NULL||head->next==NULL)
        return (head);
    p=head;
    for(i=1;i<n;i++)
    {
        p=head;
        for(j=0;j<n-i;j++)
        {
            if(p->data>p->next->data)
            {
                temp=p->next->data;
                p->next->data=p->data;
                p->data=temp;
            }
            p=p->next;
        }

    }
    return (head);


} 
               
/*链表的逆置*/ 
node *reverse(node* head)
{
    node *p1;
    node *p2;
    node *p3;
    if(head==NULL||head->next==NULL)
        return head;
    p1=head;
    p2=p1->next;
    while(p2)
    {
        p3=p2->next;
        p2->next=p1;
        p1=p2;
        p2=p3;
    }
    head->next=NULL;
    head=p1;
    return head;
}                    

/*链表插入一个节点*/  

node *insert(node* head,int num)
{
    node *p0,*p1,*p2;
    p1=head;
    p0=(node*)malloc(sizeof(node));
    p0->data=num;
    while((p0->data)>(p1->data)&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(p0->data<=p1->data)
    {
          if(head==p1)                                      //插入的是头结点
          {
              p0->next=p1;
              head=p0;
          }
          else 
          {
              p2->next=p0;                                  //插入的是中间结点
              p0->next=p1;
          }
    }
    else                                                    //插入的是尾结点
    {
        p1->next=p0;
        p0->next=NULL;
    }
return (head);
}                                                      

/*链表删除一个节点*/               
node *del(node* head,int num)
{
    node *p1,*p2;
    p1=head;
    while(num!=p1->data&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num==p1->data)
    {
        if(head==p1)
        {
            head=p1->next;
            free(p1);
        }
        else 
            p2->next=p1->next;
    }
else 
cout<<num<<"couldn't be found"<<endl;
return (head);
}
                                               

void main()
{
    node *p;
    node *head;
    int n;
    head=(node*)malloc(sizeof(node));
    head=creat();
    n=length(head);
    p=head;
    cout<<"原始链表是:";
    if(head!=NULL)                                                 //原始链表的打印
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
        cout<<endl;

        head=reverse(head);
         p=head;
        cout<<"链表长度为:"<<n;
/**************************************************************/  

        cout<<"逆置后链表是:";
        if(head!=NULL)                                                 //逆置后链表的打印
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
        cout<<endl;  
/**************************************************************/  

        head=sort(head,n);
        p=head;
        cout<<"排序后链表:";
        if(head!=NULL)                                                 //排序后链表的打印
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
        cout<<endl; 
 /**************************************************************/    
        int a;
        cout<<"insert a number:"<<endl;
        cin>>a;
        head=insert(head,a);
        p=head;
        cout<<"插入后链表是:";
         if(head!=NULL)                                                 //插入后链表的打印
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
        cout<<endl;
/**************************************************************/    
        int b;
        cout<<"delete number:"<<endl;
        cin>>b;
        head=del(head,b);
        p=head;
        cout<<"删除后链表是:";
         if(head!=NULL)                                                 //删除后链表的打印
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
        cout<<endl;                             

}
View Code
原文地址:https://www.cnblogs.com/fickleness/p/3361285.html