C++实现单向链表
来源:互联网 发布:股票app制作软件 编辑:程序博客网 时间:2024/06/05 21:19
#include <iostream>
using namespace std;
int counter;
struct Node{
Node* next;
int data;
bool hasNext;
};
void addNode(Node* head,Node* node){
node->next=head->next;
head->next=node;
counter+=1;
}
void showListData(Node* head){
head=head->next;//ignore the head node
while(true){
cout<<head->data<<"\t";
head=head->next;
if(head->next==NULL){
cout<<head->data<<endl;
break;
}
}
cout<<endl;
}
void showListForLoop(Node* head){
for(int i=0;i<=counter;i++){
cout<<head->data<<"\t";
head=head->next;
}
}
void deleteNode(Node* head,int index){
if(index==1){
head->next;
}
Node* p = head;
for(int i = 1;i<index;i++){
p=p->next;
}
Node* node = p->next;
p->next = p->next->next;
delete(node);
}
void main(){
Node* head = new Node;
head->next=NULL;
head->data=100;
for(int i=1;i<=10;i++){
Node* node = new Node;
node->data=i;
addNode(head,node);
}
cout<<"counter:"<<counter<<endl;
showListData(head);
deleteNode(head,5);
showListData(head);
// showListForLoop(head);
}
using namespace std;
int counter;
struct Node{
Node* next;
int data;
bool hasNext;
};
void addNode(Node* head,Node* node){
node->next=head->next;
head->next=node;
counter+=1;
}
void showListData(Node* head){
head=head->next;//ignore the head node
while(true){
cout<<head->data<<"\t";
head=head->next;
if(head->next==NULL){
cout<<head->data<<endl;
break;
}
}
cout<<endl;
}
void showListForLoop(Node* head){
for(int i=0;i<=counter;i++){
cout<<head->data<<"\t";
head=head->next;
}
}
void deleteNode(Node* head,int index){
if(index==1){
head->next;
}
Node* p = head;
for(int i = 1;i<index;i++){
p=p->next;
}
Node* node = p->next;
p->next = p->next->next;
delete(node);
}
void main(){
Node* head = new Node;
head->next=NULL;
head->data=100;
for(int i=1;i<=10;i++){
Node* node = new Node;
node->data=i;
addNode(head,node);
}
cout<<"counter:"<<counter<<endl;
showListData(head);
deleteNode(head,5);
showListData(head);
// showListForLoop(head);
}
- C++:单向链表实现
- C语言单向链表的实现
- C语言实现的单向链表
- 单向链表C语言实现
- 单向链表的C语言实现
- c实现逆转单向循环链表
- 自己实现C语言单向链表
- C++:操作单向链表的实现
- C语言单向链表的实现
- C语言实现单向链表
- 数据结构--单向链表C实现
- 单向循环链表C语言实现
- c语言单向链表的实现
- C语言基础 - 实现单向链表
- C语言单向链表的实现
- C语言实现 单向链表
- C语言实现单向链表
- C语言单向链表的实现
- Spring配置项<context:annotation-config/>解释说明
- HTML基础(DTD & 注释 &常见HTML编码)
- 初学ajax的一个小例子
- 在CSDN的博客终于开了,慢慢更新
- Linux Unicode 编程
- C++实现单向链表
- 格言(二)——其他
- JAVA中MESSAGEBOX,静态类直接引用
- 国家气象局免费天气预报接口,城市代码(JSON格式)
- OpenWrt中的FEATURES:=broken jffs2
- TTY终端的输入过程
- android4.0.3 修改启动动画和开机声音
- UVa 673 - Parentheses Balance
- python中文乱码问题