awfkapjfak 2022-05-17 16:36 采纳率: 68.8%
浏览 156

数据结构 判断单链表是否有序

问题遇到的现象和发生背景

设计一个算法,判断一个单链表中各个结点值是否有序。程序框架如下:

问题相关代码,请勿粘贴截图

#include <stdio.h>
#include <stdlib.h>

typedef int datatype;
typedef struct link_node{
datatype info;
struct link_node *next;
}node;
typedef node *linklist;

linklist creatbyqueue()
{
linklist head,r,s;
datatype x;
head=r=(linklist)malloc(sizeof(node));
head->next=NULL;
printf("请输入整数序列(空格分开,以0结束):\n");
scanf("%d",&x);
while (x!=0)
{
s=(linklist)malloc(sizeof(node));
s->info=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return head;
}

linklist creatbystack()
{

linklist  head,s;
datatype x;
head=(linklist)malloc(sizeof(node));
head->next=NULL;

printf("请输入整数序列(空格分开,以0结束):\n");
scanf("%d",&x);
while (x!=0)
{
    s=(linklist)malloc(sizeof(node));
    s->info=x;

    s->next=head->next;
    head->next=s;

    scanf("%d",&x);
}
return head;

}

void print(linklist head)
{
linklist p;
int i=0;
p=head->next;
printf("List is:\n");
while(p)
{
printf("%7d",p->info);
i++;
if (i%10==0) printf("\n");
p=p->next;
}
printf("\n");

}

/请将本函数补充完整,并进行测试/
int issorted(linklist head,char c)
{
//待补充
}
int main() /测试程序/
{ linklist head;
head=creatbyqueue();
print(head);
if (issorted(head,'a')) printf("单链表head是升序排列的!\n");
else
if (issorted(head,'d')) printf("单链表head是降序排列的!\n");
else printf("单链表head是无序的!\n");
return 0;
}

运行结果及报错内容
我的解答思路和尝试过的方法

输入格式:
输入一个单链表序列。

输出格式:
输出单链表是否有序。

我想要达到的结果

输入样例:
11 22 33 44 55 0
输出样例:
请输入整数序列(空格分开,以0结束):
List is:
11 22 33 44 55
单链表head是升序排列的!

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-05-18 15:13
    关注

    供参考:

    #include <stdio.h>
    #include <stdlib.h>
    typedef int datatype;
    typedef struct link_node {
        datatype info;
        struct link_node* next;
    }node;
    typedef node* linklist;
    
    linklist creatbyqueue()
    {
        linklist head, r, s;
        datatype x;
        head = r = (linklist)malloc(sizeof(node));
        head->next = NULL;
        printf("请输入整数序列(空格分开,以0结束):\n");
        scanf("%d", &x);
        while (x != 0)
        {
            s = (linklist)malloc(sizeof(node));
            s->info = x;
            r->next = s;
            r = s;
            scanf("%d", &x);
        }
        r->next = NULL;
        return head;
    }
    
    linklist creatbystack()
    {
        linklist  head, s;
        datatype x;
        head = (linklist)malloc(sizeof(node));
        head->next = NULL;
        printf("请输入整数序列(空格分开,以0结束):\n");
        scanf("%d", &x);
        while (x != 0)
        {
            s = (linklist)malloc(sizeof(node));
            s->info = x;
    
            s->next = head->next;
            head->next = s;
    
            scanf("%d", &x);
        }
        return head;
    }
    
    void print(linklist head)
    {
        linklist p;
        int i = 0;
        p = head->next;
        printf("List is:\n");
        while (p)
        {
            printf("%7d", p->info);
            i++;
            if (i % 10 == 0) printf("\n");
            p = p->next;
        }
        printf("\n");
    
    }
    
    // 请将本函数补充完整,并进行测试
    int issorted(linklist head, char c)
    {
        //待补充
        int nTime = 0, flgL = 0, flgR = 0;
        if (head == NULL || head->next == NULL)
            return 0;
        linklist p = head->next;
        while (p->next) {
            if (p->info < p->next->info)
                flgR++;
            else if (p->info > p->next->info)
                flgL++;
            else
                return 0; //相等
            if (flgR && flgL)
                return 0;
            nTime++;
            p = p->next;
        }
        if (nTime == flgR)
            return c == 'a';
        else if (nTime == flgL)
            return c == 'd';
        else
            return 0;
    }
    int main() //测试程序
    { 
        linklist head;
        head = creatbyqueue();
        print(head);
        if (issorted(head, 'a')) 
            printf("单链表head是升序排列的!\n");
        else
            if (issorted(head, 'd')) 
                printf("单链表head是降序排列的!\n");
            else 
                printf("单链表head是无序的!\n");
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 5月17日

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug