//鏈表的基本操含有對鏈表的建立,插入,刪除,查詢,輸出等基本操作
/在此特別提醒鏈表的基礎尤為重要,在這里重寫此代碼能夠更加深刻的理解了鏈表的空間的申請,
/頭節點的建立以及頭指針的重要和尾節點存在的必要性
/這個源代碼以鏈表的創建為核心,為了怕麻煩就沒有進行優化,但是創建和輸出是這個代碼的核心。
/希望這個能幫助到初學者,在這有不懂得地方可以看我的隨筆,鏈表的整表的創建,那里有更加詳細的介紹
/在此附上圖:
一、初始化界面
二、創建鏈表
三、輸出所創建的鏈表中元素
源代碼:
//先建立一個完整的鏈表,然后對其進行增刪改查
//在這里我們采用尾插入的方式來對其進行建立鏈表
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
using namespace std;
typedef int DataType;
//聲明節點
typedef struct node{
DataType data;
struct node *next;
}SLNode;
//創建頭指針
SLNode *InitiateHead(SLNode*phead){
phead = (SLNode*)malloc(sizeof(SLNode));
phead->next = NULL;
return phead;
}
//創建鏈表
SLNode * Create_List(SLNode *head){//頭節點開始的申請空間
SLNode *r,*p;
int x;
char c ;
if(head !=NULL){
free(head);
head = NULL;
}
head = (SLNode*)malloc(sizeof(SLNode));
head->data=x;
if(head !=NULL){
r = head;
cout<<"空間成功申請!"<<endl;
}
cout<<"創建鏈表:"<<endl;
while(c !=NULL||c!='n'){
cout<<"請輸入:";cin>>x;
p = (SLNode*)malloc(sizeof(SLNode));
p->data = x;
r->next = p;
r = p;
cout<<"是否繼續輸入:Y/N"<<endl;
cin>>c;
if(c == 'Y'||c == 'y'){
continue;
}else{
r->next= NULL;
cout<<"首地址1:"<<head<<endl;///
cout<<"鏈表創建完成!"<<endl;
return head;
}
}
}
//鏈表的插入
int ListInsert(SLNode *head,int i ,DataType x){
SLNode *p,*q;
p = head->next;
int j = 0;
while(p->next!= NULL && j < i-1){
p = p->next;
j++;
}
if(j != i -1){
cout<<"插入位置參數錯誤!"<<endl;
return 0;
}
q = (SLNode *)malloc(sizeof(SLNode));
q->data= x;
q->next= p->next;
p->next = q;
cout<<"插入成功 !"<<endl;
return 1;
}
//鏈表的刪除
int ListDelete(SLNode *head,int i ,DataType *x){
SLNode *p ,*s;
int j;
p = head->next;
j = 0;
while( p ->next !=NULL && j<i -1){
p = p->next;
j++;
}
if(j != i -1){
cout <<"刪除位置參數錯誤!"<<endl;
return 0;
}
s = p->next;
*x = s->data;
p->next = p->next->next;
free(s);
cout<<"刪除成功!"<<endl;
return 1;
}
//查詢獲取元素
int ListGet(SLNode *head,int i ,DataType *x){
SLNode *p ;
int j;
p = head->next;
j = 0;
while(p->next!= NULL &&j <i){
p = p->next;
j++;
}
if(j != i){
cout<<"取元素 位置參數錯誤!"<<endl;
return 0;
}
*x = p->data;
cout<<"查找成功!"<<endl;
return 1;
}
void OutPut_List(SLNode *phead){
SLNode *p;
// cout<<"首地址3:"<<phead<<endl;///
//cout<<"首地址4:"<<phead->next<<endl;
p = phead->next->next;
//cout<<"data:"<<p->data<<endl;
int i = 1;
while(p!=NULL){
cout<<"第"<<i<<"一個元素:"<<p->data<<endl;
i++;
p = p->next;
}
}
//釋放內存,避免內存的泄漏
void Destroy(SLNode *head){
SLNode *p = head ;
SLNode *p1;
while(p != NULL){
p1 = p;
p = p->next;
free(p1);
}
head = NULL;
cout<<"釋放內存成功!"<<endl;
}
void menu(){
cout<<"_________________________________________________________"<<endl;
cout<<"|*******************************************************|"<<endl;
cout<<"|*****************歡迎進入菜單選項*.********************|"<<endl;
cout<<"|*****************進入初始化階段請稍后....**************|"<<endl;
cout<<"|*****************0、清屏 **************|"<<endl;
cout<<"|*****************1、創建鏈表 **************|"<<endl;
cout<<"|*****************2、輸出鏈表 **************|"<<endl;
cout<<"|*****************3、鏈表的插入 **************|"<<endl;
cout<<"|*****************4、鏈表的刪除 **************|"<<endl;
cout<<"|*****************5、鏈表的查詢 **************|"<<endl;
cout<<"|*****************6、釋放內存空間 **************|"<<endl;
cout<<"|*******************************************************|"<<endl;
cout<<"_________________________________________________________"<<endl;
}
void operation(SLNode *phead){
SLNode *p,*p1;
int n,i;
int input;
cout<<"請選擇:"<<endl;
cin>>input;
//p1=InitiateHead(phead);//頭指針
switch(input){
case 0:system("CLS");menu();operation(phead);
case 1:p1=Create_List(p);
/* cout<<"首地址2:"<<p1<<endl;//此為創建鏈表后返回的頭節點的地址,頭節點不存任何東西*/
phead->next=p1; phead->next=p1;menu();operation(phead); break;
case 2:cout<<"輸出已創建好的鏈表中的數據:"<<endl;OutPut_List(phead);menu();operation(phead);break;
case 3:cout<<"請輸入要插入的元素:";cin>>n;cout<<endl;cout<<"請輸入要插入的位置";cin>>i;
ListInsert(phead,i ,n);menu();operation(phead);menu();operation(phead);break;
case 4:cout<<"請輸入要刪除的元素:";cin>>n;cout<<endl;cout<<"請輸入要刪除的位置";cin>>i;
ListDelete(phead,i ,&n);menu();operation(phead);menu();operation(phead);break;
case 5:cout<<"請輸入要查詢的元素:";cin>>n;cout<<endl;cout<<"請輸入要查詢的位置";cin>>i;
ListGet(phead,i ,&n);menu();operation(phead);menu();operation(phead);break;
case 6:cout<<"釋放存儲空間:"; Destroy(phead);break;
default :cout<<"請重新選擇!"<<endl; menu();operation(phead);
}
}
int main(){
SLNode *phead;
phead=InitiateHead(phead);
cout<<"首地址2.1:"<<phead<<endl;//此為頭指針的地址
menu();
operation(phead);
return 0;
}