?? 鏈表刪除.cpp
字號:
#include<iostream>
using namespace std;
#include<conio.h>
struct Lnode
{
int len;
double data;
Lnode *next;
};
Lnode *creatlnode()//創建鏈表
{
Lnode *h,*p,*q;
int n=0;
h=NULL;
cout<<"請輸入元素:";
p=new Lnode;
q=new Lnode;
cin>>p->data;
while(1)
{
n=n+1;
if(n==1)
h=p;
else
q->next=p;
q=p;
if(getchar()=='\n')
break;
p=new Lnode;
cin>>p->data;
}
q->next=NULL;
h->len=n;
return h;
}
void display(Lnode *L) //輸出鏈表
{
Lnode *p;
p=L;
cout<<"當前的鏈表為:";
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
Lnode *del(Lnode *L)//刪除鏈表中的節點
{
Lnode *p,*h;
p=L;
int del_num;
cout<<"輸入要刪除的元素:";
cin>>del_num;
if(del_num==L->data)
{
h=p;
p=p->next;
cout<<del_num<<"已刪除"<<endl;
return p;
}
while(p->data<del_num && p->next!=NULL)
{
h=p;
p=p->next;
}
if(p==NULL)
{
cout<<"鏈表為空"<<endl;
return L;
}
else if(p->data==del_num)
{
cout<<del_num<<"已刪除"<<endl;
h->next=p->next;
L->len=L->len-1;
return L;
}
else
cout<<"找不到要刪的元素"<<endl;
return L;
}
void main()
{
Lnode *L;
L=NULL;
L=creatlnode();
display(L);
L=del(L);
display(L);
cout<<endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -