亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如下情況,麻煩幫忙修改下:

如下情況,麻煩幫忙修改下:

收到一只叮咚 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個贊

這是我前幾天剛寫的,可能對你有幫助!(一個工程,自己看著操作吧?。?br/>文件名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 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號