양 방향 링크 찾기, 삭제, 삽입 노드
7017 단어 데이터 구조
#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);//
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.