#ifndef _COMMONHEADER_H_
#define _COMMONHEADER_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <strings.h>
#include <time.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <signal.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <dirent.h>
#define BUF_SIZE 1024
#include <linux/input.h>
#include <sys/mman.h>
#endif
#include "myhead.h"
typedef struct doublelist
{
int data;
struct doublelist *next;
struct doublelist *prev;
}dlist;
dlist *init_list()
{
dlist *head=malloc(sizeof(dlist));
head->next=head;
head->prev=head;
return head;
}
int insert_tail(int newdata,dlist *head)
{
dlist *p=head;
while(p->next!=head)
p=p->next;
dlist *newnode=malloc(sizeof(dlist));
newnode->data=newdata;
newnode->next=NULL;
newnode->prev=NULL;
p->next=newnode;
newnode->prev=p;
newnode->next=head;
head->prev=newnode;
}
int show_list(dlist *head)
{
dlist *p=head;
while(p->next!=head)
{
p=p->next;
printf("当前遍历的节点中存放的数据是:%d\n",p->data);
}
}
int insert_mid(int newdata,int olddata,dlist *head)
{
dlist *p=head;
while(p->next!=head)
{
if(p->data==olddata)
break;
p=p->next;
}
dlist *newnode=malloc(sizeof(dlist));
newnode->data=newdata;
newnode->next=NULL;
newnode->prev=NULL;
if(p->next==head)
{
p->next=newnode;
newnode->prev=p;
newnode->next=head;
head->prev=newnode;
return 0;
}
else
{
newnode->next=p->next;
p->next=newnode;
newnode->next->prev=newnode;
newnode->prev=p;
return 0;
}
}
int remove_list(int deldata,dlist *head)
{
dlist *p=head;
dlist *temp;
while(p->next!=head)
{
p=p->next;
if(p->data==deldata)
{
temp=p->next;
p->prev->next=p->next;
p->next->prev=p->prev;
p->next=NULL;
p->prev=NULL;
free(p);
p=temp->prev;
}
}
}
int main()
{
dlist *myhead=init_list();
insert_tail(78,myhead);
insert_tail(78,myhead);
insert_tail(78,myhead);
insert_tail(78,myhead);
insert_tail(38,myhead);
printf("====尾插完毕=====\n");
show_list(myhead);
remove_list(78,myhead);
printf("====删除完毕=====\n");
show_list(myhead);
}