要用malloc函数和单链表insert,还有要怎么证明这个插入函数在整个管理系统中是正确的?求大佬解答!拜托啦!!!
2条回答 默认 最新
- 一颗小码农 2021-07-03 03:30关注
插入函数
void Insert() { cout << "输入要插入的序号为" << endl; node* p = new node; cin >> p->data; p->next = NULL; cout << "输入在哪个序号前插入" << endl; int t; cin >> t; node* q = head->next, * o = head;//o记录q的前一个结点 while(q) { if(q->data!=t) { o = o->next; q = q->next; } else { p->next = q; o->next = p; break; } } if(q==NULL)cout << "序号输入有误" << endl; else { cout << "插入后结果如下" << endl; for (node* p = head->next; p; p = p->next) cout << p->data<<'\t'; cout << endl; } }
完整验证代码
#include<iostream> using namespace std; typedef struct node { int data; node* next; }; class List { private: node* head, * last; public: List()//初始化 { head = new node; head->next = NULL; last = head; } void CreateList()//链表创建 { cout << "节点数" << endl; int n; cin >> n; cout << "输入结点序号" << endl; for (int i = 0; i < n; ++i) { node* p = new node; cin >> p->data; //尾插法 last->next = p; last = p; } last->next = NULL; } void OutCome() { cout << "链表数据如下" << endl; for (node* p = head->next; p; p = p->next) cout << p->data << '\t'; cout << endl; } void Insert() { cout << "输入要插入的序号为" << endl; node* p = new node; cin >> p->data; p->next = NULL; cout << "输入在哪个序号前插入" << endl; int t; cin >> t; node* q = head->next, * o = head;//o记录q的前一个结点 while(q) { if(q->data!=t) { o = o->next; q = q->next; } else { p->next = q; o->next = p; break; } } if(q==NULL)cout << "序号输入有误" << endl; else { cout << "插入后结果如下" << endl; for (node* p = head->next; p; p = p->next) cout << p->data<<'\t'; cout << endl; } } }; int main() { List l; l.CreateList(); l.OutCome(); l.Insert(); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
- ¥15 错误 LNK2001 无法解析的外部符号
- ¥50 安装pyaudiokits失败
- ¥15 计组这些题应该咋做呀
- ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
- ¥15 让node服务器有自动加载文件的功能
- ¥15 jmeter脚本回放有的是对的有的是错的
- ¥15 r语言蛋白组学相关问题