/*双向非循环链表、无表头*/
#include<stdio.h>
#include<stdlib.h>
typedef struct NODE{
struct NODE *prev; //指向前一个结点
struct NODE *next; //指向后一个结点
int value; //当前结点值
}Node;
void InitList(Node *Plist)
{
Plist = NULL;
printf("InitList is succed!\n");
}
Node *CreateList(Node *Plist)
{
printf("CreateList start!\n");
Node *Pinsert;//要插入的结点
Node *Plast;//指向最后一个结点
Pinsert = (Node *)malloc(sizeof(Node));
if(Pinsert == NULL)
{
return NULL;
}
Plast = Plist;
printf("Please input the value you want to insert:");
scanf("%d",&Pinsert->value);
if(Pinsert->value < 0)
{
printf("the value is illegal\n");
return NULL;
}
while(Pinsert->value > 0)
{
if(Plist == NULL)
{
Plist = Pinsert;
Plast = Plist;
}
else
{
Plast->next = Pinsert;
Pinsert->prev = Plast;
Plast = Plast->next; //Plast移到下一个结点
}
//继续插入新的结点
Pinsert = (Node *)malloc(sizeof(Node));
if(Pinsert == NULL)
{
return NULL;
}
scanf("%d",&Pinsert->value);
}
printf("CreateList end!\n");
return Plist;
}
//在链表尾部插入新结点
int InsertList(Node *Plist,int item)
{
Node *Newnode;
Node *Curnode;
Newnode = (Node *)malloc(sizeof(Node));
if(Newnode == NULL)
{
return -1;
}
Curnode = Plist;
Newnode->value = item;
if(Curnode == NULL) //空链表的情况
{
Plist = Newnode;
return 0;
}
while(Curnode->next != NULL)
{
Curnode = Curnode->next;
}
Curnode->next = Newnode;
Newnode->prev = Curnode;
return 0;
}
//查找指定位置的结点,返回该结点的值
int Getelement(Node *Plist,int position)
{
printf("Getelement start!\n");
Node *Curnode;
int i = 1;
Curnode = Plist;
while(Curnode != NULL)
{
if(i == position)
{
printf("%d",Curnode->value);
printf("\nGetelement end!\n");
return 0;
}
Curnode = Curnode->next;
i++;
}
printf("can not find the element!");
printf("Getelement end!\n");
return -1;
}
//从第一个结点开始遍历到最后
int PrintList(Node *Plist)
{
printf("PrintList start!\n");
if(Plist == NULL)
{
printf("the LinkList is null\n");
}
else
{
while(Plist != NULL)
{
printf("%d ",Plist->value);
Plist = Plist->next;
}
}
printf("\n");
printf("PrintList end!\n");
return 0;
}
//给定结点指针分别往前后遍历
int TraList(Node *GiveNode)
{
Node *Prevnode;
Node *Nextnode;
Prevnode = GiveNode;
Nextnode = GiveNode;
printf("TraList start!\n");
if(Prevnode == NULL)
{
printf("the LinkList is null\n");
}
else
{
printf("Traverse backwards:\n");
while(Prevnode != NULL)
{
printf("%d ",Prevnode->value);
Prevnode = Prevnode->prev;
}
printf("\n");
printf("Traverse forward:\n");
while(Nextnode != NULL)
{
printf("%d ",Nextnode->value);
Nextnode = Nextnode->next;
}
}
printf("\n");
printf("TraList end!\n");
return 0;
}
//删除指定位置结点
int DeleteList(Node *Plist,int position)
{
Node *Delnode;
int i = 1;
printf("DeleteList start!\n");
Delnode = Plist;
while(Delnode != NULL)
{
if(i == position)
{
Delnode->next->prev = Delnode->prev;
Delnode->prev->next = Delnode->next;
free(Delnode);
printf("DeleteList end!\n");
return 0;
}
Delnode = Delnode->next;
i++;
}
printf("can not find the element!");
printf("DeleteList end!\n");
return 0;
}
int main()
{
printf("main bgein!\n");
Node *Plist;
Node *GiveNode;
int item;
InitList(Plist);
Plist = CreateList(Plist);
PrintList(Plist); //打印全部结点
printf("please input a item you want to insert:");
scanf("%d",&item);
InsertList(Plist,item); //插入结点
PrintList(Plist);
Getelement(Plist,3);
DeleteList(Plist,5);
PrintList(Plist);
GiveNode = Plist->next->next;
TraList(GiveNode); //按给定结点分别向前向后遍历
printf("main end!\n");
return 0;
}