#include using namespace std;

struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };

ListNode* createList() { int n, val; ListNode *head, *p; head = new ListNode(-1); p = head;

cout'请输入链表长度:'<<endl;
cin>>n;

for(int i = 0; i < n; i++)
{
    cout'请输入第'<<i+1'个节点的值:'<<endl;
    cin>>val;

    ListNode *node = new ListNode(val);
    p->next = node;
    p = p->next;
}

return head->next;

}

void printList(ListNode* head) { ListNode *p = head; while(p != NULL) { cout<val' '; p = p->next; } }

int main() { ListNode *head = createList(); cout'链表的值为:'; printList(head); return 0; }

C++ 链表构建和显示程序 - 代码示例

原文地址: https://www.cveoy.top/t/topic/mI6J 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录