</pre><p>单向链表</p><p></p><p><pre class="cpp" name="code" snippet_file_name="blog_20150327_2_6786675" code_snippet_id="629209">#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct list
{
int num;
list *next1;
}listtest;
int _tmain(int argc, _TCHAR* argv[])
{
listtest* head = NULL;
listtest* p = new listtest();
listtest* q = new listtest();
cin >> p->num;
while (p->num != 0)
{
if (head == NULL)
{
head = q = p;
p->next1 = NULL;
p = new listtest();
cin >> p->num;
}
else
{
q->next1 = p;
q = p;
p->next1 = NULL;
p = new listtest();
cin >> p->num;
}
}
while (head)
{
cout << head->num << endl;
head = head->next1;
}
return 0;
}
双向链表
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef struct list
{
int num;
list *next1;
list *prev;
}listtest;
int _tmain(int argc, _TCHAR* argv[])
{
listtest* head = NULL;
listtest* p = new listtest();
listtest* q = new listtest();
cin >> p->num;
while (p->num != 0)
{
if (head == NULL)
{
head = q = p;
p->next1 = NULL;
p->prev = NULL;
p = new listtest();
cin >> p->num;
}
else
{
q->next1 = p;
p->prev = q;
q = p;
p->next1 = NULL;
p = new listtest();
cin >> p->num;
}
}
while (head)
{
cout << head->num << endl;
head = head->next1;
}
return 0;
}