當前位置:首頁 » 操作系統 » 鏈式演算法

鏈式演算法

發布時間: 2023-07-14 01:52:03

㈠ 線性表鏈式存儲結構的基本操作演算法實現

這是我學數據結構時親手碼的,好用的話記得給分。。。。。
#include<iostream>
using namespace std;

//線性表的單鏈表存儲表示
struct LNode
{
int data;
struct LNode *next;
};

//逆序創建鏈表
void CreateList(LNode *L,int a[],int n)
{
LNode *s;
L->next = NULL;
for(int i=n-1;i>=0;--i)
{
s= new LNode;
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
//顯示鏈表
void display(LNode *L)
{
LNode *p;
p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
//插入結點操作
void ListInsert(LNode *L,int d, LNode *s)
{
LNode *p;
int i=1;
p=L->next;
while(i<d-1)
{
p=p->next;
i++;
}
s->next=p->next;
p->next=s;
}
//刪除節點操作
void ListDelete(LNode *L,int d,int &e)
{
LNode *p,*q;
int i=0;
p=L->next ;
while(i<d-1)
{
q=p;
p=p->next;
i++;
}
e=p->data;
q->next=p->next;
delete p;
}
//查找元素
LNode* LocalElem(LNode *L,int e)
{
LNode *p;
p=L->next;
while(p&&p->data!=e) p=p->next;
return p;
}
//逆置單鏈表
void InvertLinkedList(LNode *L)
{
LNode *p,*s;
p=L->next;
L->next=NULL;
while(p)
{
s=p;
p=p->next;
s->next=L->next;
L->next=s;
}
}

int main()
{
LNode *H;
int n;
cout<<"please enter the length of the list: ";
cin>>n;
int *A=new int[n];
int i;
H=new LNode;
H->next=NULL;
cout<<"please enter "<<n<<" number of the list:"<<endl;
for(i=0;i<n;i++)
cin>>A[i];
CreateList(H,A,n);
cout<<"show the members of the list: ";
display(H);
cout<<endl;

int d;
cout<<"please enter the address of the add member: ";
cin>>d;
LNode *s;
s= new LNode;//初始化局部變數s
cout<<"please enter the data of the add member: ";
cin>>s->data;
ListInsert(H,d, s);
display(H);
cout<<endl;

int p;
cout<<"please enter the address of the delete member: ";
cin>>p;
int c=0;
int &e=c;//非常量引用的初始值必須為左值!!!
ListDelete(H,p,e);
display(H);
cout<<endl;

int g;
cout<<"please enter the member that you want to find: ";
cin>>g;
cout<<"the local of the member is: "<<LocalElem(H,g);
cout<<endl;

InvertLinkedList(H);
cout<<"the invertlinklist is: ";
display(H);
cout<<endl;
return 0;
}

花時間好好看看,一定要看懂

㈡ 編寫一個鏈式隊列進隊演算法enQueue(q,e),並為每條語句添加一個注釋,解釋該語句的功能和作用

//既然是演算法
就不用源碼
具體看注釋
typedef
int
datatype;
typedef
struct
queuenode
{
datatype
data;
struct
queuenode
*next;
}queuenode;
//以上是結點類型的定義
typedef
struct
{
queuenode
rear;
}linkqueue;
//只設一個指向隊尾元素的指針
void
initqueue(
linkqueue
&q)
{
//置空隊:就是使頭結點成為隊尾元素
q.rear=(queuenode*)malloc(sizeof(queuenode))
queuenode*
s;
q->rear
=
q->rear->next;//將隊尾指針指向頭結點
while(q->rear!=q->rear->next)
//當隊列非空,將隊中元素逐個出隊
{
s=q->rear->next;
q->rear->next=s->next;
free(s);
}
//回收結點空間
}
int
emptyqueue(
linkqueue
&q)
{
//判隊空
//當頭結點的next指針指向自己時為空隊
return
q->rear->next->next==q->rear->next;
}
void
enqueue(
linkqueue
&q,
datatype
x)
{
//入隊
//也就是在尾結點處插入元素
queuenode
*p=(queuenode
*)
malloc
(sizeof(queuenode));//申請新結點
p->data=x;
p->next=q->rear->next;//初始化新結點並鏈入
q-rear->next=p;
q->rear=p;//將尾指針移至新結點
}
datatype
dequeue(
linkqueue
&q,datatype
&x)
{
//出隊,把頭結點之後的元素摘下
datatype
t;
queuenode
*p;
if(emptyqueue(
q
))
error("queue
underflow");
p=q->rear->next->next;
//p指向將要摘下的結點
x=p->data;
//保存結點中數據
if
(p==q->rear)
{
//當隊列中只有一個結點時,p結點出隊後,要將隊尾指針指向頭結點
q->rear
=
q->rear->next;
q->rear->next=p->next;
}
else
q->rear->next->next=p->next;//摘下結點p
free(p);//釋放被刪結點
return
x;
}

㈢ 數據結構鏈式存儲隊列中刪除元素的演算法

給你一個全的吧
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct LNode
{
int Data;
struct LNode *Next;
}LNode;

void CreateList(struct LNode *HeadList,int n,int a[])
{
int i;
struct LNode *p;
HeadList=(struct LNode*)malloc(sizeof(struct LNode));
HeadList->Next=NULL;
for(i=n;i>0;--i)
{
p=(struct LNode*)malloc(sizeof(struct LNode));
p->Data=a[i];
p->Next=HeadList->Next;
HeadList->Next=p;
}
}
void ListInsert(struct LNode *HeadList,int i,int newnode)
{
struct LNode *p=HeadList;
struct LNode *s;
int j=0;
while(p && j<i-1)
{
p=p->Next;
j++;
}
if(!p || j>i-1)
{
cout<<"位置小於1或大於表長";
return;
}
s=(struct LNode*)malloc(sizeof(struct LNode));
s->Data=newnode;
s->Next=p->Next;
p->Next=s;
}
void ListDelete(struct LNode *HeadList,int i)
{
struct LNode *p=HeadList;
struct LNode *s;
int j=0;
while(p->Next && j<i-1)
{
p=p->Next;
++j;
}
if(!(p->Next) || j>i-1)
{
cout<<"刪除位置不合理";
return;
}
s=p->Next;
p->Next=s->Next;
int number;
cin>>number;
s->Data=number;
cout<<"被刪除的結點是"<<s->Data;
free(s);
}
void ListDisp(struct LNode *HeadList)
{
struct LNode *p;
int i=0;
p=HeadList->Next;
while(p)
{
cout<<p->Data;
++i;
p=p->Next;
}
}
int getoptions()
{
int opt;
cout<<"1: 錄入鏈表"<<endl;
cout<<"2: 顯示鏈表"<<endl;
cout<<"3: 插入結點"<<endl;
cout<<"4: 刪除結點"<<endl;
cout<<"5: 退出"<<endl;
cout<<"輸入選項並按回車確認:";
cin>>opt;
return opt;
}
int main(int argc,char * argv[])
{
int opt;
int where;
int value;
int count;
int a[5]={2,4,6,8,10};
struct LNode *HeadList;
while(1)
{
opt=getoptions();
if(opt==1)
{
//cout<<"請輸入鏈表的初始結點數";
//cin>>count;
//cout<<"請輸入各個結點數值,每輸入一個按回車確認";
CreateList(HeadList,5,a);
ListDisp(HeadList);
system("PAUSE");
continue;
}
if(opt==2)
{
ListDisp(HeadList);
system("PAUSE");
continue;
}
if(opt==3)
{
ListDisp(HeadList);
cin>>where;
cout<<"請輸入要插入的數值";
cin>>value;
ListInsert(HeadList,where,value);
ListDisp(HeadList);
system("PAUSE");
continue;
}
if(opt==4)
{
ListDisp(HeadList);
cout<<"請輸入要刪除的位置";
cin>>where;
ListDelete(HeadList,where);
ListDisp(HeadList);
system("PAUSE");
continue;
}
if(opt==5)
{
return 0;
}
}
}
不好意思,我發完之後才看見是隊列,乍一看以為是鏈表,我發的這個是鏈表的,快下班了,明天上班再給你寫一個吧

熱點內容
編程右交換 發布:2025-03-16 15:28:43 瀏覽:399
根號的除法運演算法則 發布:2025-03-16 15:26:35 瀏覽:763
冰箱壓縮機照片 發布:2025-03-16 15:25:34 瀏覽:881
博雅象棋伺服器地址 發布:2025-03-16 15:02:26 瀏覽:816
如何實現職業化配置管理 發布:2025-03-16 14:55:41 瀏覽:967
一編程就頭疼 發布:2025-03-16 14:39:25 瀏覽:502
如何連接自己的個人伺服器 發布:2025-03-16 14:33:14 瀏覽:747
安卓緩存照片進相冊里怎麼取消 發布:2025-03-16 14:33:08 瀏覽:738
a站怎麼緩存 發布:2025-03-16 14:31:28 瀏覽:991
javascriptdes演算法 發布:2025-03-16 14:23:57 瀏覽:320