1. 程式人生 > >《資料結構》進行曲(二)順序表的鏈式表示(1)

《資料結構》進行曲(二)順序表的鏈式表示(1)

#include<stdio.h>
#include<iostream>
using namespace std;
#define MAX 100

//-----單鏈表的儲存結構---- 
typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*LinkList;

 //單鏈表的初始化
 int InitList(LinkList &L){
 	//構造一個空的單鏈表
	 L=new LNode;
	 L->next=NULL;
	 return 1;
 } 
 
 //判斷連結串列是否為空
 int EmptyList(LinkList L){
 	if(L->next){
 		return 0;//非空 
 	}else{
 		return 1;//連結串列為空 
 	}
 } 
 
 //獲取連結串列長度
 int ListLength(LinkList L){
 	int length=0;
 	struct LNode *p;
 	p=L->next;
 	while(p){
 		length++;
 		p=p->next;
 	}
 	return length;
 } 
 //遍歷單鏈表
 void TraveList(LinkList L){
 	struct LNode *p;
 	p=L->next;
 	printf("連結串列結果如下:\n");
 	while(p){
 		printf("%d ",p->data);
 		p=p->next;
 	}
 	printf("\n");
 } 
 
 //查詢
 //1.按序號查詢
 int SelectByNo(LinkList L,int i,int &e){
 	//在帶頭結點的單鏈表中查詢第i個值 
 	struct LNode *p;
 	p=L->next;//初始化,p指向第一個結點 
 	int j=1;//j用來計數
 	while(p&&j<i){//順著鏈域掃描,直到p指向第i個元素,或者p為空 
 		p=p->next;
 		++j;
 	}
 	if(!p||j>i){
 		return 0;
 	}
 	e=p->data;
 	return 1;
 }
 
 //2.按值查詢
 LNode *SelectByValue(LinkList L,int e){
 	//在帶頭結點的單鏈表中查詢值為e的元素
	 struct LNode *p;
	 while(p&&p->data!=e){
	 	p=p->next; 
	 } 
	 return p;
 } 
 
 /*
 插入操作 
 */
 int ListInsert(LinkList &L,int i,int &e){
 	//在單鏈表的第i個位置之前插入元素e
	 int j=0;
	 struct LNode *p;
	 p=L;
	 while(p&&j<i-1){//尋找第i-1個結點 
	 	p=p->next;
	 	++j;
	 } 
	 if(!p||j>i){
	 	return 0;
	 }
	 struct LNode *s;
	 s=new LNode;//生成新結點s
	 s->data=e; //將新結點的資料域置為e
	 s->next=p->next;//將新結點插入L中 
	 p->next=s;
	 return 1; 
 }
 
 /*
 刪除操作 
 */
 int ListDelete(LinkList &L,int i,int e){
 	//刪除連結串列l中的第i個位置的元素,並用e返回其值
	 struct LNode *p;
	 p=L;
	 int j=0;
	 while(p&&j<i-1){//尋找地i-1個結點 
	 	p=p->next;
	 	++j;
	 } 
	 if(!(p->next)||j>i-1){
	 	return 0;
	 }
	 struct LNode *q;
	 q=p->next;//臨時儲存要刪除的結點的地址以備釋放
	 p->next=q->next;
	 e=q->data;
	 delete q;
	 return 1; 
 }
 //前插法建立單鏈表
 void CreateList(LinkList &L,int n){
 	 //建立帶頭結點的單鏈表L,輸入n個元素的值
	 L=new LNode;//先建立一個帶頭結點的空的單鏈表
	 L->next=NULL;
	 printf("輸入n個元素的值:\n");
	 for(int i=0;i<n;i++){
	 	struct LNode *p;
	 	p=new LNode;//生成新結點
		printf("請輸入第%d個元素的值:",i+1);
		scanf("%d",&p->data);
		p->next=L->next;
		L->next=p; 
	 } 
	 
 }
 
 int main(){
 	LinkList L;
 	
 	InitList(L);
 	if(EmptyList(L)){
 		printf("連結串列為空.\n");
 	}else{
 		printf("連結串列非空.\n");
 	}
 	
 	printf("請輸入連結串列中元素的個數:\n");
 	int n;
 	scanf("%d",&n);
 	CreateList(L,n);
 	
 	TraveList(L);
 	printf("當前連結串列長度是:%d\n",ListLength(L));
 	
 	printf("請輸入要查詢元素在連結串列中的位置:\n");
 	int location;
 	scanf("%d",&location);
	int e1;
	if(SelectByNo(L,location,e1)){
		printf("%d位置元素的值是:%d\n",location,e1);
	}else{
		printf("查詢失敗!\n");
	}
	
	printf("輸入要查詢元素的值:\n");
	int e2;
	scanf("%d",&e2);
	LNode *p;
	p=SelectByValue(L,e2);
	printf("位置:%d\n",p);
 	
 	printf("請輸入插入元素的位置和值:\n");
 	int e,location1;
 	scanf("%d%d",&location,&e);
 	ListInsert(L,location,e);
 	TraveList(L);
 	
 	printf("請輸入要刪除元素的位置:\n");
 	int location2;
 	scanf("%d",&location2);
 	int e3;
 	ListDelete(L,location2,e3);
 	printf("要刪除的元素值是:%d\n",e3);
 	TraveList(L);
 	
 	return 0;
 }