目录
4.addAtindex(int index,int val) 在索引处添加值
6.deleteAtindex(int index) 删除索引处的值
1.get(int index) 搜索索引index处的值
int get(int index)
{
if(index<0 && index>size-1)
{
return -1;
}
ListNode*cur=vir_node->next;
while (index--)//往下搜寻index次
{
cur=cur->next;
}
return cur->val;
}
2.addAthead(int val) 在表头添加节点
void addAthead(int val)
{
ListNode* newnode=new ListNode(val);
newnode->next=vir_node->next;
vir_node->next=newnode;
size++;
}
3.addAttail(int val)尾部添加节点
void addAttail(int val)
{
ListNode* new_node=new ListNode(val);
ListNode* cur=vir_node;
while (cur->next!=nullptr)
{
cur=cur->next;
}
cur->next=new_node;
size++;
}
4.addAtindex(int index,int val) 在索引处添加值
void addAtindex(int index,int val)
{
if(index>size)
{
return;
}
ListNode* new_node=new ListNode(val);
ListNode* cur=vir_node;
while (index--)
{
cur=cur->next;
}
new_node->next=cur->next;
cur->next=new_node;
size++;
}
5.deleteAtval(int val) 删除某个节点
void deleteAtval(int val)
{
ListNode* cur=vir_node;
while (cur->next!=NULL)
{
if(cur->next->val==val)
{
ListNode* tmp=cur->next;
cur->next=cur->next->next;
delete tmp;
size--;
}
cur=cur->next;
}
}
6.deleteAtindex(int index) 删除索引处的值
void deleteAtindex(int index)
{
if(index>=size|| index<0)
return;
ListNode*cur=vir_node;
while (index--)
{
cur=cur->next;
}
ListNode*tmp =cur->next;
cur->next=cur->next->next;
delete tmp;
size--;
}
7.printLinklist() 打印链表
void printLinklist()
{
ListNode* cur=vir_node;
while (cur->next!=nullptr)
{
cout<<cur->next->val<<' ';
cur=cur->next;
}
}
8.reverse_listnode()翻转链表
//循环方式翻转链表
void reverse_listnode()
{
ListNode* head=vir_node->next;
ListNode* cur=head;
ListNode*tmp;
ListNode*pre=NULL;
while (cur)
{
tmp=cur->next;
cur->next=pre;
pre=cur;
cur=tmp;
}
cout<<"翻转后: "<<endl;
while(pre!=nullptr)
{
cout<<pre->val<<' ';
pre=pre->next;
}
}
//递归方式翻转链表
/*ListNode* reverse_listnode_2(ListNode* pre,ListNode* cur){
if(cur == NULL) return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse_listnode_2(cur,temp);
}
void reverseList(ListNode* head)
{
reverse_listnode_2(NULL, head);
}*/
总代码:包括节点初始化等。
#include <iostream>
#include <string.h>
using namespace std;
class Mylink_list
{
public:
struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){}
};
Mylink_list()
{
vir_node= new ListNode(0);
size=0;
}
int get(int index)
{
if(index<0 && index>size-1)
{
return -1;
}
ListNode*cur=vir_node->next;
while (index--)//往下搜寻index次
{
cur=cur->next;
}
return cur->val;
}
void addAthead(int val)
{
ListNode* newnode=new ListNode(val);
newnode->next=vir_node->next;
vir_node->next=newnode;
size++;
}
void addAttail(int val)
{
ListNode* new_node=new ListNode(val);
ListNode* cur=vir_node;
while (cur->next!=nullptr)
{
cur=cur->next;
}
cur->next=new_node;
size++;
}
void addAtindex(int index,int val)
{
if(index>size)
{
return;
}
ListNode* new_node=new ListNode(val);
ListNode* cur=vir_node;
while (index--)
{
cur=cur->next;
}
new_node->next=cur->next;
cur->next=new_node;
size++;
}
void deleteAtval(int val)
{
ListNode* cur=vir_node;
while (cur->next!=NULL)
{
if(cur->next->val==val)
{
ListNode* tmp=cur->next;
cur->next=cur->next->next;
delete tmp;
size--;
}
cur=cur->next;
}
}
void deleteAtindex(int index)
{
if(index>=size|| index<0)
return;
ListNode*cur=vir_node;
while (index--)
{
cur=cur->next;
}
ListNode*tmp =cur->next;
cur->next=cur->next->next;
delete tmp;
size--;
}
void printLinklist()
{
ListNode* cur=vir_node;
while (cur->next!=nullptr)
{
cout<<cur->next->val<<' ';
cur=cur->next;
}
}
//循环方式翻转链表
void reverse_listnode()
{
ListNode* head=vir_node->next;
ListNode* cur=head;
ListNode*tmp;
ListNode*pre=NULL;
while (cur)
{
tmp=cur->next;
cur->next=pre;
pre=cur;
cur=tmp;
}
cout<<"翻转后: "<<endl;
while(pre!=nullptr)
{
cout<<pre->val<<' ';
pre=pre->next;
}
}
//递归方式翻转链表
/*ListNode* reverse_listnode_2(ListNode* pre,ListNode* cur){
if(cur == NULL) return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse_listnode_2(cur,temp);
}
void reverseList(ListNode* head)
{
reverse_listnode_2(NULL, head);
}*/
private:
int size;
ListNode* vir_node;
};
int main()
{
Mylink_list mylist;
mylist.addAthead(1);
mylist.addAttail(3);
mylist.addAtindex(1,2);
mylist.get(1);
mylist.deleteAtindex(1);
mylist.get(1);
mylist.printLinklist();
cout<<endl;
mylist.reverse_listnode();
cout<<endl;
return 0;
}
结果:
1 3
翻转后:
3 1