C语言的数据结构,将一个顺序表里的所有数据元素以反转的方式加入到一个链表中,代码怎么写
已回答,莫辜负😊
————————————————————————————————————————
假设顺序表中的数据元素类型为int,链表节点的结构体定义如下:
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
struct ListNode {
int val;
struct ListNode *next;
};
// 反转顺序表并创建链表
struct ListNode* reverseList(int* nums, int numsSize) {
struct ListNode* head = NULL; // 头节点指针初始值为NULL
for (int i = numsSize - 1; i >= 0; i--) { // 遍历顺序表中的所有数据元素
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); // 创建新节点
node->val = nums[i]; // 将数据元素的值赋给新节点的val成员
node->next = head; // 将新节点的next指针指向当前头节点
head = node; // 将头节点指针指向新节点
}
return head; // 返回头节点指针
}
// 打印链表
void printList(struct ListNode* head) {
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
printf("\n");
}
// 测试
int main() {
int nums[] = {1, 2, 3, 4, 5};
int numsSize = sizeof(nums) / sizeof(int);
struct ListNode* head = reverseList(nums, numsSize);
printList(head); // 5 4 3 2 1
return 0;
}
#include <sys/timeb.h>
#if defined(WIN32)
# define TIMEB _timeb
# define ftime _ftime
#else
#define TIMEB timeb
#endif
time_t ltime1, ltime2, tmp_time;
struct TIMEB tstruct1, tstruct2;
ftime (&tstruct1); // start time ms
time (<ime1); // start time s
//work
time (<ime2); // end time sec
ftime (&tstruct2); // end time ms
tmp_time = (ltime2 * 1000 + tstruct2.millitm) - (ltime1 * 1000 + tstruct1.millitm);
下面的代码是一个可以在windows和linux平台下进行毫秒级计时的程序。
程序中是进行上万次的内存分配来耗时,演示计时的方法的。
毫秒级的计时的主要使用的函数ftime,使用ftime可以得到当前时间的毫秒和秒,从而我们可以得到毫秒级的计时。
但是如果要以毫秒为单位输出时间的话,必须使用64位的数据类型来表示。在linux上是long long,而windows下是使用__int64.并且如果使用printf的话,需要使用64位情况下对应的输出方式。不然会输出负数,这时就是溢出了。
linux下是:printf("%lld",n)
windows下是:printf(“%I64d",n)
#include <stdio.h>
#include <sys/timeb.h>
#include <stdlib.h>
#if defined(WIN32)
# define TIMEB _timeb
# define ftime _ftime
typedef __int64 TIME_T;
#else
#define TIMEB timeb
typedef long long TIME_T;
#endif
int time_interval()
{
struct TIMEB ts1,ts2;
TIME_T t1,t2;
int ti;
ftime(&ts1);//开始计时
//do some work
{
int i;
for(i=0;i<100000;i++)
{
int *p=malloc(10000);
int *q=malloc(10000);
int *s=malloc(10000);
int *t=malloc(10000);
free(p);
free(q);
free(s);
free(t);
}
}
ftime(&ts2);//停止计时
t1=(TIME_T)ts1.time*1000+ts1.millitm;
printf("t1=%lld\n",t1);
t2=(TIME_T)ts2.time*1000+ts2.millitm;
printf("t2=%lld\n",t2);
ti=t2-t1;//获取时间间隔,ms为单位的
return ti;
}
int main()
{
int ti=time_interval();
printf("time interval=%d\n",ti);
}
不过其实如果只是单纯的获得时间的间隔的话,也不用考虑64位的问题,因为将两个时间的秒一级的耗时相减的话结果就比较小了,代码如下:
#include <stdio.h>
#include <sys/timeb.h>
#include <stdlib.h>
#if defined(WIN32)
# define TIMEB _timeb
# define ftime _ftime
#else
#define TIMEB timeb
#endif
int time_interval()
{
struct TIMEB ts1,ts2;
time_t t_sec,ti;
ftime(&ts1);//开始计时
//do some work
{
int i;
for(i=0;i<100000;i++)
{
int *p=malloc(10000);
int *q=malloc(10000);
int *s=malloc(10000);
int *t=malloc(10000);
free(p);
free(q);
free(s);
free(t);
}
}
ftime(&ts2);//停止计时
t_sec=ts2.time-ts1.time;//计算秒间隔
t_ms=ts2.millitm-ts1.millitm;//计算毫秒间隔
ti=t_sec*1000+t_ms;
return ti;
}
int main()
{
int ti=time_interval();
printf("time interval=%d\n",ti);
}