首页 > 代码库 > 20140429

20140429

1、单链表循环体用while(p->next!=NULL)而不用while(p!=NULL)的原因

node *Find_MidNode(node *head)
{
    if(head->next==NULL||head->next->next==NULL)
        return head->next;
    node *p=head->next,*mid=head->next;
    while(p->next->next!=NULL)  // (1->2->3->4->NULL)p最终等于3;(1->2->3->4->5->NULL)p最终等于5 
    {
        p=p->next->next;
        mid=mid->next;
        if(p->next==NULL)
            break;
    }
    return mid;
}

2、单链表 的 找中间元素

#include<stdio.h>
#include<iostream>
#include<malloc.h>
#define swap(x,y) x=(x)+(y); y=(x)-(y); x=(x)-(y)
typedef struct student
{
    int data;
    struct student *next; 
}node;

node *create( )   //尾插法建立带头结点head的单链表
{
    node * head,*rear,*s;
    int x=0;
    head=(node *)malloc(sizeof(node *));
    head->data=http://www.mamicode.com/0;
    printf("please input data: ");
    scanf("%d",&x);
    rear=head;
    while(x!=0)
    {
        s=(node *)malloc(sizeof(node));
        s->data=http://www.mamicode.com/x;
        rear->next=s;
        rear=s;
        head->data++;
        printf("please input data: ");
        scanf("%d",&x);
    }
    rear->next=NULL;   //这句话一定加上,不然后果很严重
    return head;
}
int length(node *head)
{
    int n=0;
    node *rear=head;
    while(rear->next!=NULL)
    {n++;    rear=rear->next;}
    printf("the length is %d\n",n);
    return n;
}

void display(node *head)
{
    node *rear=head->next;
    while(rear!=NULL)  
    {
        printf("%d ",rear->data);
        rear=rear->next;
    }
    printf("\n");
}
int del_node1(node *head,int num)  //删除第一个data为num的节点
{
    node *p=head->next,*q=head;
    while(num!=p->data&&p!=NULL)
    {
        q=p;
        p=p->next;
    }
    if(p!=NULL)
    {
        q->next=p->next;
        free(p);
    }
    else 
        printf("\n could not find %d\n",num);
    return num;
}

void insert_node(node *head,int num)   //在第一个大于等于num的节点之前插入, 带头结点的单链表,中间,末尾,开头插入都一样
{
    node *p=head->next,*q=head;
    node *s=(node *)malloc(sizeof(node));
    s->data=http://www.mamicode.com/num;
    while(num>p->data&&p!=NULL)
    {
        q=p;
        p=p->next;
        if(p==NULL)
            break;
    }
    q->next=s;
    s->next=p;
}

void sort(node *head) //升序,冒泡法
{
    int n=length(head);
    node *p,*q;
    for(int i=0;i<n;i++)
    {
        p=head->next;
        q=p->next;
        for(int j=1;j<(n-i);j++)//对于循环次数,以需要交换的次数来确定。
        {
            if(p->data>q->data)
            {swap(p->data,q->data);}
            p=p->next;
            q=p->next;
        }
    }
}

void reverse(node *head)
{
    if(head->next==NULL||head->next->next==NULL)
        return;
    node *p1=head->next,*p2=p1->next,*p3;
    while(p2!=NULL)
    {
        p3=p2->next;
        p2->next=p1;
        p1=p2;
        p2=p3;
    }
    head->next->next=NULL;
    head->next=p1;
}

node *Find_MidNode(node *head)
{
    if(head->next==NULL||head->next->next==NULL)
        return head->next;
    node *p=head->next,*mid=head->next;
    while(p->next->next!=NULL)  //保证最后一次进入循环之前:p为2,进入之后,p=p->next->next后,p为4。 (1->2->3->4->NULL) 
    {
        p=p->next->next;
        mid=mid->next;
        if(p->next==NULL)
            break;
    }
    return mid;
}

void main()
{
    node *head=create();
    int delnode;
    length(head);
    display(head);
    // del_node1(head,1);
    //insert_node(head,3);
    //sort(head);
    //reverse(head);
    printf("%d",Find_MidNode(head)->data);
    //display(head);
}