1. 程式人生 > >資料結構實驗一:線性表的基本操作

資料結構實驗一:線性表的基本操作

// 186658-S181-李雙源.cpp : 定義控制檯應用程式的入口點。
//時間 2018-10-27
//內容:線性表的基本操作 

#include "stdafx.h"
#include <iostream>
#include<stdlib.h>
using namespace std;

typedef struct 
{
	int L[100];  
	int length;
}SList;

void InitList_L(SList *,int );       //初始化 

void out_L(SList *);                 //遍歷線性表  
bool  ListInsert_Sq (SList *,int ,int );  //插入 
void ListDelete_Sq(SList *,int ,int ); // 刪除 
void ListCombine_Sq(SList*,SList*,SList*); //合併 
bool GetElem_Sq(SList *,int,int *) ;      //找出要查第幾位的元素 
int LocatElem_Sq(SList*,int) ;			//要查元素在第幾位 
void ListDifferent_Sq(SList*,SList*,SList*);  //差集 也就是兩表不相同的元素 

int _tmain(int argc, _TCHAR* argv[])
{
	SList L,L1,C;						 //  L是第一個表名,L1是第二個表名 ,C是第三表名
	int num,sum;                     // num是第一個表的個數,num是第二個表的個數 
	int Val,loc,place,wz;					 // loc是刪除第幾個元素 wz是存放要要找元素的位置 
	int pVal,pVal2,pVal3;						 // pVal 儲存刪除的元素   pVal2 要找的元素值  pVal3 是查位置的元素 
	cout<<"請輸入第一個表的個數:";
	cin>>num;
	cout<<"請輸入第一個表的值:";
	InitList_L(&L,num);
	cout<<"第一個線性表值為:";
	out_L(&L);
	cout<<"請輸入第二個表的個數:";
	cin>>sum;
	cout<<"輸入第二個表的值:";
	InitList_L(&L1,sum);
	cout<<"第二個線性表值為:";
	out_L(&L1);
//	cout<<"要插入的值和要插入的位置:";
//	cin>>Val>>place;
//	ListInsert_Sq (&L,place,Val );
//	out_L(&L);
	
//	cout<<"輸入要刪除第幾個元素:";
//	cin>>loc; 
//	ListDelete_Sq(&L,loc,pVal);
//	cout<<"刪除後的線性表:";
//	out_L(&L); 
	cout<<"請輸入要找出元素的位置:";
	cin>>wz;
	GetElem_Sq(&L,wz,&pVal2) ;
	cout<<"找出的值為:"<<pVal2<<endl; 
	cout<<"當前表的值為;";
	out_L(&L);
	cout<<"輸入要查位置的元素:";
	cin>>pVal3; 
	cout<<"返回元素的位置是:"; 
	LocatElem_Sq(&L,pVal3); 
//	cout<<"兩表合併:";
//	ListCombine_Sq(&L,&L1,&C);
//	 out_L(&C);
	 cout<<"兩表差集:" ; 
	 ListDifferent_Sq(&L,&L1,&C);
	 out_L(&C) ;
	return 0;
}

void InitList_L(SList *L,int len)   //初始化 
{
	int i;
	for(i = 0; i<len; ++i)
	{
		cin>>L->L[i];
	}
	L->length = len;
}

void out_L(SList *L)            //輸出 
{
	int i;
	for( i = 0; i<L->length; ++i)
	{
		cout<<L->L[i]<<"  ";
	}
	cout<<endl;
}

bool ListInsert_Sq(SList *L,int place ,int Val)  //插入 
{
	int i;
	if(place<0||place>L->length+1)
	return false;                                    //如果插入的位置不正確就跳出程式 
	for(i = L->length-1; i>=place-1;i--)
	{
		L->L[i+1] = L->L[i]; 
	}
	L->L[place-1] = Val;
	L->length++;
	return true;
	
}

void  ListDelete_Sq(SList *L,int loc,int pVal)   //loc 是要刪除的位置  pVal是儲存刪除掉的資料 
{	
	if(loc<1||loc>L->length)
	cout<<"請從新輸入"; 
	pVal = L->L[loc-1];
	for(int i =loc;i<L->length;i++)
	{
		L->L[i-1] = L->L[i];
	}
	 L->length--;
	cout<<"被刪除的元素是:"<<pVal<<endl; 
	return ;
} 

bool GetElem_Sq(SList *L,int wz,int *pVal2)   //找出第wz位置元素 通過pVal2 返回 
{
	if(wz<0||wz>L->length)
	{
	 	return false;
	 } 
	 else 
	  * pVal2 = L->L[wz-1];
	  return true;
}

 int  LocatElem_Sq(SList* L,int pVal3) 
 {
 	  int i = 0;
 	  for(i = 0;i<L->length;i++)
 	  {
 	  	if(L->L[i] == pVal3)
 	  	return i+1;      // 因為返回的是元素的位置,所以要下標要+1 
	   }	
 }


void ListCombine_Sq(SList *L,SList *L1,SList *C)  // 並集 
{
	int i,j,k=0; 
	for(i=0;i<L->length;i++)
	C->L[i] = L->L[i];
	C->length = L->length;
	for(i= 0;i<L1->length;i++)
	{
		j = 0;
		while(j<L->length&&L1->L[i]!=L->L[j])
		j++;
		if(j==L->length)
		C->L[C->length+k++] = L1->L[i];
		}	
		C->length += k;
	
}    

void ListDifferent_Sq(SList *L,SList *L1,SList *C)  // 差集
{
	int i, j,k=0;
	for(i = 0;i<L->length;i++)
	{
		j = 0;
		while(j<L1->length&&L1->L[j] != L->L[i])
		j++;
		if(j==L1->length)
		C->L[k++]=L->L[i];
	}
	C->length = k;
 }