#include<stdio.h>
#include<iostream>
#include<malloc.h>
#define NULL 0
typedef struct node
{
int data;
struct node*prio;
struct node*next;
}ElemSN;
//创建双向链表
ElemSN*Creatlink(int a[],int n)
{
ElemSN*h, *p, *q; int i ;
p=h = (ElemSN*)malloc(sizeof(ElemSN));
h->data = a[0];
h->prio = NULL;
for (i = 1; i < n; i++)
{
q = (ElemSN*)malloc(sizeof(ElemSN));
q->data = a[i];
p->next = q;
q->prio = p;
q->next = NULL;
p = q;
}
return h;
}
//删除链表中的结点
ElemSN*Delnode(ElemSN*h,int x)
{
ElemSN*p, *q; q = h;
for (p = h; p&&p->data - x;q = p,p = p->next);
{
if (p - h&&p->next!=NULL)
{
q->next = p->next;
q = p->next->prio;
free(p);
}
else if (p->next == NULL)
{
q->next = NULL;
free(p);
}
else
{
h = h->next;
h->prio = NULL;
free(p);
}
}
return h;
}
//插入结点
ElemSN*InsertNode(ElemSN*h, ElemSN*s)
{
ElemSN*p, *q; q =h ;
for (p = h; p && (p->data < s->data); q = p, p = p->next);
if (p == h)
{
s->next = h;
s->prio = NULL;
h = s;
}
else if (!p)
{
s->next=q->next;
q->next = s;
s->prio = q;
}
else
{
s->next = q->next;
q->next = s;
s->prio = q;
p->prio = s;
}
return h;
}
//打印链表
void Printlink(ElemSN*h)
{
ElemSN*p;
for (p = h; p; p = p->next)
{
printf("%d", p->data);
}
system("pause");
}
void main()
{
int a[5] = { 1,3,5,7,9 };
//int x;
ElemSN*head,*h,*s;
head = Creatlink(a,5);
Printlink(head);
//printf("请输入要删除节点的值\n");
//std::cin >> x;
//head=Delnode(head, x);
//Printlink(head);
s = (ElemSN*)malloc(sizeof(ElemSN));
printf("请输入插入节点的值:\n");
std::cin >> s->data;
s->prio = NULL; s->next = NULL;
h=InsertNode(head, s);
Printlink(h);
getchar();
}
#include<iostream>
#include<malloc.h>
#define NULL 0
typedef struct node
{
int data;
struct node*prio;
struct node*next;
}ElemSN;
//创建双向链表
ElemSN*Creatlink(int a[],int n)
{
ElemSN*h, *p, *q; int i ;
p=h = (ElemSN*)malloc(sizeof(ElemSN));
h->data = a[0];
h->prio = NULL;
for (i = 1; i < n; i++)
{
q = (ElemSN*)malloc(sizeof(ElemSN));
q->data = a[i];
p->next = q;
q->prio = p;
q->next = NULL;
p = q;
}
return h;
}
//删除链表中的结点
ElemSN*Delnode(ElemSN*h,int x)
{
ElemSN*p, *q; q = h;
for (p = h; p&&p->data - x;q = p,p = p->next);
{
if (p - h&&p->next!=NULL)
{
q->next = p->next;
q = p->next->prio;
free(p);
}
else if (p->next == NULL)
{
q->next = NULL;
free(p);
}
else
{
h = h->next;
h->prio = NULL;
free(p);
}
}
return h;
}
//插入结点
ElemSN*InsertNode(ElemSN*h, ElemSN*s)
{
ElemSN*p, *q; q =h ;
for (p = h; p && (p->data < s->data); q = p, p = p->next);
if (p == h)
{
s->next = h;
s->prio = NULL;
h = s;
}
else if (!p)
{
s->next=q->next;
q->next = s;
s->prio = q;
}
else
{
s->next = q->next;
q->next = s;
s->prio = q;
p->prio = s;
}
return h;
}
//打印链表
void Printlink(ElemSN*h)
{
ElemSN*p;
for (p = h; p; p = p->next)
{
printf("%d", p->data);
}
system("pause");
}
void main()
{
int a[5] = { 1,3,5,7,9 };
//int x;
ElemSN*head,*h,*s;
head = Creatlink(a,5);
Printlink(head);
//printf("请输入要删除节点的值\n");
//std::cin >> x;
//head=Delnode(head, x);
//Printlink(head);
s = (ElemSN*)malloc(sizeof(ElemSN));
printf("请输入插入节点的值:\n");
std::cin >> s->data;
s->prio = NULL; s->next = NULL;
h=InsertNode(head, s);
Printlink(h);
getchar();
}