33,008
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <iostream>
using namespace std;
class info
{
public :
int h;
char status;
info *next_pos;
info *next_h;
info *prior_pos;
info *prior_h;
};
int main(int argc, char *argv[])
{
map<int, info *> m;
FILE *fp;
int i, j, n;
info *inf, *tmp, *h, *pos;
if ((fp = fopen(argv[1], "r")) == NULL) {
return -1;
}
tmp = NULL;
while (!feof(fp) && fscanf(fp, "%d ", &n)) {
inf = new info();
inf->h = n;
inf->prior_pos = tmp;
inf->next_pos = NULL;
inf->prior_h = inf->next_h = NULL;
m.insert(map<int, info *>::value_type(n, inf));
if (tmp) {
tmp->next_pos = inf;
} else {
pos = inf; //保存头节点
}
tmp = inf;
}
fclose(fp);
if (tmp) tmp->next_pos = NULL;
tmp = NULL;
for (map<int, info *>::iterator it = m.begin(); it != m.end(); ++it) {
it->second->next_h = NULL;
it->second->prior_h = tmp;
if (tmp) { tmp->next_h = it->second; }
else h = it->second; //保存头节点
tmp = it->second;
}
tmp->next_h = NULL;
#ifdef _DEBUG_
tmp = h;
while (tmp)
{
printf("%d->", tmp->h);
tmp = tmp->next_h;
}
tmp = pos;
printf("\n");
while (tmp)
{
printf("%d->", tmp->h);
tmp = tmp->next_pos;
}
#endif
return 0;
}
#include <iostream>
using namespace std;
class info
{
public :
int h;
char status;
info *next_pos;
info *next_h;
info *prior_pos;
info *prior_h;
info(int val = 0)
{
h = val;
next_pos = next_h = prior_pos = prior_h = NULL;
}
};
//info t[1000]; //构造链表的话,这样的声明就没有意义了
void append(info* head, info**pre, int val)
{
info* n = new info(val);
//根据输入顺序建立链表
(*pre)->next_pos = n;
n->prior_pos = *pre;
//插入排序
info *t = head;
while (t->next_h != NULL)
{
if (t->next_h->h >= val)
{
break;
}
t = t->next_h;
}
n->next_h = t->next_h;
if (t->next_h)
{
t->next_h->prior_h = n;
}
t->next_h = n;
n->prior_h = t;
*pre = n;
}
int init(info **head)
{
*head = new info();
return 0;
}
int destory(info *head)
{
info *t = head->next_pos;
info *del;
while (t)
{
del = t;
t = t->next_pos;
delete []del;
}
head->next_pos = NULL;
return 0;
}
void showList(info *head)
{
//顺序输出
info* t = head;
while (t->next_h)
{
cout << t->next_h->h << ' ';
t = t->next_h;
} cout << endl;
//逆序输出
while (t != head)
{
cout << t->h << ' ';
t = t->prior_h;
} cout << endl;
}
int main()
{
int n;
int val;
info *phead = NULL; //phead是表头,不用于存储数据
info *pins = NULL;
init(&phead);
pins = phead;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> val;
append(phead, &pins, val);
}
showList(phead);
destory(phead);
system("pause");
return 0;
}