两道数据结构编程题

1.写出在顺序存储结构下将线性表逆转的算法,要求使用最少的附加空间。

解:输入:长度为n的线性表数组A(1:n)
输出:逆转后的长度为n的线性表数组A(1:n)。
C语言描述如下(其中ET为数据元素的类型):

2. 编写程序,将若干整数从键盘输入,以单链表形式存储起来,然后计算单链表中结点的个数(其中指针P指向该链表的第一个结点)。
解:编写C程序如下(已上机通过):
全局变量及函数提前说明:

#include<stdio.h>

#include<stdlib.h>

typedef struct liuyu{int data;struct liuyu*link;}test;

liuyu *p,*q,*r,*head;

int m=sizeof(test);

void main () /第一步,从键盘输入整数,不断添加到链表 /

{int i;

head=(test*)malloc(m); /m=sizeof(test); /

p=head; i=0;

while (i!=-9999)

{ printf("/ninput an integer [stop by '-9999']:");

scanf("%d",&i);

p->data=i; /* input data is saved /
p->link=(test
)malloc(m); /m=sizeof(test)); /

q=p;

p=p->link;

}

q->link=NULL; /原先用p->link=NULL似乎太晚!/

p=head; i=0; /统计链表结点的个数并打印出来 /

while (p->link!=NULL)

{printf("%d",p->data);

p=p->link;

i++;

}

printf("\n node number=%d\n", i-1); /结点的个数不包括-9999 /

}

相关推荐
5***g29835 分钟前
MySQL 数据库连接池爆满问题排查与解决
android·数据库·mysql
报错小能手40 分钟前
数据结构 带头节点的双向循环链表
数据结构·算法·链表
牛奔42 分钟前
php 8.2 配置安装php-zbarcode扩展
android·开发语言·php
Yolo_TvT1 小时前
数据结构:算法复杂度
数据结构·算法
带土11 小时前
14. 某马数据结构整理(2)
数据结构
OKkankan1 小时前
多态概念及使用
开发语言·数据结构·c++·算法
hudawei9961 小时前
kotlin协程编译成Android字节码后是什么样的
android·开发语言·kotlin
lxh01131 小时前
合并K个升序链表题解
前端·数据结构·链表
2501_915106321 小时前
iOS 抓不到包怎么办?从 HTTPS 代理排查到 TCP 数据流捕获的全链路解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
游戏开发爱好者81 小时前
APP上架苹果应用商店经验教训与注意事项
android·ios·小程序·https·uni-app·iphone·webview