数据机构-四种链表
單鏈表
?
雙鏈表
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)??????? //p跟i不同步了
?????? {
????????????? 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;
}
?
總結
- 上一篇: python每天学习30分钟系列
- 下一篇: java各种包的用途