题目:
实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数
题解:
- #include <stdio.h>
- typedef struct tmpNode{
- struct tmpNode *prev;
- struct tmpNode *next;
- int value;
- }TNode;
- TNode *root=NULL;
- int main(void){
- int value;
- root=(TNode *)malloc(sizeof(TNode));
- root->next=root;
- root->prev=root;
- root->value=0;
- addNode();
- addNode();
- addNode();
- addNode();
- addNode();
- addNode();
- addNode();
- addNode();
- scanf("%d",&value);
- insertNode(value);
- viewNode();
- scanf("%d",&value);
- deleteNode(value);
- viewNode();
- scanf("%d",&value);
- deleteNode(value);
- viewNode();
- return 0;
- }
- void addNode(){
- TNode *temp;
- TNode *newNode;
- newNode=(TNode *)malloc(sizeof(TNode));
- newNode->prev=newNode;
- newNode->next=newNode;
- newNode->value=0;
- temp=root;
- while(temp->next!=NULL&&temp->next!=temp){
- temp=temp->next;
- }
- temp->next=newNode;
- newNode->prev=temp;
- newNode->value=temp->value+1;
- }
- void viewNode(){
- TNode *temp;
- temp=root;
- while(1){
- printf("this node addr is %p,and the value is %d/n",temp,temp->value);
- if(temp->next==NULL||temp->next==temp)
- break;
- temp=temp->next;
- }
- }
- void insertNode(int value){
- TNode *temp;
- TNode *newNode;
- temp=root;
- newNode=(TNode *)malloc(sizeof(TNode));
- newNode->next=newNode;
- newNode->prev=newNode;
- newNode->value=value;
- while(temp->value<newNode->value&&temp->next!=NULL&&temp->next!=temp){
- temp=temp->next;
- }
- if(temp->next!=NULL&&temp->next!=temp){
- TNode *tmpNode=temp->next;
- tmpNode->prev=newNode;
- newNode->next=tmpNode;
- }
- newNode->prev=temp;
- temp->next=newNode;
- }
- void deleteNode(int value){
- TNode *temp;
- temp=root;
- while(temp->value!=value&&temp->next!=NULL&&temp->next!=temp){
- temp=temp->next;
- }
- if(temp->value!=value)
- return;
- if(temp->prev!=NULL&&temp->prev!=temp){
- TNode *prev;
- prev=temp->prev;
- if(temp->next!=NULL&&temp->next!=temp){
- TNode *next;
- next=temp->next;
- prev->next=next;
- next->prev=prev;
- }else{
- prev->next=prev;
- }
- }else{
- if(temp->next!=NULL&&temp->next!=temp){
- TNode *next;
- next=temp->next;
- next->prev=next;
- root=next;
- }else{
- printf("can't delete root!/n");
- return;
- }
- }
- free(temp);
- temp=NULL;
- }
编译环境:
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
运行过程:
[explover@Explover 20081101]$ gcc -g pl.c -o plpl.c:34: warning: type mismatch with previous implicit declarationpl.c:15: warning: previous implicit declaration of `addNode'pl.c:34: warning: `addNode' was previously implicitly declared to return `int'pl.c:49: warning: type mismatch with previous implicit declarationpl.c:25: warning: previous implicit declaration of `viewNode'pl.c:49: warning: `viewNode' was previously implicitly declared to return `int'pl.c:59: warning: type mismatch with previous implicit declarationpl.c:24: warning: previous implicit declaration of `insertNode'pl.c:59: warning: `insertNode' was previously implicitly declared to return `int'pl.c:78: warning: type mismatch with previous implicit declarationpl.c:27: warning: previous implicit declaration of `deleteNode'pl.c:78: warning: `deleteNode' was previously implicitly declared to return `int'[explover@Explover20081101]$ ./pl9this node addr is 0x80499a8,and the value is 0this node addr is 0x80499b8,and the value is 1this node addr is 0x80499c8,and the value is 2this node addr is 0x80499d8,and the value is 3this node addr is 0x80499e8,and the value is 4this node addr is 0x80499f8,and the value is 5this node addr is 0x8049a08,and the value is 6this node addr is 0x8049a18,and the value is 7this node addr is 0x8049a28,and the value is 8this node addr is 0x8049a38,and the value is 95this node addr is 0x80499a8,and the value is 0this node addr is 0x80499b8,and the value is 1this node addr is 0x80499c8,and the value is 2this node addr is 0x80499d8,and the value is 3this node addr is 0x80499e8,and the value is 4this node addr is 0x8049a08,and the value is 6this node addr is 0x8049a18,and the value is 7this node addr is 0x8049a28,and the value is 8this node addr is 0x8049a38,and the value is 99this node addr is 0x80499a8,and the value is 0this node addr is 0x80499b8,and the value is 1this node addr is 0x80499c8,and the value is 2this node addr is 0x80499d8,and the value is 3this node addr is 0x80499e8,and the value is 4this node addr is 0x8049a08,and the value is 6this node addr is 0x8049a18,and the value is 7this node addr is 0x8049a28,and the value is 8
(本人水平有限,不足之处,请大家多多指正,谢谢!)