📄 strlink.cpp
字号:
#include <iostream>#include <string>using namespace std;#include "tvector.h"// compare linked list construction to vector constructionstruct Node{ string info; Node * next; Node(const string& s, Node * link) : info(s), next(link) { }};void Print(Node * list);void Print(const tvector<string> & list);int main(){ Node * first=0; // initially no nodes in list Node * temp=0; // initialize to 0 for defensive programming int k; tvector<string> vec; string storage[] = {"I", "learn", "to", "code"}; for(k=0; k < 4; k++) { vec.push_back(storage[k]); temp = new Node(storage[k],first); // new node before first first = temp; // make first point at new node } cout << "vector:\t\t"; Print(vec); cout << "linked list:\t"; Print(first); return 0;}void Print(Node * list)// pre: list is 0-terminated (last node's next field is 0)// post: all info fields of list printed on one line{ Node * temp; for(temp = list; temp != 0; temp = temp->next) { cout << temp->info << " "; } cout << endl;}void Print(const tvector<string> & list)// pre: list contains list.size() entries// post: all elements printed on one line{ int k; for(k=0; k < list.size(); k++) { cout << list[k] << " "; } cout << endl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -