zoukankan      html  css  js  c++  java
  • 单链表(建立、插入、删除、排序、逆置、打印)

    #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("
    please input the data( 0 means stop ):");
            scanf("%d",&x);
            if(x)
            {
                s=(node *)malloc(sizeof(node));
                s->data=x;
                printf("
    %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("
    Now.These %d records are: ",n);
        p=head;
        if(!head) return -1;
        while(p)
        {
            printf("
     %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("
    %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("
    ----------------");
            printf("
    ******Menu******|");
            printf("
    0:exit          |");
            printf("
    1:creat list    |");
            printf("
    2:delete a node |");
            printf("
    3:insert a node |");
            printf("
    4:print list    |");
            printf("
    5:sort list     |");
            printf("
    6:reverse list  |");
            printf("
    ----------------");
            printf("
     please chose your number listed above: ");
            scanf("%d",&num);
            switch(num)
            {
                case 0: return 0;
                case 1: head=creat(); break;
                case 2: printf("
    chose the number you want delete: ");
                        scanf("%d",&n);
                        head=del(head,n);
                        break;
                case 3: printf("
    chose 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;
            }
        }
    }
  • 相关阅读:
    jQuery获取当前元素是该父元素的第几个元素&获取父元素的第n个子元素
    获取当前月份的天数
    获取当年的每个月份的天数:
    解决ul下的li换行问题,(父元素div加overflow:scroll没作用的问题)
     获取当年的月份的天数:
    程序员成长道路上必经的几个阶段
    CSS如何让文字垂直居中?
    2015年7个重要的Web设计趋势
    引入css文件时,css link和@import区别
    在html页面引用css文件的方法
  • 原文地址:https://www.cnblogs.com/zhanbiqiang/p/4054925.html
Copyright © 2011-2022 走看看