T_2(带头结点的双向循环链表)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
//*******************************//
typedef struct Node{
char data;
Node *pre, *next;
}Node;
//*******************************//
Node *List_Creat(void){
Node *Head=NULL, *buff=NULL, *pre=NULL;
char data=0;
Head = (Node*)malloc(sizeof(Node));
assert(Head);
Head->data = 0;
Head->next = Head;
Head->pre = Head;
pre = Head;
while(1){
scanf("\n%c", &data);
if('.'==data) break;
buff = (Node*)malloc(sizeof(Node));
assert(buff);
buff->data = data;
buff->next = Head;
buff->pre = pre;
Head->pre = buff;
pre->next = buff;
pre = buff;
}
return Head;
}
void List_Output(Node *Head){
assert(Head);
Node *now=Head->next;
while(Head!=now){
printf("%c\n", now->data);
now = now->next;
}
}
Node* List_Insert_1(Node *Head, int n, char data){
assert(Head);
Node *now=Head->next, *buff=NULL;
int i=1;
while(ii++;
now = now->next;
}
if(now!=Head){
buff = (Node*)malloc(sizeof(Node));
assert(buff);
buff->data = data;
buff->next = now;
buff->pre = now->pre;
now->pre->next = buff;
now->pre = buff;
}
return Head;
}
Node* List_Insert_2(Node *Head, int n, char data){
assert(Head);
Node *now=Head->next, *buff=NULL;
int i=1;
while(ii++;
now = now->next;
}
if(now!=Head){
buff = (Node*)malloc(sizeof(Node));
assert(buff);
buff->data = data;
buff->next = now->next;
buff->pre = now;
now->next->pre = buff;
now->next = buff;
}
return Head;
}
Node* List_Insert_3(Node *Head, char x, char data){
assert(Head);
Node *now=Head->next, *buff=NULL;
while(now->data!=x && now!=Head) now = now->next;
if(now!=Head){
buff = (Node*)malloc(sizeof(Node));
assert(buff);
buff->data = data;
buff->next = now;
buff->pre = now->pre;
now->pre->next = buff;
now->pre = buff;
}
return Head;
}
Node* List_Insert_4(Node *Head, char x, char data){
assert(Head);
Node *now=Head->next, *buff=NULL;
while(now!=Head && now->data!=x) now = now->next;
if(now!=Head){
buff = (Node*)malloc(sizeof(Node));
assert(buff);
buff->data = data;
buff->next = now->next;
buff->pre = now;
now->next->pre = buff;
now->next = buff;
}
return Head;
}
Node* List_Del_1(Node *Head, int n){
assert(Head);
Node *now=Head->next;
int i=1;
while(ii++;
now = now->next;
}
if(now!=Head){
now->next->pre = now->pre;
now->pre->next = now->next;
free(now);
}
return Head;
}
Node* List_Del_2(Node *Head, char x){
assert(Head);
Node *now=Head->next;
while(now!=Head && now->data!=x) now = now->next;
if(now!=Head){
now->next->pre = now->pre;
now->pre->next = now->next;
free(now);
}
return Head;
}
Node* List_Del_3(Node *Head, char x){
assert(Head);
Node *now=Head->next, *buff=NULL;
while(now!=Head){
if(now->data == x){
buff = now;
now->next->pre = now->

pre;
now->pre->next = now->next;
now = now->next;
free(buff);
}
else now = now->next;
}
return Head;
}
Node* List_Del_4(Node *Head, char x){
assert(Head);
Node *now=Head->next, *buff=NULL;
int flag=0;
while(now!=Head){
if(now->data == x){
if(0==flag){
flag = 1;
now = now->next;
}
else{
buff = now;
now->next->pre = now->pre;
now->pre->next = now->next;
now = now->next;
free(buff);
}
}
else now = now->next;
}
return Head;
}
Node* List_Del_5(Node *Head){
assert(Head);
Node *now=Head->next, *buff=NULL;
int i=0, j=0;
char x=0;
while(1){
i = j;
now = Head->next;
while(i--) now = now->next;
if(now == Head) break;
x = now->data;
now = now->next;
while(now!=Head){
if(now->data == x){
buff = now;
now->next->pre = now->pre;
now->pre->next = now->next;
now = now->next;
free(buff);
}
else now = now->next;
}
j++;
}
return Head;
}
//*******************************//
int main(void){
Node *Head=List_Creat();
// Head = List_Insert_1(Head, 1, 'a');
// Head = List_Insert_1(Head, 5, 'b');
// Head = List_Insert_1(Head, 3, 'c');
// Head = List_Insert_1(Head, 44, 'e');
// Head = List_Insert_2(Head, 1, 'a');
// Head = List_Insert_2(Head, 5, 'b');
// Head = List_Insert_2(Head, 3, 'c');
// Head = List_Insert_2(Head, 44, 'e');
// Head = List_Insert_3(Head, '1', 'a');
// Head = List_Insert_3(Head, '5', 'b');
// Head = List_Insert_3(Head, '3', 'c');
// Head = List_Insert_3(Head, 'e', 'e');
// Head = List_Insert_4(Head, '1', 'a');
// Head = List_Insert_4(Head, '5', 'b');
// Head = List_Insert_4(Head, '3', 'c');
// Head = List_Insert_4(Head, 'e', 'e');
// Head = List_Del_1(Head, 1);
// Head = List_Del_1(Head, 4);
// Head = List_Del_1(Head, 2);
// Head = List_Del_1(Head, 12);
// Head = List_Del_2(Head, '1');
// Head = List_Del_2(Head, '5');
// Head = List_Del_2(Head, '3');
// Head = List_Del_2(Head, 'e');
// Head = List_Del_3(Head, '3');
// Head = List_Del_4(Head, '3');
// Head = List_Del_5(Head);

List_Output(Head);
while(1);
return 0;
}

相关文档
最新文档