单链表(建立、插入、删除、排序、逆置、打印)

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

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

node * creat(void)
{
    node *head,*p,*s;
    int x,cycle=1;
    head=(node*)malloc(sizeof(node));
    p=head;
    while(cycle)
    {
        printf("\nplease input the data( 0 means stop ):");
        scanf("%d",&x);
        if(x)
        {
            s=(node *)malloc(sizeof(node));
            s->data=x;
            printf("\n%d",s->data);
            p->next=s;
            p=s;
        }
        else cycle=0; //input x=0,then cycle=0,the for cycle is over;
    }
    head=head->next; //remove header
    p->next=NULL;
return head;
}

int length(node *head) //measuring the length of list
{
    int n=0;
    node *p;
    p=head;
    while(p)
    {
        p=p->next;
        n++;
    }
    return n;
}

int print(node *head) //print the list
{
    node *p;
    int n=0,i=1;
    n=length(head);
    printf("\nNow.These %d records are: ",n);
    p=head;
    if(!head) return -1;
    while(p)
    {
        printf("\n %d   ",p->data);
        p=p->next;
    }
}

node * del(node *head,int num)  //delete the node whose data == num
{
    node *p1,*p2;
    p1=head;
    while(num!=p1->data && p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num == p1->data)
    {
        if(p1 == head)
        {
            head=p1->next;
            free(p1);
        }
        else
            p2->next=p1->next;
    }
    else
        printf("\n%d could not been found",num);
    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
        {
            p0->next=p1;
            p2->next=p0;
        }
    }
    else
    {
        p1->next=p0;
        p0->next=NULL;
    }
    return head;
}

node * sort(node *head)
{
    node *p,*p2,*p3;
    int n,temp;

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

node * reverse (node *head)
{
    node *p1,*p2,*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;
}

int main (int argc, char **argv)
{
    node *head;
    int num,n;
    while(1)
    {
        printf("\n----------------");
        printf("\n******Menu******|");
        printf("\n0:exit          |");
        printf("\n1:cerat list    |");
        printf("\n2:delete a node |");
        printf("\n3:insert a node |");
        printf("\n4:print list    |");
        printf("\n5:sort list     |");
        printf("\n6:reverse list  |");
        printf("\n----------------");
        printf("\n please chose your number listed above: ");
        scanf("%d",&num);
        switch(num)
        {
            case 0: return 0;
            case 1: head=creat(); break;
            case 2: printf("\nchose the number you want delete: ");
                    scanf("%d",&n);
                    head=del(head,n);
                    break;
            case 3: printf("\nchose the number you want insert: ");
                    scanf("%d",&n);
                    head=insert(head,n);
                    break;
            case 4: print(head); break;
            case 5: head=sort(head); break;
            case 6: head=reverse(head); break;
        }
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。