선형 표 의 이중 순환 링크
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int Elemtype;
typedef int Status;
typedef struct Node{
Elemtype data;
struct Node* prior;
struct Node* next;
}Node;
typedef struct Node* DolLinkList;
Status InitDolLinkList(DolLinkList *DL){
*DL = (DolLinkList)malloc(sizeof(struct Node));
if(!(*DL))
return ERROR;
(*DL)->next = *DL;
(*DL)->prior = *DL;
return OK;
}
Status ClearDolLinkList(DolLinkList DL){
//
DolLinkList s,f = DL->next;
while(f != DL){
s = f;
f = f->next;
free(s);
}
DL->next = DL;
DL->prior = DL;
return OK;
}
Status DestoryDolLinkList(DolLinkList DL){
ClearDolLinkList(DL);
free(DL);
DL = NULL;
}
int Length_DolLinkList(DolLinkList DL){
int length = 0;
DolLinkList t = DL->next;
while(t != DL){
t = t->next;
length++;
}
return length;
}
Status GetElement_DolLinkList(DolLinkList DL,int position,int *value){
int count = 0;
int length = Length_DolLinkList(DL);
if(position < 1 || position > length){
return ERROR;
}
else{
while(count < position){
DL = DL->next;
count++;
}
*value = DL->data;
return OK;
}
}
Status LocateElement_DolLinkList(DolLinkList DL,Elemtype value,int *position){
*position = 1;
DolLinkList t = DL->next;
while(t != DL){
if(t->data == value){
return OK;
}
t = t->next;
(*position)++;
}
return ERROR;
}
Status GetPriorElement_DolLinkList(DolLinkList DL,int currentElement,int *priorElement){
int r1;
int count = 0;
if(LocateElement_DolLinkList(DL,currentElement,&r1)){
while(count < r1){
count++;
DL = DL->next;
}
if(count == 1)
*priorElement = DL->prior->prior->data;
else
*priorElement = DL->prior->data;
return OK;
}
else
return ERROR;
}
Status GetNextElement_DolLinkList(DolLinkList DL,int currentElement,int *nextElement){
int r1;
if(LocateElement_DolLinkList(DL,currentElement,&r1)){
int count = 0;
while(count < r1){
count++;
DL = DL->next;
}
if(count == Length_DolLinkList(DL))
*nextElement = DL->next->next->data;
else
*nextElement = DL->next->data;
return OK;
}
else
return ERROR;
}
Status Insert_DolLinkList(DolLinkList DL,int position,int value){
if(position < 1 || position > Length_DolLinkList(DL) + 1)
return ERROR;
else{
int count = 0;
while(count < position){
count++;
DL = DL->next;
}
DolLinkList new = (DolLinkList)malloc(sizeof(struct Node));
new->data = value;
new->prior = DL->prior;
DL->prior->next = new;
new->next = DL;
DL->prior = new;
return OK;
}
}
Status Delete_DolLinkList(DolLinkList DL,int position,int *value){
if(position < 1 || position > Length_DolLinkList(DL))
return ERROR;
else{
int count = 0;
while(count < position){
count++;
DL = DL->next;
}
*value = DL->data;
DL->next->prior = DL->prior;
DL->prior->next = DL->next;
free(DL);
}
}
Status CreateDolLinkList_TailInsert(DolLinkList DL,int number){
DolLinkList new;
int i;
printf("PLEASE ENTER %d ELEMNET!
",number);
for(i = 1; i <= number; i++){
new = (DolLinkList)malloc(sizeof(struct Node));
if(!new)
return ERROR;
printf("please enter element%d: ",i);
scanf("%d",&(new->data));
new->prior = DL->prior;
new->next = DL;
DL->prior->next = new;
DL->prior = new;
}
return OK;
}
void DisplayDolLinkList(DolLinkList DL){
DolLinkList temp = DL;
while(temp->next != DL){
temp = temp->next;
printf("%d ",temp->data);
}
printf("
Display Executed!
");
}
int main(){
DolLinkList DL;
InitDolLinkList(&DL);
CreateDolLinkList_TailInsert(DL,5);
printf("the length of list is %d
",Length_DolLinkList(DL));
DisplayDolLinkList(DL);
int r1,r2;
if(GetElement_DolLinkList(DL,3,&r1))
printf("the element of postion 3 is %d
",r1);
else
printf("Error:getElement:position
");
if(GetElement_DolLinkList(DL,6,&r2))
printf("the element of postion 6 is %d
",r2);
else
printf("Error:getElement:position
");
if(LocateElement_DolLinkList(DL,4,&r1))
printf("the position of element 4 is %d
",r1);
else
printf("Error:LocateElement:value
");
if(LocateElement_DolLinkList(DL,8,&r1))
printf("the position of element 8 is %d
",r1);
else
printf("Error:LocateElement:value
");
if(GetPriorElement_DolLinkList(DL,6,&r1))
printf("before the 6 is %d
",r1);
else
printf("Error:GetPriorElement:currentElement
");
if(GetPriorElement_DolLinkList(DL,1,&r2))
printf("before the 1 is %d
",r2);
else
printf("Error:GetPriorElement:currentElement
");
if(GetNextElement_DolLinkList(DL,2,&r1))
printf("next the 2 is %d
",r1);
else
printf("Error:nextElement:currentElement
");
if(GetNextElement_DolLinkList(DL,5,&r1))
printf("next the 5 is %d
",r1);
else
printf("Error:nextElement:currentElement
");
Insert_DolLinkList(DL,3,666);
Insert_DolLinkList(DL,7,999);
Insert_DolLinkList(DL,1,111);
Insert_DolLinkList(DL,9,2222);
DisplayDolLinkList(DL);
Delete_DolLinkList(DL,2,&r1);
Delete_DolLinkList(DL,8,&r1);
DisplayDolLinkList(DL);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 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에 따라 라이센스가 부여됩니다.