开发者社区> 问答> 正文

设待排序的记录序列用单链表做存储结构,试写出插入排序算法。。。。

设待排序的记录序列用单链表做存储结构,试写出插入排序算法。。。。

展开
收起
知与谁同 2018-07-21 12:55:05 4768 0
1 条回答
写回答
取消 提交回答
  • 这个时候,玄酱是不是应该说点什么...
    #include"stdafx.h"
    #include "stdio.h"
    typedef struct node{
    int data;
    struct node *next;
    }node;
    void main()
    {
    node *create(){
    node *head,*p,*q;
    q = head;
    int i=0;
    int x;
    head=(node *)malloc(sizeof(node));
    while(1)
    {
    printf("please input the node:");
    scanf("%d",&x);
    if(x==0) {break;}
    p=(node *)malloc(sizeof(node));
    p->data=x;
    if(++i==1)
    {
    head->next=p;
    }
    else
    {
    q->next=p;
    }
    q=p;
    }
    q->next=NULL;
    return head;
    }
    node *search(node *head,int pos)
    {
    node *p;
    int len=length(head);
    p=head->next;

    if(pos<0) {

    printf("incorrect position!\n"); return NULL;
    }
    else if(pos>len)
    {
    printf("incorrect position!\n"); return NULL;
    }
    else if(pos==0) {

    return head;
    }
    if(p==NULL) {

    printf("the link is empty!\n"); return NULL;
    }
    while (--pos)

    { p=p->next; }

    return p;
    }
    node *insert(node *head,int pos,int x)
    {
    node *p,*q=NULL;
    q=(node *)malloc(sizeof(node));
    q->data=x;
    if(pos==0) {

    head->next=q; return head; }

    p=search(head,pos);

    if(p!=NULL) { q->next=p->next; p->next=q; }

    return head;
    }
    }
    2019-07-17 22:50:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
数据+算法定义新世界 立即下载
袋鼠云基于实时计算的反黄牛算法 立即下载
Alink:基于Apache Flink的算法平台 立即下载