为了账号安全,请及时绑定邮箱和手机立即绑定

如下情况,麻烦帮忙修改下:

如下情况,麻烦帮忙修改下:

收到一只叮咚 2022-08-04 11:07:01
1)单链表创建,插入节点的过程和遍历过程;2)实现在链表中查找指定元素;2)实现数据节点的删除操作,Int ListDelete_Link (LinkList L,int i,ElemType &e)3)实现数据节点的修改操作(modify),该函数带入两个参数,一个表示要修改节点的位置,另一个表示该节点的新值。#include <stdlib.h>#include <stdio.h>#include <malloc.h>struct LNode{int data;struct LNode *next;};typedef struct LNode *LinkList;ListInit_Link(LinkList L){L=(LinkList)malloc(sizeof(struct LNode));if(!L) exit(-1);L->next=NULL;return 1;}void CreateList_Link (LinkList &L,int n){int i; LinkList p,q;L=(LinkList)malloc(sizeof(struct LNode));L->next=NULL; q=L;printf(" 请输入%d 个数据\n",n);for(i=1;i<=n;i++){p=(LinkList)malloc(sizeof(struct LNode));scanf("%d",&p->data);q->next=p;q=q->next;}p->next=NULL;}int ListInsert_Link (LinkList L,int i,int e){int j=0;LinkList p=L,s;while(p&&j<i-1){p=p->next;j++;}if(!p||j>i-1)return 0;s=(LinkList)malloc(sizeof(struct LNode));s->data=e;s->next=p->next;p->next=s;return 1;}void ListTraverse_Link (LinkList L){LinkList p=L->next;while(p){printf("%d",p->data);p=p->next;}}
查看完整描述

3 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

这是我前几天刚写的,可能对你有帮助!(一个工程,自己看着操作吧!)
文件名h:
template <class T> struct node
{ T data;
node *next;
};
template <class T> class linked_Queue //链队列类
{ private: //数据成员
node<T> *front; //链队列队首指针
node<T> *rear; //链队列队尾指针
int n; //链队列长度为n
public: //成员函数
linked_Queue(); //构造函数,建立空队列,即队列初始化
void prt_linked_Queue(); //顺序输出链队列中的元素
int flag_linked_Queue(); //检测链队列的状态
void ins_linked_Queue(T); //入队
void del_linked_Queue(); //出队,并打印输出出队元素值
};

文件名.cpp
#include <iostream.h>
#include "zah.h"

template <class T> linked_Queue<T>::linked_Queue()
{
front = NULL;
rear = NULL;
n = 0;
}

template <class T> void linked_Queue<T>::prt_linked_Queue()
{
node<T> *p;
p=front;
while(p!=0)
{
cout<<p->data<<endl;
p=p->next;
}
cout<<endl;
}

template <class T> int linked_Queue<T>::flag_linked_Queue()
{
if(front==NULL)
return 0;
return 1;
}

template <class T> void linked_Queue<T>::ins_linked_Queue(T x) //入队
{
node<T> *p;
p=new node<T>;
p->data=x;
rear->next=p;
n++;
}

template <class T> void linked_Queue<T>::del_linked_Queue() //出队
{
if(flag_linked_Queue()==0)
cout<<"队列为空,无法输出!"<<endl;
node<T> *p;
p=front;
front=front->next;
cout<<p->data<<"出队"<<endl;
delete p;
n--;
}

void main()
{
linked_Queue<int> queue;
int x[5] = {1,2,3,4,5};
for(int i=0; i<5; i++)
queue.ins_linked_Queue(x[i]);
queue.prt_linked_Queue();
queue.del_linked_Queue();
queue.prt_linked_Queue();
queue.del_linked_Queue();
queue.del_linked_Queue();
queue.del_linked_Queue();
queue.prt_linked_Queue();
}


查看完整回答
反对 回复 2022-08-08
?
德玛西亚99

TA贡献1770条经验 获得超3个赞

示例如下:

  #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;
}


查看完整回答
反对 回复 2022-08-08
?
哔哔one

TA贡献1854条经验 获得超8个赞

是这个效果吗


查看完整回答
反对 回复 2022-08-08
  • 3 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信