描述
就是C++盲写一个简配版link,时间紧急,只写了creatL()和打印函数printL();
前一个是不断在头部插入节点(节点只有一个整型数据),打印是依次输出所有节点的id;
非常简单,有时间可以扩展为更多,如:
- 根据输入,增加节点
- 根据输入,删除节点
- 统计长度
- 查找链表的元素值
- 改变链表序列(如根据id排序,由于没有检查过元素,所以可以重复哦)
代码
#include<iostream>
using namespace std;
struct Node {
int id;
Node *next;
};
Node* createL(Node *h, int id){
Node *p = new Node;//插入头部
p->id = id;
p->next = h;
return p;
}
void printL(Node *h){
while(h!=NULL){
cout<<"This node has val of "<< h->id<< endl;
h = h->next;
}
}
int main(){
Node *h = new Node;
h->id = 1;
h->next = NULL;
h = createL(h, 2);
h = createL(h, 3);
h = createL(h, 4);
printL(h);
return 0;
}

1623

被折叠的 条评论
为什么被折叠?



