日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据机构-四种链表

發布時間:2025/4/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据机构-四种链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

單鏈表


#include <stdio.h> #include <math.h> #include <malloc.h> //不要丟掉 #include <string.h> #include <conio.h>#define NULL0 typedefstruct student {int data;struct student * next; } node;//---------------------------------------------- node* creat(int n) //1 node *head = NULL, *p = NULL, *s = NULL;int x;head = (node*)malloc(sizeof(node));p = head;for (int i = 1; i <= n; i++) //用一個n次循環來處理{printf("第%d個數據是:", i);scanf("%d;", &x);s = (node *)malloc(sizeof(node));s->next = NULL;s->data = x;p->next = s;p = s;}return (head); }//---------------------------------------------- int ListEmpty(node* h) //返回1說明是空的 {return (h->next == NULL); }//--------------------------------------------void Display(node *h) {node *p = h->next;if (p == NULL){printf("List isempty");return;}while (p != NULL) //這里比較的不是p->next;{//每個元素都可以處理到,循環n次處理n個元素printf("%d ", p->data);p = p->next;}printf("\n"); }//------------------------------------------------int Length(node *h) {node *p = h;int i = 0; //i是用來記錄循環次數的while (p->next){i++;p = p->next;}return i; }//------------------------------void GetElem(node* h, int i, int &e) // 找到單鏈表h中第i個元素 {node *p = h;int j = 0;if (p == NULL){printf("List isempty");return;}while ((j < i) && p->next){j++;p = p->next;}if (p->next == NULL){printf("沒有第%d個元素\n", i);}else{e = p->data;printf("第%d個元素為:%d\n", i, e);} }//-----------------------------------------------void LocateElem(node *h, int e) // 尋找給定元素在表中的位置標號 {node *p = h->next;int i = 1; //跟p不配套了if (p == NULL){printf("List isempty");return;}while (p && (p->data != e)){p = p->next; //此時i跟p是配套的i++;}if (p == NULL){printf("沒有第%d這個元素\n", e);}else{printf("%d在鏈表的第%d個位置\n", e, i);} }int Insert(node* &h, inti, int e) // 在第i個元素前插入一個元素 {node* p = h;node* s;int j = 0;while ((j < i - 1) && p) //選擇p是為了能在最后一個元素后插入元素{j++;p = p->next;}if (p == NULL){printf("i范圍錯誤");return 0;}else{s = (node*)malloc(sizeof(node));s->next = p->next;s->data = e;p->next = s;return 1;} }int Del(node*&h, inti) // 刪除第i個元素 {//要刪除一個節點,p是不可能走到最后一個節點的。int j = 0;node* p = h;node*q = NULL;if (p->next == NULL){//printf("List isempty");return 0;}while ((j < i - 1) && p->next){j++;p = p->next;}if (p->next == NULL){//printf("i超出范圍");return 0;}else{q = p->next; //q=0p->next = q->next; //q=0->nextfree(q);return 1;} }void destroy(node*&h) // 釋放鏈表 {node*p = h;node*q = p->next;while (q != NULL){free(p);p = q;q = q->next;}free(p); }void reverse(node*&h) {node*q1 = h->next,;node*q2 = q1->next;node*temp;q1->next = NULL;while (q2) //至少有兩個元素才好反轉{temp = q2->next;q2->next = q1;q1 = q2;q2 = temp;}h->next = q1; //q1指向最后一個節點 }node* bubble_sort(node*&h, int size) //因為鏈表是單向的,所以選擇下沉石頭 {int i, j, temp;node*p = h->next;for (j = size - 1; j >= 1; j--){p = h->next;for (i = 0; i + 1 <= j; i++){if (p->next->data < p->data){temp = p->next->data;p->next->data = p->data;p->data = temp;}p = p->next;}}return h; }









?

雙鏈表

typedefstruct DNode

{

??? int data;

??? struct DNode *prior;

??? struct DNode *next;

}node;

?

voidInit(node *&h)

{

??? h=(node*)malloc(sizeof(node));

??? h->prior=h->next=0;

}

?

?

插入節點

intInsert(node *&h,int i,int e)

{

??? int j=0;

??? node*p=h;

??? node*s=null;

??? while(p&&j<i-1)

??? {

??????? j++;

??????? p=p->next;

??? }

??? if(p==null)

??? {

??????? printf("為找到第%d個節點\n",i-1);//未找到地i-1個節點

??????? return 0;

??? }

??? else

??? {

??????? s=(node*)malloc(sizeof(node));

??????? s->data=e;

????????????? if(p->next!= NULL) p->next->prior=s;//如果是要在最后一個元素后面插入這句話就可以省掉

????????????? s->prior=p;

????????????? s->next=p->next;

????????????? p->next=s;

????????????? return 1;

??? }

}

?

刪除節點

intDeleteElem(node*&h,int i)//要刪除一個節點,p是不可能走到最后一個節點的。

{

??? int j=0;

??? node*p=h;

? ??node*q=null;

??? while(j<i-1&&p->next)

??? {

??????? j++;

??????? p=p->next;

??? }

??? if(p->next==null)

??? {

??????? printf("找到地%d個節點\n",i-1);//未找到地i-1的節點,如果要刪除節點的前一個節點是最后一個節點的話

??????? return 0;

??? }

??? else

??? {

??????? q=p->next;//q指向要刪除的節點

? ??????p->next=q->next;//0

??????? if(p->next != null)//如果刪除的是最后一個節點

?????? ?? ? ????p->next->prior=p;

??????? free(q);

??????? return 1;

??? }

}

?

?

循環單鏈表

typedefstruct LNode

{

??? char data;

??? struct LNode *next;

}node;

?

voidInit(node*&h)

{

??? h=(node*)malloc(sizeof(node));

??? h->next=h;

}

?

voidDestroy(node*&h)??? //引起注意

{

??? node*p=h;

??? node*q=p->next;

??? while(q!=h)//注意這里和單鏈表不一樣

??? {

??????? free(p);

??????? p=q;

??????? q=p->next;

??? }

??? free(p);

}

?

voidEmpty(node *h)

{

??? if(h->next==h)

?? ?????printf("list is empty!\n");

??? else

??????? printf("list is notempty\n");

}

?

voidLength(node*h)

{

??? node*p=h->next;

??? int i=0;

??? while(p!=h)

??? {

??????? i++;

??????? p=p->next;

??? }

??? printf("length is %d\n",i);

}

?

voidDisplay(node*h)

{

??? node*p=h->next;

??? if(null==h)

??????? printf("list is empty!\n");

??? while(p!=h)

??? {

??????? printf("%c?? ",p->data);

??????? p=p->next;

??? }

}

?

voidGetElem(node*h,int i)

{

??? int j=0;

??? node*p=null;

??? if(h->next!=null)

??????? printf("list is empty\n");

??? p=h->next;

??? while(p!=h)

??? {

??????? j++;

??????? p=p->next;

??? }

??? if(h==p)

??????? printf("cant get theelem!\n");

??? else

??? {

??????? printf("the elemis%c\n",p->data);

??? }

???

}

?

voidLocate(node*h,char e)

{

??? node*p=h->next;

??? int i=0;

??? while(p!=h && p->data!=e)

??? {

??????? p=p->next;

??????? i++;

??? }

??? if(p==h)

??????? printf("cant do it !\n");

??? else

??????? printf("the number of %c is%d\n",e,i);

}

插入

voidInsert(node*h,int i,char e)//插入節點就兩句話

{

??? int j=0;

??? node*p=h;

??? node*s=null;

??? while(p!=h&&j<i-1)

??? {

??????? j++;

??????? p=p->next;

??? }

??? if(p==h)

??? {

??????? printf("未找到第%d個節點\n",i-1);//未找到地i-1個節點

??? }

??? else

??? {

??????? s=(node*)malloc(sizeof(node));

??????? s->data=e;

??????? s->next=p->next;//1

??????? p->next=s;//2

??? }

}

刪除

void del(node*&h,int i)

{

??? int j=0;

??? node*p=h;

??? node *q=null;

?????? if(p->next==null)

????????????? {

???????????????????? printf("List isempty");

???????????????????? return 0;

????????????? }??? if

??? while(j<i-1&&p->next!=h)

??? {

??????? j++;

??????? p=p->next;

??? }

??? if(p->next==h)

??? {

??????? printf("未找到地%d個節點\n",i-1);

??? }

??? else

??? {

??????? q=p->next;//q指向要刪除的節點

??????? p->next=q->next;//0

??????? free(q);

??? }

}

?

//棧是一個簡單的鏈表,只有push(),pop(),top(),display()幾個操作

#include<stdio.h>

#include<malloc.h>

#define NULL0

typedef struct stack

{

????? int data;

????? struct stack *next;???

}node;

?

voidInitStack(node* &h)

{

???? h=(node*)malloc(sizeof(node));

???? h->next=NULL;

}

棧長(跟鏈表一樣)

intLength(node* h)

{

?????? int i=0;

?????? node* p=h;

?????? while(p->next)

?????? {

????????????? i++;

?????? ? ??? p=p->next;

?????? }

?????? //printf("鏈表的長度為%d\n",i);

?????? return i;

}

Push

void Push(node* &h,int e)//實際上就是在第一個節點前插入數據

{??????????????????? //處理h后的一個節點

?????? node* s;

?????? s=(node*)malloc(sizeof(node));

?????? s->data=e;

?????? s->next=h->next;

?????? h->next=s;

}

Pop

intpop(node* &h)//實際上就是刪除第一個節點

{???????????? //處理h后的一個節點

?????? node* p=h;

?????? int e;

?????? if(p->next==0)

????????????? {

???????????????????? printf("Stack isempty!\n");

???????????????????? return 0;

????????????? }

?????? p=p->next;//指向第一個節點

?????? //e=p->data;

?????? h->next=p->next;

?????? free(p);

?????? //printf("彈出的元素是:%d\n",e);

?????? return 1;

}

Top

intTop(node*h)//實際上就是讀取第一個數據

{

?????? node*p=h;

?????? int e;

?????? if(p->next==NULL)

????????????? {

???????????????????? printf("Stack isempty");

???????????????????? return 0;

????????????? }

?????? e=p->next->data;

?????? printf("頂點的數為%d\n",e);

?????? return e;

}

打印(跟鏈表一樣)

voiddispaly(node* h)

{

?????? node *p=h->next;

?????? if(p==NULL)

????????????? {

???????????????????? printf("Stack isempty!\n");

???????????????????? return;

????????????? }

?????? while(p!=NULL)//這里比較的不是p->next;

?????? {?????//每個元素都可以處理到,循環n次處理n個元素

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

????????????? p=p->next;

?????? }

?????? printf("\n");

}

?

voiddestroy(node*&h)???? // 釋放鏈表

{

?????? node*p=h;

?????? node*q=p->next;

?????? while(q!=NULL)

?????? {

????????????? free(p);

????????????? p=q;

????????????? q=q->next;

?????? }

?????? free(p);

}

?

?

?

/*

隊列

//隊列,就是enqueue(),front(),dequeue(),display()一些函數

//用鏈表來表示

?

#include<stdio.h>

#include<malloc.h>

#define NULL0

?

typedefstruct student

{

?????? int data;

?????? struct student *next;

}node;

?

typedef struct linkqueue

{

?????? node*front;

?????? node*rear;

}queue;

?

?

void Init(queue*&q)

{

?????? q=(queue*)malloc(sizeof(queue));

?????? q->front=q->rear=NULL;

}

測長

int Length(queue*h)

{

?????? inti=0;

?????? node*p=h->front;//賦值有少許變化,把第一個元素的地址發給了p

?????? while(p)??????? //pi不同步了

?????? {

????????????? i++;

????????????? p=p->next;

?????? }

?????? returni;

}

?

int Empty(queue*h)

{

?????? if(h->rear==NULL)

????????????? return 1;

?????? else

????????????? return 0;

}

入隊

queue*enQueue(queue *&h,inte)//想象成人們在排隊

{

?????? node *s;

?????? s=(node*)malloc(sizeof(node));

?????? s->data=e;

?????? s->next=NULL;//插入一個元素,排在最后,從后面插入

?????? if(h->rear==NULL)//若隊列為空

????????????? h->front=h->rear=s;

?????? else

?????? {

??????? node *b=h->rear;

????????????? b->next=s;//h->rear->next=s;

????????????? h->rear=s;

????????????? }

?????? return h;

}

出隊

intdeQueue(queue *&h)

{

?????? node *t;

?????? if(h->rear==NULL)

????????????? {

???????????????????? printf("Queue isempty!");

???????????????????? return 0;

??????? }

?????? if(h->front==h->rear)//隊列中只有一個節點時

????????????? {

???????????????????? t=h->front;

???????????????????? h->front=h->rear=NULL;

????????????? }

?????? else

????????????? {

???????????????????? t=h->front;

???????????????????? h->front=t->next;

????????????? }

?????? free(t);

?????? return 1;

}

?

void Clear(queue*&h)

{

?????? node *p=h->front;//p指向第一個節點

?????? node *r=NULL;

?????? if(p)

????????????? {

???????????????????? r=p->next;

???????????????????? while(r!=NULL)

???????????????????? {

??????????????????????????? free(p);

??????????????????????????? p=r;

??????????????????????????? r=p->next;

???????????????????? }

????????????? }

?????? free(h);

}

打印

voiddisplay(queue *h)

{

?????? node* p=h->front;//p指向第一個節點

?????? if(h->rear==NULL)

?????? ???printf("Queue is empyt!\n");

?????? while(p)

?????? {

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

????????????? p=p->next;

?????? }

?????? printf("\n");

}

約瑟夫問題(猴子選大王)

循環鏈表C語言實現

Slyar2009.3.31

http://www.slyar.com

*/

?

#include<stdio.h>

#include<stdlib.h>

?

/* 定義鏈表節點類型 */

typedefstruct node

{

??? int data;

??? struct node *next;

}linklist;

?

int main()

{

??? int i, n, k, m, total;

??? linklist *head, *p, *s, *q;

??? /* 讀入問題條件 */

??? printf("請輸入猴子的個數:");

??? scanf("%d", &n);

??? printf("請輸入要從第幾個猴子開始報數:");

??? scanf("%d", &k);

??? printf("請輸入出局數字:");

??? scanf("%d", &m);

??? /* 創建循環鏈表,頭節點也存信息 */

??? head = (linklist*)malloc(sizeof(linklist));

??? p = head;

??? p->data = 1;

??? p->next = p;

??? /* 初始化循環鏈表 */

??? for (i = 2; i <= n; i++)

??? {

??????? s = (linklist*)malloc(sizeof(linklist));

??????? s->data = i;

??????? s->next = p->next;

??????? p->next = s;

??????? p = p->next;

??? }

??? /* 找到第 k 個節點 */

? ??p =head;

??? for (i = 1; i < k; i++)

??? {

??????? p = p->next;

??? }

??? /* 保存節點總數 */

??? total = n;

??? printf("\n出局序列為:");

??? q = head;

??? /* 只剩一個節點時停止循環 */

??? while (total != 1)

??? {

??????? /* 報數過程,p指向要刪除的節點 */

??????? for (i = 1; i < m; i++)

??????? {

??????????? p = p->next;

??????? }

??????? /* 打印要刪除的節點序號 */

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

??????? /* q 指向 p 節點的前驅 */

??????? while (q->next != p)

??????? {

??????????? q = q->next;

??????? }

??????? /* 刪除 p 節點 */

??????? q->next = p->next;

?? ?????/* 保存被刪除節點指針 */

??????? s = p;

??????? /* p 指向被刪除節點的后繼 */

??????? p = p->next;

??????? /* 釋放被刪除的節點 */

??????? free(s);

??????? /* 節點個數減一 */

??????? total--;

??? }

??? /* 打印最后剩下的節點序號 */

??? printf("\n\n猴子大王為第 [%d] 號\n\n", p->data);

??? free(p);

??? //system("pause");

??? return 0;

}

?

總結

以上是生活随笔為你收集整理的数据机构-四种链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。