strUCt test
{
char name[10];
float socre;
test *next;
};//這樣是正確的!
struct test
{
char name[10];
float socre;
test next;
};//這樣是錯誤的!
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
struct test
{
char name[10];
float socre;
test *next;
};
test *head;//創建一個全局的引導進入鏈表的指針
test *create()
{
test *ls;//節點指針
test *le;//鏈尾指針
ls = new test;//把ls指向動態開辟的堆內存地址
cin>>ls->name>>ls->socre;
head=NULL;//進入的時候先不設置head指針指向任何地址,因為不知道是否一上來就輸入null跳出程序
le=ls;//把鏈尾指針設置成剛剛動態開辟的堆內存地址,用于等下設置le->next,也就是下一個節點的位置
while(strcmp(ls->name,"null")!=0)//創建循環條件為ls->name的值不是null,用于循環添加節點
{
if(head==NULL)//判定是否是第一次進入循環
{
head=ls;//假如是第一次進入循環,那么把引導進入鏈表的指針指向第一次動態開辟的堆內存地址
}
else
{
le->next=ls;//假如不是第一次進入那么就把上一次的鏈尾指針的le->next指向上一次循環結束前動態創建的堆內存地址
}
le=ls;//設置鏈尾指針為當前循環中的節點指針,用于下一次進入循環的時候把上一次的節點的next指向上一次循環結束前動態創建的堆內存地址
ls=new test;//為下一個節點在堆內存中動態開辟空間
cin>>ls->name>>ls->socre;
}
le->next=NULL;//把鏈尾指針的next設置為空,因為不管如何循環總是要結束的,設置為空才能夠在循環顯鏈表的時候不至于死循環
delete ls;//當結束的時候最后一個動態開辟的內存是無效的,所以必須清除掉
return head;//返回鏈首指針
}
void showl(test *head)
{
cout<<"鏈首指針:"<<head<<endl;
while(head)//以內存指向為null為條件循環顯示先前輸入的內容
{
cout<<head->name<<""<<head->socre<<endl;
head=head->next;
}
}
void main()
{
showl(create());
cin.get();
cin.get();
}
test *head;//創建一個全局的引導進入鏈表的指針
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
struct test
{
char name[10];
float socre;
test *next;
};
test *head;//創建一個全局的引導進入鏈表的指針
test *create()
{
return head;//返回鏈首指針
}
void showl(test *head)
{
cout<<"鏈首指針:"<<head<<endl;
while(head)//以內存指向為null為條件循環顯示先前輸入的內容
{
cout<<head->name<<""<<head->socre<<endl;
head=head->next;
}
}
void main()
{
showl(create());
cin.get();
cin.get();
}
test *create()
{
test *ls;//節點指針
test *le;//鏈尾指針
ls = new test;//把ls指向動態開辟的堆內存地址
cin>>ls->name>>ls->socre;
head=NULL;//進入的時候先不設置head指針指向任何地址,因為不知道是否一上來就輸入null跳出程序
le=ls;//把鏈尾指針設置成剛剛動態開辟的堆內存地址,用于等下設置le->next,也就是下一個節點的位置
le->next=NULL;//把鏈尾指針的next設置為空,因為不管如何循環總是要結束的,設置為空才能夠在循環顯鏈表的時候不至于死循環
delete ls;//當結束的時候最后一個動態開辟的內存是無效的,所以必須清除掉
return head;//返回鏈首指針
}
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
struct test
{
int number;
float socre;
test *next;
};
test *head;//創建一個全局的引導進入鏈表的指針
test *create()
{
test *ls;//節點指針
test *le;//鏈尾指針
ls = new test;//把ls指向動態開辟的堆內存地址
cin>>ls->number>>ls->socre;
head=NULL;//進入的時候先不設置head指針指向任何地址,因為不知道是否一上來就輸入null跳出程序
le=ls;//把鏈尾指針設置成剛剛動態開辟的堆內存地址,用于等下設置le->next,也就是下一個節點的位置
while(ls->number!=0)//創建循環條件為ls->number的值不是null,用于循環添加節點
{
if(head==NULL)//判定是否是第一次進入循環
{
head=ls;//假如是第一次進入循環,那么把引導進入鏈表的指針指向第一次動態開辟的堆內存地址
}
else
{
le->next=ls;//假如不是第一次進入那么就把上一次的鏈尾指針的le->next指向上一次循環結束前動態創建的堆內存地址
}
le=ls;//設置鏈尾指針為當前循環中的節點指針,用于下一次進入循環的時候把上一次的節點的next指向上一次循環結束前動態創建的堆內存地址
ls=new test;//為下一個節點在堆內存中動態開辟空間
cin>>ls->number>>ls->socre;
}
le->next=NULL;//把鏈尾指針的next設置為空,因為不管如何循環總是要結束的,設置為空才能夠在循環顯鏈表的時候不至于死循環
delete ls;//當結束的時候最后一個動態開辟的內存是無效的,所以必須清除掉
return head;//返回鏈首指針
}
void showl(test *head)
{
cout<<"鏈首指針:"<<head<<endl;
while(head)//以內存指向為null為條件循環顯示先前輸入的內容
{
cout<<head->number<<""<<head->socre<<endl;
head=head->next;
}
}
void deletel(test *&head,int number)//這里假如參數換成test *head,意義就完全不同了,head變成了復制而不是原有鏈上操作了,非凡注重,很多書上都不對這里
{
test *point;//判定鏈表是否為空
if(head==NULL)
{
cout<<"鏈表為空,不能進行刪除工作!";
return;
}
if(head->number==number)//判刪除的節點是否為首節點
{
point=head;
cout<<"刪除點是鏈表第一個節點位置!";
head=head->next;//重新設置引導指針
delete point;
return;
}
test *fp=head;//保存連首指針
for(test *&mp=head;mp->next;mp=mp->next)
{
if(mp->next->number==number)
{
point=mp->next;
mp->next=point->next;
delete point;
head=fp;//由于head的不斷移動丟失了head,把進入循環前的head指針恢復!
return;
}
}
}
void main()
{
head=create();//調用創建
showl(head);
int dp;
cin>>dp;
deletel(head,dp);//調用刪除
showl(head);
cin.get();
cin.get();
}
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
struct test
{
int number;
float socre;
test *next;
};
test *head;//創建一個全局的引導進入鏈表的指針
test *create()
{
test *ls;//節點指針
test *le;//鏈尾指針
ls = new test;//把ls指向動態開辟的堆內存地址
cout<<"請輸入第一個節點number和節點score,輸入0.0跳出函數"<<endl;
cin>>ls->number>>ls->socre;
head=NULL;//進入的時候先不設置head指針指向任何地址,因為不知道是否一上來就輸入null跳出程序
le=ls;//把鏈尾指針設置成剛剛動態開辟的堆內存地址,用于等下設置le->next,也就是下一個節點的位置
while(ls->number!=0)//創建循環條件為ls->number的值不是null,用于循環添加節點
{
if(head==NULL)//判定是否是第一次進入循環
{
head=ls;//假如是第一次進入循環,那么把引導進入鏈表的指針指向第一次動態開辟的堆內存地址
}
else
{
le->next=ls;//假如不是第一次進入那么就把上一次的鏈尾指針的le->next指向上一次循環結束前動態創建的堆內存地址
}
le=ls;//設置鏈尾指針為當前循環中的節點指針,用于下一次進入循環的時候把上一次的節點的next指向上一次循環結束前動態創建的堆內存地址
ls=new test;//為下一個節點在堆內存中動態開辟空間
cout<<"請下一個節點number和節點score,輸入0.0跳出函數"<<endl;
cin>>ls->number>>ls->socre;
}
le->next=NULL;//把鏈尾指針的next設置為空,因為不管如何循環總是要結束的,設置為空才能夠在循環顯鏈表的時候不至于死循環
delete ls;//當結束的時候最后一個動態開辟的內存是無效的,所以必須清除掉
return head;//返回鏈首指針
}
void showl(test *head)
{
cout<<"鏈首指針:"<<head<<endl;
while(head)//以內存指向為null為條件循環顯示先前輸入的內容
{
cout<<head->number<<""<<head->socre<<endl;
head=head->next;
}
}
void deletel(test *&head,int number)//這里假如參數換成test *head,意義就完全不同了,head變成了復制而不是原有鏈上操作了,非凡注重,很多書上都不對這里
{
test *point;//判定鏈表是否為空
if(head==NULL)
{
cout<<"鏈表為空,不能進行刪除工作!";
return;
}
int derror=1;//設置找不到的情況的條件,預先設置為真
test *check=head;
while(check)//利用循環進行查找
{
if (check->number==number)
{
derror=0;//條件轉為假
}
check=check->next;
}
if(derror)//假如為假就跳出函數
{
return;
}
if(head->number==number)//判刪除的節點是否為首節點
{
point=head;
cout<<"刪除點是鏈表第一個節點位置!";
head=head->next;//重新設置引導指針
delete point;
return;
}
test *fp=head;//保存連首指針
for(test *&mp=head;mp->next;mp=mp->next)
{
if(mp->next->number==number)
{
point=mp->next;
mp->next=point->next;
delete point;
head=fp;//由于head的不斷移動丟失了head,把進入循環前的head指針恢復!
return;
}
}
}
void insterl(int number)
{
test *point=new test;
cout<<"請輸入節點number和節點score"<<endl;
cin>>point->number>>point->socre;
if(head==NULL)//鏈表為空的情況下插入
{
head=point;
point->next=NULL;
return;
}
int ierror=1;//設置找不到的情況的條件,預先設置為真
test *le;
test *check=head;
while(check)//利用循環進行查找
{
if (check->number==number)
{
ierror=0;//條件轉為假
}
le=check;
check=check->next;
}
if(ierror)
{
cout<<le->number;
le->next=point;
point->next=NULL;
return;
}
if(head->number==number)//檢測是否是在第一個節點處插入
{
point->next=head;
head=point;
return;
}
for(test *&mp=head;mp->next;mp=mp->next)//在鏈表中間插入
{
if(mp->next->number==number)
{
point->next=mp->next;
mp->next=point;
return;
}
}
}
void main()
{
head=create();//調用創建
showl(head);
int dp;
cout<<"請輸入刪除點假如找不到就跳出函數"<<endl;
cin>>dp;
deletel(head,dp);//調用刪除
showl(head);
int ip;
cout<<"請輸入插入點假如找不到就在鏈尾添加"<<endl;
cin>>ip;
insterl(ip);
showl(head);
cin.get();
cin.get();
}
新聞熱點
疑難解答