#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct NODE
{
int data;
struct node * prior;
struct node * NEXT;
}node,*pnode;
Pnode createlist (Pnode);
void Travellist (Pnode);
void Insertlist (Pnode,int,int);
int lengthlist (pnode);
void Dellist (Pnode,int,int *);
void Main ()
{
The head node needs to be created, and he's allocated space.
Pnode phead= (pnode) malloc (sizeof (NODE));
if (Null==phead)
{
printf ("Memory allocation failed");
Exit (-1);
}
phead->next=null;
phead->prior=null;
Create a doubly linked list
Phead=createlist (Phead);
Traversing a doubly linked list
Travellist (Phead);
printf ("\ n");
Insertion of linked lists
Insertlist (phead,2,3);
Traversing a doubly linked list
Travellist (Phead);
printf ("\ n");
printf ("Start preparing to delete node \ n");
int Val;
Linked List node deletion
Dellist (Phead,2,&val);
Traversing a doubly linked list
Travellist (Phead);
printf ("\ n");
}
Pnode createlist (Pnode phead)
{
int Len;
int Val;
Pnode P=phead;
printf ("Please enter the number of nodes to initialize:");
scanf ("%d", &len);
for (int i=0;i<len;i++)
{
Pnode pnew= (pnode) malloc (sizeof (NODE));
if (pnew==null)
{
printf ("Memory allocation failed");
Exit (-1);
}
printf ("Please enter an assignment for the current%d node", i+1);
scanf ("%d", &val);
pnew->data=val;
pnew->prior=null;
pnew->next=null;
pnew->next=p->next;
pnew->prior=p;
p->next=pnew;
P=pnew;
}
return phead;
}
void Dellist (Pnode phead,int pos,int *val)
{
int i=0;
Pnode P=phead;
if (p==null| | pos<1| | Pos>lengthlist (Phead))
{
printf ("Cannot delete, delete illegal");
Return
}
while (I<POS-1)
{
p=p->next;
i++;
}
Pnode q=p->next;
*val=q->data;
printf ("%d", *val);
p->next=p->next->next;
p->next->prior=p;
Free (q);
}
void Travellist (Pnode phead)
{
Pnode pnew=phead->next;
while (Pnew!=null)
{
printf ("%d", pnew->data);
pnew=pnew->next;
}
}
void Insertlist (Pnode phead,int pos,int val)
{
Pnode P=phead;
int i=0;
if (p==null| | pos<1| | Pos>lengthlist (Phead))
{
printf ("Unable to insert node, illegal");
Exit (-1);
}
while (I<POS-1)
{
p=p->next;
i++;
}
Initializing nodes
Pnode pnew= (pnode) malloc (sizeof (NODE));
pnew->data=val;
pnew->next=null;
pnew->prior=null;
Prepare to insert the node.
pnew->next=p->next;
p->next->prior=pnew;
pnew->prior=p;
p->next=pnew;
}
int lengthlist (Pnode phead)
{
Pnode Pnew=phead;
int i=0;
while (Pnew!=null)
{
pnew=pnew->next;
i++;
}
return i;
}
This article from the "Jane Answers Life" blog, reproduced please contact the author!
Double-linked list implementation of linear structure