MOOC清华《程序设计基础》第8章:链表的基本操作

来源:互联网 发布:java定时器配置 编辑:程序博客网 时间:2024/06/07 02:36

一、链表的创建过程:

Node* head = NULL;while(...){Node* data = new Node;//读入一行数据...//添加至链表... data->next = head;head = data;}

二A、遍历链表结点数据的方法:(while循环版本)

int cnt = 0;while(head){cout << cnt << ' ' << head->id << endl;cnt++;head = head->next;}

二B、遍历链表结点数据的方法:(for循环版本)

int cnt = 0;for(Node* p = head; p != NULL; p = p->next){cout << cnt << ' ' << p->id << endl;cnt++;}

三、释放链表的方法:

while(head){Node* tmp = head;head = head->next;delete tmp;}

阅读全文
0 0
原创粉丝点击