当前位置:首页 » 操作系统 » 链式算法

链式算法

发布时间: 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 23:16:42 浏览:173
vcmfc源码 发布:2025-03-16 23:14:17 浏览:503
如何设置禁止访问服务器ip 发布:2025-03-16 23:14:07 浏览:498
linuxloadrunner 发布:2025-03-16 23:12:18 浏览:765
搭建fms服务器 发布:2025-03-16 23:11:27 浏览:978
代码编程图片 发布:2025-03-16 23:09:58 浏览:412
研发加密 发布:2025-03-16 23:09:51 浏览:609
哪些车有配置前后防撞钢梁 发布:2025-03-16 22:55:35 浏览:729
服务器怎么设置外网访问 发布:2025-03-16 22:53:03 浏览:185
安卓手机如何绕过缓存软件 发布:2025-03-16 22:35:16 浏览:243