C++ linked list

来源:互联网 发布:青果软件学院 编辑:程序博客网 时间:2024/06/03 15:45
//C++: linked list#include <iostream>#include <cstdlib> // for system("PAUSE")#include <iomanip>using namespace std;struct nodeType {   int info;   nodeType* link;}; // 不要忘了分号nodeType* buidListForward();nodeType* buildListBackward();int main() {   nodeType* head;   nodeType* current;   head = buildListBackward();   current = head;   cout << "list = ";   int count = 0;   while(current != NULL) {       cout << setw(5) << current -> info;       count++;       if(count % 10 == 0)          cout << endl;       current = current -> link;   }   system("PAUSE");   return 0;}//build a linked list forwardnodeType* buildListForward() {   nodeType *first, *last, *newNode;   int num;   cout << "Enter a list of integers ending with -999"        << endl;   cin >> num;   first = NULL;   while (num != -999) {      newNode = new nodeType;      newNode -> info = num;      newNode -> link = NULL;   //insert the new node   if(first == NULL) {      first = newNode;      last = newNode;   }   else {      newNode -> link = first;      first = newNode;   }   cin >> num;   }   return first;}//build a list backwardnodeType* buildListBackward() {   nodeType *first, *newNode;   int num;   cout << "Enter a list of integers ending with -999"        << endl;   cin >> num;   first = NULL;   while (num != -999) {      newNode = new nodeType;      newNode -> info = num;      newNode -> link = first;      first = newNode;      cin >> num;   }   return first;}


运行结果如下:

0 0
原创粉丝点击