Realization of double linked list of linear structure

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.