用C++实现双循环链表,建造一个空表,插入第i个结点,删除第i个结点,插入第一个结点,插入最后一个结点 15
1个回答
展开全部
初步写了一个,供参考:
struct LINK
{
DATA data;
LINK *prev;
LINK *next;
};
LINK *head = NULL;
void create(int n)
{
LINK *p = NULL, *q = NULL;
for(int i=0; i<n; i++)
{
p = new LINK;
if(q)
{
q->next = p;
p->prev = q;
}
if(head == NULL)
{
head = p;
}
}
head->prev = p;
p->next = head;
}
void insert(int i, LINK *node)
{
LINK *p = head;
LINK *q;
for(int j = 0; j < i; j++)
{
p = p->next;
}
q = p->next;
p->next = node;
node->prev = p;
node->next = q;
q->prev = node;
}
void del(int i)
{
LINK *p = head;
LINK *q, *t;
for(int j = 0; j < i; j++)
{
p = p->next;
}
q = p->prev;
t = p->next;
delete p;
q->next = t;
t->prev = q;
}
void push_front(LINK *node)
{
LINK *t = head->prev;
node->next = head;
node->prev = t;
head->prev = node;
t->next = node;
head = node;
}
void push_back(LINK *node)
{
LINK *t = head->prev;
node->prev = t;
node->next = head;
t->next = node;
head->prev = node;
}
struct LINK
{
DATA data;
LINK *prev;
LINK *next;
};
LINK *head = NULL;
void create(int n)
{
LINK *p = NULL, *q = NULL;
for(int i=0; i<n; i++)
{
p = new LINK;
if(q)
{
q->next = p;
p->prev = q;
}
if(head == NULL)
{
head = p;
}
}
head->prev = p;
p->next = head;
}
void insert(int i, LINK *node)
{
LINK *p = head;
LINK *q;
for(int j = 0; j < i; j++)
{
p = p->next;
}
q = p->next;
p->next = node;
node->prev = p;
node->next = q;
q->prev = node;
}
void del(int i)
{
LINK *p = head;
LINK *q, *t;
for(int j = 0; j < i; j++)
{
p = p->next;
}
q = p->prev;
t = p->next;
delete p;
q->next = t;
t->prev = q;
}
void push_front(LINK *node)
{
LINK *t = head->prev;
node->next = head;
node->prev = t;
head->prev = node;
t->next = node;
head = node;
}
void push_back(LINK *node)
{
LINK *t = head->prev;
node->prev = t;
node->next = head;
t->next = node;
head->prev = node;
}
更多追问追答
追问
着不是C++写的,还能帮我改成C++版吗
追答
这是C++写的好不
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询