链表练习(创建和输出)

一、题目1

使用头插法创建链表(形参是int n,代表指定创建结点个数),要求创建两个链表并分别输出查看,第1个链表有5个结点,第2个链表3个结点,链表的作用是存储字符型数据。

二、题目1解题

(1)解题思路:分主函数、创建函数、输出函数三个函数,创建函数形参为节点数n,输出函数返回值为void,主函数中得到用户要创建的链表个数m,运用循环分别获得用户每个函数要的节点数n,随后分别调用创建函数和输出函数,另外定义一个全局结构体指针数组存放每个链表的头结点地址,方便后续添加功能时可调用,如第二问。

(2)运行结果:

 (3)代码:

#include<iostream>

using namespace std;

struct AB

{

char zf;

AB* next;

};

AB* LinkedList[10] = {0};//分别存放链表的头节点地址

AB* head_insert(int n)

{

AB* head = NULL;//head指向头节点,p指向新节点

AB* p = NULL;

head = new AB;

head->next = NULL;

cout << "请输入这个节点的数据:";

cin >> head->zf;

while(n-- > 1)//上面已经创建了一个节点所以大于 1而不是大于 0

{

p = new AB;

cout << "请输入这个节点的数据:";

cin >> p->zf;

p->next = head;

head = p;

}

return head;

}

void put_linkedlist(int i,int n)//第二种方法就是不传 n,for循环的条件改为 head != NULL

{

AB* head;

cout << "输出第" << i << "个链表数据:";

head = LinkedList[i-1];

for(int j = 0;j < n;j++)

{

cout << head->zf << '\t';

head = head->next ;

}

return;

}

int main()

{

int m,n;//m:用户输入要创建的链表个数 n:链表节点数

cout << "请输入要创建的链表个数:";

cin >> m;

for(int i = 1;i <= m;i++)

{

cout << "请输入第" << i << "个链表的节点数:";

cin >> n;

LinkedList[i-1] = head_insert(n);

put_linkedlist(i,n);

cout << endl;//美观

}

return 0;

}

 三、题目2

在题目1的基础上,在第1个链表的第3个结点后插入一个新的结点,存储字符‘C’,在第2个链表的尾结点后插入一个新的结点,存储字符'D'. 输出查看两个链表数据。

四、题目2解题

(1)运行结果:

 (2)代码:

#include<iostream>

using namespace std;

struct AB

{

char zf;

AB* next;

};

AB* LinkedList[10] = {0};//分别存放链表的头节点地址

int num[10] = {0};//存放链表节点数

AB* head_insert(int n)

{

AB* head = NULL;//head指向头节点,p指向新节点

AB* p = NULL;

head = new AB;

head->next = NULL;

cout << "请输入这个节点的数据:";

cin >> head->zf;

while(n-- > 1)//上面已经创建了一个节点所以大于 1而不是大于 0

{

p = new AB;

cout << "请输入这个节点的数据:";

cin >> p->zf;

p->next = head;

head = p;

}

return head;

}

void del(AB* temp1)//删除节点

{

AB* temp2 = NULL;

while(temp1 != NULL)

{

AB* move = temp1;

temp1 = temp2->next ;

temp2 = temp1;

delete move;

}

return;

}

void put_linkedlist(int i)

{

AB* head = LinkedList[i-1];

cout << "输出第" << i << "个链表数据:";

while(head != NULL)

{

cout << head->zf << '\t';

head = head->next ;

}

return;

}

AB* add(int r,AB* head)//中间加节点

{

AB* temp = head;//找到节点

AB* p = new AB;//创建新节点

cout << "请输入新节点的数据";

cin >> p->zf;

for(int i = 1;i != r && temp != NULL;i++)

{

temp = temp->next;

}

if(temp->next == NULL)//尾部加节点

{

p->next = NULL;

temp->next = p;

}

else//中间加节点

{

p->next = temp->next;//避免内存泄露

temp->next = p;

}

//num[r-1] ++;

return head;

}

int main()

{

int m,n;//m:用户输入要创建的链表个数 n:链表节点数

cout << "请输入要创建的链表个数:";

cin >> m;

for(int i = 1;i <= m;i++)

{

cout << "请输入第" << i << "个链表的节点数:";

cin >> num[i-1];

LinkedList[i-1] = head_insert(num[i-1]);

put_linkedlist(i);

cout << endl;//美观

}

int x,r,choice;//加在 r节点后

while(1)

{

int i = 1;

cout << "请输入要操作哪个链表:";

cin >> x;

for(;i != x;i++);//索引到要操作的链表

cout << "请输入要在哪个节点后加节点:";

cin >> r;

LinkedList[i-1] = add(r,LinkedList[i-1]);

cout << "是否继续(继续1,结束0)?";

cin >> choice;

if(choice == 0)  break;

}

for(int i = 1;i <= m;i++)

{

put_linkedlist(i);

}

for(int i = 0;LinkedList[i] != 0;i++)

{

del(LinkedList[i]);

}

return 0;

}

(3)遇到的困难或改进之处:

①改进:增加了删除节点的函数

②问题:我定义了一个存放各链表节点数的数组,但是在增加节点后对应数组的值(也就是某链表的节点数)无法更新,导致后面输出加节点后的链表时无法输出最后那个节点的数据

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值