#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
//struct ListNode {
// int val;
// struct ListNode *next;
// ListNode(int x) :
// val(x), next(NULL) {
// }
//};
int main(){
//ListNode *head=new ListNode(0);
int k;
//cin>>k;
int t=8;
ListNode* phead = NULL;
phead = new ListNode;
phead->m_nKey = 0;
phead->m_pNext = NULL;
ListNode* p = phead;
for (int i = 1; i < 8; ++i) {
ListNode* pNewNode = new ListNode;
pNewNode->m_nKey = i; // 将新节点的值赋值为i
pNewNode->m_pNext = NULL;
p->m_pNext = pNewNode; // 上一个节点指向这个新建立的节点
p = pNewNode; // p节点指向这个新的节点
}
//插入
ListNode* p2 = phead;
for (int i = 1; i < 8; i++) {
//ListNode* node=p2->m_pNext;
if(i==2){
ListNode* m=new ListNode() ;
m->m_nKey=15;
m->m_pNext=p2->m_pNext;
p2->m_pNext=m;
t++;
break;
}
else
p2=p2->m_pNext;
}
//删除
ListNode* p3 = phead;
for (int i = 1; i < t; i++) {
ListNode* node=p3->m_pNext;
if(i==5){
p3->m_pNext=node->m_pNext;
t--;
break;
}
else
p3=p3->m_pNext;
}
ListNode* p1 = phead;
for (int i = 1; i < t; ++i) {
ListNode* node=p1->m_pNext;
//if(i==7-k+1){
cout<<node->m_nKey<<endl;
//}
p1=p1->m_pNext;
}
}
链表的插入删除
最新推荐文章于 2022-06-04 19:18:17 发布