單鏈表各種操作(頭插法建表、初始化、求表長、查找、刪除、插入、取值)17計科班教學用

#include<iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char ElemType;
typedef int Status;
typedef struct LNode
{
     ElemType   data;       //數據域
     struct LNode  *next;   //指針域
}LNode,*LinkList;  
//初始化鏈表 
Status InitList_L(LinkList &L){ 
L=new LNode;
L->next=NULL;
return OK;
} 
//求表長,返回L中數據元素個數
int  ListLength_L(LinkList L){
	int  i=0; 
    LinkList p;
    p=L->next;  //p指向第一個結點               
     while(p){//遍歷單鏈表,統計結點數
           i++;
           p=p->next;    } 
    return i;                             
 }
//取值,獲取線性表L中的某個數據元素的內容
Status GetElem_L(LinkList L,int i,ElemType &e){ 
    LNode *p=L->next;
	int j=1; //初始化
     while(p&&j<i){	//向後掃描,直到p指向第i個元素或p爲空 
       p=p->next; ++j; 
     } 
     if(!p || j>i)return ERROR; //第i個元素不存在 
     e=p->data; //取第i個元素 
     return OK; 
}
//在線性表L中查找值爲e的數據元素 
LNode *LocateELem_L(LinkList L,ElemType e) {
 //返回L中值爲e的數據元素的地址,查找失敗返回NULL
  LNode *p=L->next;
  while(p &&p->data!=e)  
        p=p->next;                		
  return p; 	
} 
//插入,在L中第i個元素之前插入數據元素e 
Status ListInsert_L(LinkList &L,int i,ElemType e){ 
      LNode *p=L;
	  int j=0; 
      while(p&&j<i-1){p=p->next;++j;}	//尋找第i-1個結點 
      if(!p||j>i-1)return ERROR;	//i大於表長n+1或者小於1  
      LinkList s;
	  s=new LNode;			//生成新結點s 
      s->data=e;      		           //將結點s的數據域置爲e 
      s->next=p->next;	   	          //將結點s插入L中 
      p->next=s; 
      return OK; 
}//ListInsert_L 
//刪除,將線性表L中第i個數據元素刪除
 Status ListDelete_L(LinkList &L,int i){
    LNode *p=L;
    LNode *q;
	int j=0; 
    while(p->next &&j<i-1){                  //尋找第i個結點,並令p指向其前驅 
        p=p->next; ++j; 
    } 
    if(!(p->next)||j>i-1) return ERROR; //刪除位置不合理 
    q=p->next;                                        //臨時保存被刪結點的地址以備釋放 
    p->next=q->next; 	                  //改變刪除結點前驅結點的指針域 
    //e=q->data; 	                                //保存刪除結點的數據域 
    delete q; 	                                //釋放刪除結點的空間 
 return OK; 
}//ListDelete_L 
//前插法,建立單鏈表 
void CreateList_F(LinkList &L,int n){ 
      L=new LNode; 
      L->next=NULL; //先建立一個帶頭結點的單鏈表 
      LinkList p;
      for(int i=n;i>0;--i){ 
        p=new LNode; //生成新結點 
        cin>>p->data; //輸入元素值 
        p->next=L->next;L->next=p; 	//插入到表頭 
     } 
}//CreateList_F
//單鏈表元素輸出 
void Out_LinkList(LNode *p){
	 printf("The Created LinkList Elem Is:");
	 while(p!=NULL)
	 {
	  cout<<p->data<<" "; 
	  p=p->next;
	 } 
}//Out_LinkList 
int main(){
	//第一步 初始化單鏈表
	 LinkList L;
	 LNode *p,*q,*Locate_Address;
	 int Init_flag,GetElem_flag;
	 int n=5;//建立單鏈表時需要創建節點的個數 
	 Init_flag=InitList_L(L);
	 if(Init_flag)
	 cout<<"Init OK!"<<endl;
	 else
	 cout<<"Init ERROR!"<<endl;
	//第二步 利用前插法建立單鏈表
	cout<<"Please Input The LinkList Elem_Value( "<<n<<" values):"<<endl;
	CreateList_F(L,n);
	cout<<"Create LinkList OK!"<<endl;
	Out_LinkList(L->next);//輸出現有單鏈表中元素
	cout<<endl; 
	//第三步 單鏈表按照位置i取值
	int GetElem_i;
	ElemType GetElem_e;
	cout<<"Please Input The  GetElem_i: ";
	cin>>GetElem_i;
	GetElem_flag=GetElem_L(L,GetElem_i,GetElem_e);
	if(GetElem_flag)
	cout<<"GetElem Is OK:"<<GetElem_e<<endl;
	else
	cout<<"GetElem Is ERROR!"<<endl; 
    //第四步 單鏈表按值查找,如果找到返回地址,否則返回null 
    ElemType Locate_e;
	cout<<"Please input LocateElem value:"; 
	cin>>Locate_e;
    Locate_Address=LocateELem_L(L,Locate_e); 
    if(Locate_Address!=NULL)
    cout<<"LocateElem Is OK, The Address Is:"<<Locate_Address<<endl;
	else
	cout<<"LocateElem Is ERROR!"<<endl; 
	//第五步 單鏈表的插入操作
	ElemType ListInsert_e; 
	int ListInsert_i,ListInsert_flag;
	cout<<"Please Input The ListInsert_i:";
	cin>>ListInsert_i;
	cout<<"Please Input The ListInsert_e:";
	cin>>ListInsert_e;
	ListInsert_flag=ListInsert_L(L,ListInsert_i,ListInsert_e);
	if(ListInsert_flag)
	{
		cout<<"ListInsert Is OK!"<<endl;
		cout<<"The LinkList of Length is: "<<ListLength_L(L)<<endl;
		Out_LinkList(L->next);//輸出現有單鏈表中元素	
	}
	else
	cout<<"ListInsert Is ERROR!"<<endl; 
	cout<<endl;
	//第六步 單鏈表刪除操作
	int delete_i,delete_flag;
	cout<<"Please input The delete_i:";
	cin>>delete_i;
	delete_flag=ListDelete_L(L,delete_i);
	if(delete_flag)
	{
		cout<<"ListDelete is Ok!"<<endl;
		cout<<"The LinkList of length is: "<<ListLength_L(L)<<endl;
		Out_LinkList(L->next);//輸出現有單鏈表中元素	
	}
	else
	cout<<"ListDelete Is ERROR!"<<endl;
	return OK;
} 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章