#include <iostream>
using namespace std;
typedef struct Node{
int val;
Node *next; //或者写struct Node *next;
}Node;
Node head;
void init() //初始化head
{
head.val = -1;
head.next = NULL;
}
void add_to_head(Node &node)
{
node.next = head.next;
head.next = &node;
}
void create(Node &node,int val)
{
node.val = val;
node.next = NULL;
}
void add(Node &before,Node &node)
{
if(before.next != NULL)node.next = before.next;
before.next = &node;
}
int main()
{
Node node0,node1,node2;
init();
create(node0, 100);
create(node1, 162);
create(node2, 111);
add_to_head(node0);
add(node0,node1);
add(node1,node2);
//cout << head.val;
for(auto it = head;it.next != NULL;it = *(it.next))
{
cout << (*it.next).val << " ";
}
//cout << node[0].val;
return 0;
}
自己实现的单链表
最新推荐文章于 2024-07-19 15:45:46 发布