// 链表.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
using namespace std;
typedef struct Node {
int date;
struct Node* next;
}node;
node* newHeadNode();
void input(node* head);
void readALL(node* head);
int main()
{
node* head = NULL;
printf("please input date:\n");
head= newHeadNode(); //新建一个头结点
input(head); //往链表内加入新数据
readALL(head); //遍历读出所有数据
}
node* newHeadNode() {
node* firstp=NULL;
firstp = (node*)malloc(sizeof(node));
firstp->date = 0;
firstp->next = NULL;
return firstp;
}
void input(node* head) {
int number;
node* last=head;
while (1)
{
cin >> number;
if (number!=-1)
{
node* newp;
newp = (node*)malloc(sizeof(node));
newp->date = number;
newp->next = NULL;
if (last) {
while (last->next)
{
last = last->next;
}
last->next = newp;
}
else
{
head->next = newp;
last = head;
}
}
else
{
break;
}
}
}
void readALL(node* head) {
node* read = head->next;
while (read!=NULL) {
printf("%d ", read->date);
read = read->next;
}
}
C语言学习链表的一些心得
最新推荐文章于 2023-03-29 00:06:29 发布