双向链表
#include
#include
typedef struct node_t
{
int val;
struct node_t* front;
struct node_t* back;
}node;
node* creatdlist(node* head)
{
int type_len;
node* p=head;
printf("输入链表长长度是:");
scanf("%d",&type_len);
head->front=NULL;
while(type_len--)
{
int type_val;
node* r=(node*)malloc(sizeof(node));
scanf("%d",&type_val);
r->val=type_val;
p->back=r;
r->front=p;
p=r;
}
p->back=NULL;
return p;
}
void printflist_b(node* head)
{ int a=10;
node* p=head;
p=p->back;
while(p!=NULL)
{
printf("%d ",p->val);
p=p->back;
}
}
void printflist_f(node* head)
{ int a=10;
node* p=head;
do
{
printf("%d ",p->val);
p=p->front;
} while(p->front!=NULL);
}
void delnode(node* head,int num)
{
node* p=head;
node* p_free;
do
{
if(p->val==num)
{
p->front->back=p->back;
p->back->front=p->front;
p_f=p;
}
p=p->back;
} while(p!=NULL);
free(p_free);
}
void insertnode(node* head,int num,int val)
{
node* p=head;
int cnt=0;
do
{
cnt++;
if(cnt==num)
{
node* q=p->back;
node* r=(node*)malloc(sizeof(node));
r->val=val;
r->front=p;
r->back=p->back;
p->back=r;
q->front=r;
}
p=p->back;
} while(p!=NULL);
}
int main(void)
{
node* head=(node*)malloc(sizeof(node));
node* end=creatdlist(head);
// delnode(head,100);//删除值为100的节点
insertnode(head,5,999);
printflist_b(head);
printf("逆序再输出\n");
printflist_f(end);
}