양 방향 링크 찾기, 삭제, 삽입 노드

7017 단어 데이터 구조
양 방향 링크 앞 에 노드 의 앞 자 리 를 찾 을 필요 가 없습니다. 그 자체 에 prior 포인터 가 있 기 때 문 입 니 다.근본적으로 말하자면 그것 은 단일 체인 시계 와 유사 하 다.
#include
#include
typedef struct student
{
    int num;
    struct student *next;
    struct student *prior;
}LDstudent,*LDPstudent;
//-------------    
void InitLink(LDPstudent *phead)
{
    *phead=NULL;
    LDPstudent ptr;
    LDPstudent r=ptr;//          
    int n;
    printf("please input num of student:
"); scanf("%d",&n); while(n>0) { ptr=malloc(sizeof(LDstudent)); ptr->num=n; /* ptr->next=*phead;// ptr->prior=NULL; if(*phead!=NULL) (*phead)->prior=ptr; *phead=ptr; */ // ptr->next=NULL; if(*phead==NULL) { ptr->prior=NULL; *phead=ptr;} else { ptr->prior=r; r->next=ptr;} r=ptr; printf("please input num of student:
"); scanf("%d",&n); } } //------------- void printf_link(LDstudent *ptr) { LDPstudent p1=ptr; printf(" :
"); while(p1!=NULL) { printf("%d\t",p1->num); p1=p1->next; } printf("
"); printf(" :
"); p1=ptr; while(p1->next!=NULL) p1=p1->next; while(p1!=NULL) { printf("%d\t",p1->num); p1=p1->prior; }printf("
"); } //----------- unsigned int ListLength(LDPstudent ptr) { unsigned int l=0; while(ptr!=NULL) { ptr=ptr->next; l++;} return l; } //------------------- LDPstudent ListLocate(LDPstudent ptr,int i) { int j=1; while(ptr!=NULL&&jnext; j++; } if(j==i)return ptr; else return NULL; } //----------------------- LDPstudent ListLocate2(LDPstudent ptr,int i) { while(ptr!=NULL) { if(ptr->num==i)return ptr; else ptr=ptr->next;} return ptr; } //------------------------- void ListInsert(LDPstudent ptr,int i,int j) { LDPstudent p1=ListLocate(ptr,i); if(p1==NULL) { printf(" %d ,error!
",i);return;} else{ LDPstudent p2=malloc(sizeof(LDstudent)); p2->num=j; p2->prior=p1; if(p1->next!=NULL) { p2->next=p1->next; p1->next->prior=p2;} else p2->next=NULL; p1->next=p2;} printf("---------- ----------:
"); printf_link(ptr); } //------------------------- void ListInsert2(LDPstudent ptr,int i,int j) { LDPstudent p1=ListLocate(ptr,i); if(p1==NULL) { printf(" %d ,error!
",i);return;} else{ LDPstudent p2=malloc(sizeof(LDstudent)); p2->num=j; p2->next=p1; p2->prior=p1->prior; if(p1->prior!=NULL) { p1->prior->next=p2;} else ptr=p2;// ; p1->prior=p2;} printf("---------- ----------:
"); printf_link(ptr); } //-------------------------- void ListInsert3(LDPstudent ptr,int i,int j) { LDPstudent p1=ListLocate2(ptr,i); if(p1==NULL) { printf(" ,error!
");return;} else{ LDPstudent p2=malloc(sizeof(LDstudent)); p2->num=j; p2->prior=p1; if(p1->next!=NULL) { p2->next=p1->next; p1->next->prior=p2;} else p2->next=NULL; p1->next=p2;} printf("---------- ----------:
"); printf_link(ptr); } //--------------------------- void ListInsert4(LDPstudent ptr,int i,int j) { LDPstudent p1=ListLocate2(ptr,i); if(p1==NULL) { printf(" ,error!
");return;} else{ LDPstudent p2=malloc(sizeof(LDstudent)); p2->num=j; p2->next=p1; p2->prior=p1->prior; if(p1->prior!=NULL) { p1->prior->next=p2;} else ptr=p2;// ; p1->prior=p2;} printf("---------- ----------:
"); printf_link(ptr); } //----------------------------- void ListDelete(LDPstudent ptr,int i) { LDPstudent p1=ListLocate2(ptr,i); if(p1==NULL) { printf(" ( ) ,error!
");return;} else { if(p1->prior==NULL&&p1->next!=NULL) { p1->next->prior=NULL;ptr=p1->next;// } else if(p1->next==NULL&&p1->prior!=NULL) { p1->prior->next=NULL; } else if(p1->prior==NULL&&p1->next==NULL) { ptr=NULL; } else { p1->prior->next=p1->next; p1->next->prior=p1->prior; } printf("
------ :----------
"); if(ptr==NULL)printf("NULL
"); else printf_link(ptr); } } //----------------------------- void ListDelete2(LDPstudent ptr,int i) { LDPstudent p1=ListLocate2(ptr,i); if(p1==NULL) { printf(" ( ) ,error!
");return;} else { if(p1->prior==NULL&&p1->next!=NULL) { p1->next->prior=NULL;ptr=p1->next;// } else if(p1->next==NULL&&p1->prior!=NULL) { p1->prior->next=NULL; } else if(p1->prior==NULL&&p1->next==NULL) { ptr=NULL; } else { p1->prior->next=p1->next; p1->next->prior=p1->prior; } printf("
------ :----------
"); if(ptr==NULL)printf("NULL
"); else printf_link(ptr); } } // -====================== int main() { LDPstudent head; InitLink(&head); printf_link(head);// unsigned int l;// l=ListLength(head); printf("
l:%d
",l); /* LDPstudent p=ListLocate(head,3);// if(p==NULL) printf("no third student
"); else printf("this third is %d
",p->num); LDPstudent p2=ListLocate2(head,4);// if(p2==NULL) printf("no this student
"); else printf("this is %d
",p2->num); */ //  ListInsert(head,3,3333);// //  ListInsert2(head,3,2222);// //  ListInsert3(head,6,6666);// //  ListInsert4(head,6,5555);// ListDelete(head,3);// ListDelete2(head,6);// }

좋은 웹페이지 즐겨찾기