#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node*right;
struct Node*left;
};
struct Node*createList()
{
struct Node*headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->right = headNode->left = headNode;
return headNode;
}
struct Node*createNode(int data)
{
struct Node*newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->left = newNode->right = NULL;
newNode->data=data;
return newNode;
}
void insertNodeByTail(struct Node*headNode, int data)
{
struct Node* newNode = createNode(data);
struct Node* tailNode = headNode->left;
tailNode->right = newNode;
newNode->left = tailNode;
newNode->right = headNode;
headNode->left = newNode;
}
void insertNodeByAppoin(struct Node*headNode,int data,int posData)
{
if(headNode->left == headNode->right)
{
printf("链表为空,无法指定位置插入");
return;
}
else
{
struct Node*posNode = headNode->right;
struct Node*posNodeLeft = headNode;
while(posNode->data != posData)
{
posNodeLeft = posNode;
posNode=posNodeLeft->right;
if(posNode == headNode)
{
printf("没有找到指定位置,无法插入!");
}
}
struct Node*newNode = createNode(data);
posNodeLeft->right = newNode;
newNode->left = posNodeLeft;
posNode->left = newNode;
newNode->right = posNode;
}
}
void printListRight(struct Node* headNode)
{
if(headNode->left == headNode->right)
{
printf("表头为空,无法打印!");
return;
}
struct Node*pMove = headNode->right;
while(pMove != headNode)
{
printf("%d\t",pMove->data);
pMove=pMove->right;
}
printf("\n");
}
void printListLeft(struct Node*headNode)
{
if(headNode->left ==headNode->right)
{
printf("表头为空,无法打印!");
return;
}
struct Node*pMove = headNode->left;
while(pMove!=headNode)
{
printf("%d\t",pMove->data);
pMove=pMove->left;
}
printf("\n");
}
int main()
{
struct Node*list = createList();
insertNodeByTail(list,3);
insertNodeByTail(list,2);
insertNodeByTail(list,1);
printListRinght(list);
printListLeft(list);
system("pause");
return 0;
}