Double-chain table in C Language

Source: Internet
Author: User

[Cpp]
# Include <assert. h>
# Include <stdlib. h>
# Include <stdio. h>


Typedef struct myList DNode;
Struct myList
{
DNode * next;
DNode * prev;
Int data;
};
DNode * init (int data );
Void insertLastNode (DNode * L, int data );
Void insertNode (DNode * L, int data );
DNode * findPrev (DNode * L, int data );
Void deleNode (DNode * L, int data );
DNode * findCur (DNode * L, int data );

/*************************************** *********************************/
/* Initialize the first node */
/*************************************** *********************************/
DNode * init (int data)
{
DNode * headNode = (DNode *) malloc (sizeof (DNode ));
Assert (headNode );
HeadNode-> next = NULL;
HeadNode-> prev = NULL;
HeadNode-> data = data;
Return headNode;
}

Int isEmpty (DNode * L)
{
Return L-> next = NULL;
}

Int isLast (DNode * L, DNode * p)
{
Return p-> next = NULL;
}

DNode * findCur (DNode * L, int data)
{
DNode * curNode;

CurNode = L-> next;

While (curNode! = NULL & curNode-> data! = Data)
CurNode = curNode-> next;

Return curNode;
}
/*************************************** *********************************/
/* Insert a node to the backend of the queue */
/*************************************** *********************************/
Void insertLastNode (DNode * L, int data)
{
Assert (L );
DNode * oNode = (DNode *) malloc (sizeof (DNode ));
ONode-> data = data;
L-> next = oNode;
ONode-> prev = L;
}
/**
Insert to the specified position of the linked list
**/
Void insertNode (DNode * L, int data)
{
Assert (L );
DNode * qNode = (DNode *) malloc (sizeof (DNode ));
DNode * prevNode = findPrev (L, data );



QNode-> next = prevNode-> next;
PrevNode-> next-> prev = qNode;

PrevNode-> next = qNode;
QNode-> prev = prevNode;
Return;
}

DNode * findPrev (DNode * L, int data)
{
Assert (L );
DNode * tmpNode;
TmpNode = L;

While (tmpNode-> next! = NULL & tmpNode-> next-> data! = Data)
{
TmpNode = tmpNode-> next;
}
Return tmpNode;
}

Void deleNode (DNode * L, int data)
{
DNode * tmpNode = findCur (L, data );
If (tmpNode = NULL)
{
Return;
}
If (isLast (L, tmpNode ))
{
TmpNode-> prev = NULL;
TmpNode-> prev-> next = NULL;
Free (tmpNode );
}

TmpNode-> prev-> next = tmpNode-> next;
TmpNode-> next-> prev = tmpNode-> prev;
Free (tmpNode );
Return;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
DNode * myDNode;
MyDNode = init (1 );
Return 0;
}

# Include <assert. h>
# Include <stdlib. h>
# Include <stdio. h>
 
 
Typedef struct myList DNode;
Struct myList
{
DNode * next;
DNode * prev;
Int data;
};
DNode * init (int data );
Void insertLastNode (DNode * L, int data );
Void insertNode (DNode * L, int data );
DNode * findPrev (DNode * L, int data );
Void deleNode (DNode * L, int data );
DNode * findCur (DNode * L, int data );
 
/*************************************** *********************************/
/* Initialize the first node */
/*************************************** *********************************/
DNode * init (int data)
{
DNode * headNode = (DNode *) malloc (sizeof (DNode ));
Assert (headNode );
HeadNode-> next = NULL;
HeadNode-> prev = NULL;
HeadNode-> data = data;
Return headNode;
}
 
Int isEmpty (DNode * L)
{
Return L-> next = NULL;
}
 
Int isLast (DNode * L, DNode * p)
{
Return p-> next = NULL;
}
 
DNode * findCur (DNode * L, int data)
{
DNode * curNode;
 
CurNode = L-> next;
 
While (curNode! = NULL & curNode-> data! = Data)
CurNode = curNode-> next;
 
Return curNode;
}
/*************************************** *********************************/
/* Insert a node to the backend of the queue */
/*************************************** *********************************/
Void insertLastNode (DNode * L, int data)
{
Assert (L );
DNode * oNode = (DNode *) malloc (sizeof (DNode ));
ONode-> data = data;
L-> next = oNode;
ONode-> prev = L;
}
/**
Insert to the specified position of the linked list
**/
Void insertNode (DNode * L, int data)
{
Assert (L );
DNode * qNode = (DNode *) malloc (sizeof (DNode ));
DNode * prevNode = findPrev (L, data );
 
 

QNode-> next = prevNode-> next;
PrevNode-> next-> prev = qNode;

PrevNode-> next = qNode;
QNode-> prev = prevNode;
Return;
}
 
DNode * findPrev (DNode * L, int data)
{
Assert (L );
DNode * tmpNode;
TmpNode = L;
 
While (tmpNode-> next! = NULL & tmpNode-> next-> data! = Data)
{
TmpNode = tmpNode-> next;
}
Return tmpNode;
}
 
Void deleNode (DNode * L, int data)
{
DNode * tmpNode = findCur (L, data );
If (tmpNode = NULL)
{
Return;
}
If (isLast (L, tmpNode ))
{
TmpNode-> prev = NULL;
TmpNode-> prev-> next = NULL;
Free (tmpNode );
}
 
TmpNode-> prev-> next = tmpNode-> next;
TmpNode-> next-> prev = tmpNode-> prev;
Free (tmpNode );
Return;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
DNode * myDNode;
MyDNode = init (1 );
Return 0;
}


 

Related Article

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.