目录
源码
#include<iostream>
#include <string>
using namespace std;
typedef struct Node
{
// 存放数据
int data;
// 存放下一个节点
Node* next;
}LinkList;
// 菜单界面
void showMenu() {
cout << "1、创建一条含整数结点的无序链表" << endl;
cout << "2、链表结点的输出" << endl;
cout << "3、链表结点的升序排序" << endl;
cout << "4、分别计算链表中奇数和偶数结点之和并输出" << endl;
cout << "5、释放链表" << endl;
cout << "0、退出" << endl;
}
LinkList * createLinkList(LinkList *head) {
cout << "请输入数字,输入 -1 结束循环" << endl;
// 头节点,不保存数据
//LinkList * head = new LinkList;
head = (LinkList *)malloc(sizeof(LinkList));
head->next = NULL;
LinkList * pre = head;
bool num = true;
int data = -1;
while (num)
{
cin >> data;
if (data == -1) {
pre->next = NULL;
// 输入-1,跳出循环
cout << "输入结束" << endl;
system("pause"); // 请按任意键继续
system("cls"); // 清屏
break;
}
else {
//LinkList *p = new LinkList;
LinkList *p = (LinkList *)malloc(sizeof(LinkList));
p->data = data;
pre->next = p;
pre = p;
}
}
return head;
}
// 判断链表是否是空
bool ListEmpty(LinkList *L)
{
if (L==NULL) {
return true;
}
else {
return(L->next == NULL);
}
}
// 获取链表长度
int ListLength(LinkList *L)
{
LinkList *p = L;
int i = 0;
while (p->next != NULL)
{
i++;
p = p->next;
}
return(i);
}
// 输出链表各个节点
void showNode(LinkList * head) {
if (ListEmpty(head)) {
cout << "链表为空!!!" << endl;
}
else {
LinkList *node = head->next;
while (node != NULL) {
cout << "节点值是:" << node->data << endl;
node = node->next;
}
cout << "链表输出
标签:LinkList,NULL,cout,作业,next,链表,课程,数据结构,data
From: https://blog.csdn.net/qq_38593865/article/details/143662270