C語言資料結構之鍊錶部分(11)

2021-10-03 17:09:39 字數 1846 閱讀 4529

11.建立單向迴圈鍊錶

#include

#include

typedef struct node

elemsn;

elemsn *createlink(int a,int n);

void printlink(elemsn *h);

elemsn *delkeynode(elemsn *h,int key);

int main()

;int key;

printf("請輸入關鍵字值key:");

scanf("%d",&key);

elemsn *head;

//建立單向迴圈鍊錶

head = createlink(a,5);

//列印迴圈單向鍊錶

printf("迴圈單向鍊錶結果為:");

printlink(head);

printf("\n");

//刪除關鍵字key

head = delkeynode(head,key);

//列印刪除後的迴圈單向鍊錶

printf("刪除後的鍊錶為:");

printlink(head);

printf("\n");

return 0;

}elemsn *createlink(int a,int n)

else

}return h;

}void printlink(elemsn *h)

while(p != h);

}elemsn *delkeynode(elemsn *h,int key)

else

break;

}while(p != h);

if(p == h && q != null)

else  //肯定找著了

else  //說明p != h(即在中間)

}return h;

}//約瑟夫環問題

#include

#include

typedef struct node

elemsn;

elemsn *createlink(int a,int n);

void printlink(elemsn *h);

void ysofloop(elemsn *h,int n,int s);

int main()

;int s;

printf("請輸入間隔值s:");

scanf("%d",&s);

elemsn *head;

//建立單向迴圈鍊錶

head = createlink(a,10);

//列印迴圈單向鍊錶

printf("迴圈單向鍊錶結果為:");

printlink(head);

printf("\n");

//約瑟夫環

printf("約瑟夫環結果為:");

ysofloop(head,10,3);

printf("\n");

return 0;

}elemsn *createlink(int a,int n)

else

}return h;

}void printlink(elemsn *h)

while(p != h);

}void ysofloop(elemsn *h,int n,int s)

printf("%d ",p->data);

q->next = p->next;

free(p);

p = q->next;

i = 1;

n--;

}printf("%d ",p->data);

}

C語言資料結構之鍊錶部分(2)

2.鍊錶的一些練習 1 輸出尾結點元素 include include define n 5 typedef struct node elemsn elemsn createlink1 int a void printlink elemsn h int main elemsn head null h...

C語言資料結構之鍊錶部分(10)

10.練習 統計單向鍊錶的每個元素出現的次數 include include define n 11 typedef struct node elemsn elemsn createlink int a void printlink elemsn h int main 建立 head createl...

C 語言 資料結構之雙向鍊錶

雙向鍊錶的空間結構如下圖所示 int initdlist dlinklist head 初始化雙向迴圈鍊錶 int createdlist dlinklist head,int n 建立雙向迴圈鍊錶 return 1 void printdlist dlinklist head 輸出雙向迴圈鍊錶中的...