一、实验目的及要求
- 掌握结构体类型变量的定义和使用;
- 掌握链表的概念,掌握对链表的综合操作操作;
- 进一步提高程序的设计能力和调试能力
- 要求:上机前先编制程序并画出程序框图;独立完成实验;独立完成实验报告。
3.从键盘上输入若干个数,以-1结束,按照与输入相反的顺序建立一个链表,并输出。要求建立链表和输出链表用函数实现。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void link_create(struct node* head)
{
struct node* p = head, * q;
int x;
printf("请输入一系列整数,以空格隔开,以-1结束");
scanf_s("%d", &x);
while (x != -1)//逆序链表
{
q = (struct node*)malloc(sizeof(struct node));
q->data = x; //p->next=head head=p p=malloc链表
q->next = p->next;
p->next = q;
scanf_s("%d", &x);
}
}
void link_print(struct node* head)
{
struct node* p = head->next;
while (p)
{
printf("%d", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
struct node head;
head.next = 0;
link_create(&head);
link_print(&head);
return 0;
}