#include <stdio.h>
#include <stdlib.h>
/*双向链表,在表头插入数据*/
typedef struct node
{
int key;
struct node *Next;
struct node *Pre;
}Node;
void Insert(int num,Node *head)//插入新的节点
{
Node *p = NULL;
p = (Node *) malloc (sizeof(Node));//开辟新的节点
p->key = num;//赋值
if(head->Next==NULL)//如果是空链表
{
p->Next=NULL;p->Pre=NULL;head->Next=p;//将原来的头结点挂在新节点后,并把新节点挂在表头上
}
else //非空链表,则把旧元素的pre指向p,并将p的next指向原来的头元素
{
head->Next->Pre=p;
p->Next=head->Next;
head->Next=p;
}
}
void Print_List(Node *head)
{
Node *p = head->Next;
while(p != NULL)//遍历打印节点值
{
printf("%d->", p->key);
p = p->Next;
}
printf("\n");
}
Node *Search_List(Node *head,int num)//寻找值为num的节点,并返回位置指针
{
Node *p=NULL; p=head;
while((p->Next!=NULL)&&p->
数据结构之双向链表,头部插入数据
最新推荐文章于 2023-09-18 17:58:50 发布