大家看看这个单链表的删除操作合理不

1.  前言

好久没写过单链表了,再刷题的时候遇见了,然后就写了下面的答案,但是在评论区看见有的同学说删除的时候要用到二级指针,但是我觉得一级指针就可以做到。下面看代码。

2. 正文

以下是我的代码

读入10个复数,建立对应链表,然后求所有复数的和。

#includeusing namespace std;

typedef struct ListPlural{
int real;
int imaginary;
ListPlural *next;
}listdata;

//创建头节点
listdata *creatListHead(){
listdata * head = new listdata;
if(head == NULL){
return NULL;
}
head -> next = NULL;
return head;
}

//增加数据
void addData(listdata *head, const int &real, const int &imaginary){
listdata *data = new listdata;
listdata * temp;
data -> real = real;
data -> imaginary = imaginary;
for(temp = head; temp -> next != NULL; temp = temp -> next){}
temp -> next = data;
data -> next = NULL;
}

//链表的遍历
void listTraverse(listdata *head){
listdata *temp;
int real = 0, imaginary = 0;
for(temp = head -> next; temp != NULL; temp = temp -> next){
real += temp -> real;
imaginary += temp -> imaginary;
}
cout << real << "+" << imaginary << "i" << endl;
}

//链表的删除
void listDelete(listdata *head){
listdata *temp = head -> next;
while(head-> next -> next != NULL){
temp = head->next;
head -> next = head -> next->next;
delete temp;
}
temp = head -> next;
delete temp;
delete head;
}
using namespace std;


//任务函数
int main()
{
listdata *head;
int real, imaginary;
head = creatListHead();
for(int i = 1; i > real,cin >> imaginary;
addData(head, real, imaginary);
}
listTraverse(head);
listDelete(head);
return 0;
}

下面是另一位同学的代码部分

void link_free(STU **p_head)
{
    STU *p_mov;
    while(*p_head!=NULL)
    {
        p_mov=*p_head;
        *p_head=p_mov->next;
        free(p_mov);
    }
 
}

3. 备注

以上就是两种释放方式,请各位看到有问题或是自己的见解,在评论区讨论。

>>>>>>

生命始于精彩,却不止于精彩。

>>>>>> 

好像越来越烦躁了。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华的地方不一定留下你的脚印

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值