线性表链式存储结构的基本操作算法实现
库函数和常量定义:#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;};Nod...
库函数和常量定义:
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
Node* Init_LinkList()
{
Node *h = new Node;
h->next=NULL;
h->data=-1;
return h;
}
(1)创建带头结点链式线性表算法
void Create_LinkListT(Node* h)
{
}
(2)在带头结点线性表的第i个位置前插入元素pv算法
void insert_before(Node* h, int v, int pv)
//在带头结点的单链表h中第v个位置之前插入元素pv。
{
}
(3)在带头结点线性表的第i个位置后插入元素pv算法
void insert_after(Node* h, int v, int pv)
//在带头结点的单链表h中第v个位置之后插入元素pv。
{
}
(4)遍历线性表元素算法
void Print_LinkList(Node* h)
/*遍历带头结点的链表*/
{
}
(5)返回线性链表h中第1个与d元素相同的位置。
int search_link(Node* h, int d)
{
}
int main()
{
Node *h = Init_LinkList();
char choice;
//Print_LinkList(h);
while(true)
{
cout<<"please input the choice"<<endl;
cout<<"s for search "<<endl;
cout<<"c for create link list"<<endl;
cout<<"p for print link list"<<endl;
cout<<"i for insert after"<<endl;
cout<<"a for insert before"<<endl;
cout<<"q for exit"<<endl;
cin>>choice;
switch(choice)
{
case 's':
cout<<"请输入你要查找的值"<<endl;
int d;
cin>>d;
cout<<"第"<<search_link(h, d)<<"位置"<<endl;
break;
case 'c':
Create_LinkListT(h);
break;
case 'p':
Print_LinkList(h);
break;
case 'i':
cout<<"请输入你要插入节点的值"<<endl;
int v;
cin>>v;
cout<<"请输入你要插入在哪个借点的后面"<<endl;
int pv;
cin>>pv;
insert_after(h,v,pv);
break;
case 'a':
cout<<"请输入你要插入节点的值"<<endl;
//int v;
cin>>v;
cout<<"请输入你要插入在哪个借点的后面"<<endl;
//int pv;
cin>>pv;
insert_before(h,v,pv);
break;
case 'q':
delete h;
return 0;
}
} 展开
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
Node* Init_LinkList()
{
Node *h = new Node;
h->next=NULL;
h->data=-1;
return h;
}
(1)创建带头结点链式线性表算法
void Create_LinkListT(Node* h)
{
}
(2)在带头结点线性表的第i个位置前插入元素pv算法
void insert_before(Node* h, int v, int pv)
//在带头结点的单链表h中第v个位置之前插入元素pv。
{
}
(3)在带头结点线性表的第i个位置后插入元素pv算法
void insert_after(Node* h, int v, int pv)
//在带头结点的单链表h中第v个位置之后插入元素pv。
{
}
(4)遍历线性表元素算法
void Print_LinkList(Node* h)
/*遍历带头结点的链表*/
{
}
(5)返回线性链表h中第1个与d元素相同的位置。
int search_link(Node* h, int d)
{
}
int main()
{
Node *h = Init_LinkList();
char choice;
//Print_LinkList(h);
while(true)
{
cout<<"please input the choice"<<endl;
cout<<"s for search "<<endl;
cout<<"c for create link list"<<endl;
cout<<"p for print link list"<<endl;
cout<<"i for insert after"<<endl;
cout<<"a for insert before"<<endl;
cout<<"q for exit"<<endl;
cin>>choice;
switch(choice)
{
case 's':
cout<<"请输入你要查找的值"<<endl;
int d;
cin>>d;
cout<<"第"<<search_link(h, d)<<"位置"<<endl;
break;
case 'c':
Create_LinkListT(h);
break;
case 'p':
Print_LinkList(h);
break;
case 'i':
cout<<"请输入你要插入节点的值"<<endl;
int v;
cin>>v;
cout<<"请输入你要插入在哪个借点的后面"<<endl;
int pv;
cin>>pv;
insert_after(h,v,pv);
break;
case 'a':
cout<<"请输入你要插入节点的值"<<endl;
//int v;
cin>>v;
cout<<"请输入你要插入在哪个借点的后面"<<endl;
//int pv;
cin>>pv;
insert_before(h,v,pv);
break;
case 'q':
delete h;
return 0;
}
} 展开
1个回答
展开全部
这是我学数据结构时亲手码的,好用的话记得给分。。。。。
#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;
}
花时间好好看看,一定要看懂
#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;
}
花时间好好看看,一定要看懂
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
彩驰科技
2024-11-22 广告
2024-11-22 广告
互联网算法备案平台,专业代理代办,快速响应,高效办理!专业代理代办,快速办理,让您省时省力!专业团队为您提供优质服务,让您的互联网算法备案更顺利!咨询电话:13426378072,13436528688...
点击进入详情页
本回答由彩驰科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询