雙向鏈表的操作問題

建立一個長度爲n的帶頭結點的雙向鏈表,使得該鏈表中的數據元素遞增有序排列。(必須使用雙向鏈表完成,數據類型爲整型。)

輸入

第一行:雙向表的長度; 
第二行:鏈表中的數據元素。

輸出

輸出雙向鏈表中的數據元素的值。

樣例輸入

10
2 4 6 3 5 8 10 21 12 9

樣例輸出

2 3 4 5 6 8 9 10 12 21
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>
using namespace std;
struct node
{
	int date;
	node *last;
	node *next;
};
int main()
{
	int n;
	while (cin >> n)
	{
		node *p = new node;
		node *s, *q, *t;
		cin >> p->date;
		p->next = NULL;
		s = p;
		for(int i=2;i<=n;i++)
		{
			t = new node;
			cin >> t->date;
			t->next = NULL;
			s->next = t;
			t->last = s;
			s = t;
		}
		for (int i = 1; i <= n; i++)
		{
			t = p;
			while (t != NULL)
			{
				if (t->next != NULL)
				{
					if (t->date > t->next->date)
					{
						int temp = t->date;
						t->date = t->next->date;
						t->next->date = temp;
					}
				}
				t = t->next;
			}
		}
		t = p;
		while (t != NULL)
		{
			cout << t->date << ' ';
			t = t->next;
		}
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章