#include <stdio.h>
#include <malloc.h>
#define OPT_SUCCESS 1
#define OPT_FAILURE 0
typedef int ElemType;
struct LinkedList
{
int data;
struct LinkedList *next;
struct LinkedList *prev;
};
typedef struct LinkedList Node;
Node* InitList(){
Node *p;
p = (Node *) malloc (sizeof(Node));
if(p==NULL) return NULL;
p->data = 0;
p->next = NULL;
p->prev = NULL;
return p;
}
Node* NewNode(ElemType k){
Node* p;
p = (Node *)malloc(sizeof(Node));
if( p == NULL) return NULL;
p->data = k;
p->next = p->prev = NULL;
return p;
}
int IsEmptyList(Node* plist){
if(plist->next == plist->prev) return OPT_SUCCESS;
return OPT_FAILURE;
}
int IsCircleList(Node* plist){
Node *pSentry = plist->next;
Node *opt = pSentry;
if (pSentry->prev != NULL) return OPT_SUCCESS;
while(opt != NULL){
opt = opt->next;
if (opt == pSentry) return OPT_SUCCESS;
}
return OPT_FAILURE;
}
void DestoryList(Node* plist){
Node *pSentry = plist->next;
Node *opt = plist->next;
while(opt != NULL){
pSentry = opt;
opt = opt->next;
free(pSentry);
}
free(plist);
}
int AppendElem(Node* phead,ElemType e){
Node *plist;
plist = phead;
if(plist->next == NULL){
plist->next = NewNode(e);
plist->prev = NULL;
return OPT_SUCCESS;
}
Node* pSentry = NULL;
Node* opt;
opt = plist->next;
while(opt!=NULL){
pSentry = opt;
if(opt->next!=NULL) opt = opt->next;
else break;
}
opt->next = NewNode(e);
opt->next->prev = pSentry;
opt->next->next = NULL;
return OPT_SUCCESS;
}
int HeadinterElem(Node* phead, ElemType e){
Node * plist;
plist = phead;
if (plist == NULL) return OPT_FAILURE;
if (plist->next == NULL){
plist->next = NewNode(e);
plist->prev = NULL;
return OPT_SUCCESS;
}
Node *p;
p = NewNode(e);
p->next = plist->next;
p->prev = NULL;
plist->next = p;
if(p->next!= NULL)
p->next->prev = p;
return OPT_SUCCESS;
}
int DeleteElem(Node* plist,ElemType e){
if (plist == NULL) return OPT_FAILURE;
Node *pSentry = NULL;
Node* opt = plist;
while(opt != NULL){
pSentry = opt;
opt = opt->next;
if(opt->data == e){
pSentry->next = opt->next;
opt->next->prev = pSentry;
free(opt);
return OPT_SUCCESS;
}
}
return OPT_FAILURE;
}
int UpdateElem(Node* plist,ElemType e, ElemType update){
if (plist == NULL) return OPT_FAILURE;
Node *pSentry = NULL;
Node* opt = plist->next;
while(opt != NULL){
pSentry = opt;
opt = opt->next;
if(opt->data == e){
opt->data = update;
return OPT_SUCCESS;
}
}
return OPT_FAILURE;
}
Node* SearchElem(Node *plist, ElemType e){
if (plist == NULL) return OPT_FAILURE;
Node *pSentry = NULL;
Node* opt = plist;
while(opt != NULL){
pSentry = opt;
opt = opt->next;
if(opt->data == e){
return opt;
}
}
return NULL;
}
int main(){
Node* head;
head = InitList();
Node *opt =NULL;
printf("%d\n",IsEmptyList(head));
ElemType e;
for (int i = 0; i < 10; i++)
{
scanf("%d",&e);
if(HeadinterElem(head,e) == OPT_SUCCESS){
printf("Insert Success\n");
}else{
printf("Insert Failure\n");
}
}
opt = head->next;
int i = 0;
while(opt!=NULL){
printf("%d\n",opt->data);
opt = opt->next;
i++;
if(i > 100) break;
}
return 0;
}