1.概念:
- #include
- #include
- #include
-
- typedef struct node{
- struct node *prev;
- struct node *next;
- int data;
- }Node;
-
- Node *buylistnode(int data){
- Node *newnode=(Node *)malloc(sizeof(Node));
- if(newnode==NULL){
- printf("malloc error");
- exit(-1);
- }
- newnode->prev=newnode->next=NULL;
- newnode->data=data;
- return newnode;
- }
-
- Node *listinit(){
- Node *plist=buylistnode(0);
- plist->prev=plist->next=plist;
- return plist;
- }
-
- void listpushback(Node *plist,int data){
- if(plist==NULL){
- printf("This list is empty.\n");
- return;
- }
- Node *newnode=buylistnode(data);
- Node *tail=plist->prev;
- tail->next=newnode;
- newnode->prev=tail;
- newnode->next=plist;
- plist->prev=newnode;
- }
-
- void listpopback(Node *plist){
- if(plist==NULL){
- printf("This list is empty.\n");
- return;
- }
- if(plist->next!=plist){
- printf("This list is not only head.\n");
- }
- Node *tail=plist->prev;
- Node *tailprev=tail->prev;
- tailprev->next=plist;
- plist->prev=tailprev;
- free(tail);
- tail=NULL;
- }
-
- void listpushfront(Node *plist,int data){
- if(plist==NULL){
- printf("This list is empty.\n");
- }
- Node *newnode=buylistnode(data);
- Node *first=plist->next;
- plist->next=newnode;
- newnode->prev=plist;
- newnode->next=first;
- first->prev=newnode;
- }
-
- void listpopfront(Node *plist){
- if(plist==NULL){
- printf("This list is empty.\n");
- return;
- }
- if(plist->next!=plist){
- printf("List is not only head\n");
- }
- Node *first=plist->next;
- Node *second=first->next;
- plist->next=second;
- second->prev=plist;
- free(first);
- first=NULL;
- }
-
- Node *listfind(Node *plist,int data){
- if(plist==NULL){
- printf("This list is empty.\n");
- return NULL;
- }
- Node *cur=plist->next;
- while(cur!=plist){
- if(cur->data==data){
- return cur;
- }
- cur=cur->next;
- }
- return NULL;
- }
-
- void listinsert(Node *pos,int data){
- if(pos==NULL){
- printf("List is empty.\n");
- return;
- }
- Node *newnode=buylistnode(data);
- Node *posprev=pos->prev;
- posprev->next=newnode;
- newnode->prev=posprev;
- newnode->next=pos;
- pos->prev=newnode;
- }
-
- void listerase(Node *pos){
- if(pos==NULL){
- printf("List is empty.\n");
- return;
- }
- Node *posprev=pos->prev;
- Node *posnext=pos->next;
- posprev->next=posnext;
- posnext->prev=posprev;
- free(pos);
- pos=NULL;
- }
-
- void listprint(Node *plist){
- if(plist==NULL){
- printf("List is empty.\n");
- return;
- }
- Node *cur=plist->next;
- while(cur!=plist){
- if(cur->next==plist){
- printf("%d",cur->data);
- }else{
- printf("%d<->",cur->data);
- }
- cur=cur->next;
- }
- printf("\n");
- }
-
- void testlist01(){
- Node *plist=listinit();
- listpushback(plist,1);
- listpushback(plist,2);
- listpushback(plist,3);
- listpushback(plist,4);
- listprint(plist);
-
- listpopback(plist);
- listpopback(plist);
- listpopback(plist);
- listpopback(plist);
- listprint(plist);
-
- listpushfront(plist,1);
- listpushfront(plist,2);
- listpushfront(plist,3);
- listpushfront(plist,4);
- listprint(plist);
-
- listpopfront(plist);
- listpopfront(plist);
- listpopfront(plist);
- listpopfront(plist);
- listprint(plist);
- }
-
- void testlist02(){
- Node *plist=listinit();
- listpushback(plist,1);
- listpushback(plist,1);
- listpushback(plist,1);
- listpushback(plist,1);
- listprint(plist);
-
- Node *pos=listfind(plist,3);
- listinsert(pos,30);
- listprint(plist);
- listerase(pos);
- listprint(plist);
- }
-
- int main(){
- testlist01();
- testlist02();
- }
结果如下:
