// VCPro1.cpp :Algorithm in the course of data structure
//Author: Acefly
//Date: 2008/12/26
#include "stdafx.h"
#include <iostream>
using namespace std;
// Define the struct node for lineList
#define InitLineListSize 100
typedef struct
{
int * baseAddress;
int length;
int initialSize;
} LineList,*PLineList;
// Define the struct node for linkList
struct node
{
int data;
struct node * next;
};
// Function declaration
void CreateLineList(LineList lineList);
void InsertDataIntoLineList(int *lineList,int length,int insertLocation,int lineListData);
void DeleteDataFromLineList(int *ineList,int length,int deleteLocation);
int SearchDataFromLineList(int *LineList,int length,int searchData);
void DisplayAllDataInTheLineList(int *LineList,int length);
void ExitFromProgram();
void InsertDataIntoLinkList(struct node* linkNodes,int insertLocation,int linkListData);
void DisplayDataInLinkList(struct node* linkNodes);
void DeleteDataFromLinkList(struct node* linkList,int deleteLocation);
void SearchDataFromLinkList(struct node* linkList,int searchLocation);
/// <summary>
/// Main function
/// </summary>
int _tmain(int argc, _TCHAR* argv[])
{
//the series number for operation of the platform.
int num;
//define an lineList object
LineList lineList;
//create the lineList.
lineList.baseAddress=(int *)malloc(InitLineListSize*sizeof(lineList));
if(!lineList.baseAddress)
{
cout <<"Initial lineList failed.";
exit(0);
}
else
{
lineList.length=0;
lineList.initialSize=InitLineListSize;
}
// build the linkList
struct node* linkNodes = (struct node *)malloc(sizeof(struct node));
linkNodes->next=NULL;
//build the user interface.
cout << "DataStructure testing platform"<<endl;
cout << "1: Insert data into the lineList" <<endl;
cout << "2: Delete data from lineList" <<endl;
cout << "3: Search data from lineList" <<endl;
cout << "4: Add data to the linkList"<<endl;
cout << "5: Delete data from the linkList"<<endl;
cout << "6: Search data from the linkList"<<endl;
cout << "" << endl;
cout << "100: Exit" << endl;
cout << "***************************************"<<endl;
cout << "Please make your choice (1,2,3,4,5,6)" <<endl;
cin >> num;
//verify the input series number.
while(num!=1&&num!=2&&num!=3&&num!=4&&num!=5&&num!=6&&num!=100)
{
cout << "You have input the wrong series number,please input it again:"<<endl;
cin >> num;
}
do
{
switch(num)
{
case 1 :
{
int insertLocation;
int lineListData;
cout << "Please input the insert location:(the length of the current lineList is "<<lineList.length<<") "<<endl;
cin >> insertLocation;
while(insertLocation<0||insertLocation>lineList.length)
{
cout << "you can't insert data here ,Please input again:"<<endl;
cin >> insertLocation;
}
cout << "Please intput the insert data:"<<endl;
cin >>lineListData;
InsertDataIntoLineList(lineList.baseAddress,lineList.length, insertLocation,lineListData);
lineList.length++;
//Display all data in the lineList
DisplayAllDataInTheLineList(lineList.baseAddress,lineList.length);
break;
}
case 2 :
{
int deleteLocation;
cout << "Please input series number of the data you want to delete"<<endl;
cin >> deleteLocation;
while(deleteLocation < 0||deleteLocation > lineList.length)
{
cout << "The data is not exist,please input again:"<<endl;
cin >> deleteLocation;
}
DeleteDataFromLineList(lineList.baseAddress,lineList.length,deleteLocation);
lineList.length--;
//Display all data in the lineList
DisplayAllDataInTheLineList(lineList.baseAddress,lineList.length);
break;
}
case 3 :
{
int searchData;
int dataLocation;
cout << "Please input the data you want to search" <<endl;
cin >> searchData;
dataLocation=SearchDataFromLineList(lineList.baseAddress,lineList.length,searchData);
if(dataLocation)
{
cout << "The number of the data you want is : " << dataLocation << endl;
}
else
{
cout << "The data you search is not in the lineList" <<endl;
}
//Display all data in the lineList
DisplayAllDataInTheLineList(lineList.baseAddress,lineList.length);
break;
}
case 100 :
{
ExitFromProgram();
break;
}
case 4:
{
//Insert node to the linkList
int insertLocation;
int linkListData;
cout << "Please input the insert location:"<<endl;
cin >> insertLocation;
cout << "Please intput the insert data:"<<endl;
cin >> linkListData;
InsertDataIntoLinkList(linkNodes,insertLocation,linkListData);
//Display all data in the linkList
DisplayDataInLinkList(linkNodes);
break;
}
case 5:
{
//Delete node from the linkList
int deleteLocation;
cout << "Please input series number of the data you want to delete"<<endl;
cin >> deleteLocation;
DeleteDataFromLinkList(linkNodes,deleteLocation);
//Display all data in the linkList
DisplayDataInLinkList(linkNodes);
break;
}
case 6:
{
//Search node from the linkList
int searchLocation;
int result;
cout << "Please input series number of the data you want to search"<<endl;
cin >> searchLocation;
SearchDataFromLinkList(linkNodes,searchLocation);
//Display all data in the linkList
DisplayDataInLinkList(linkNodes);
break;
}
default: break;
}
cout << "LineList function testing platform"<<endl;
cout << "1: Insert data into the lineList" <<endl;
cout << "2: Delete data from lineList" <<endl;
cout << "3: Search data from lineList" <<endl;
cout << "4: Add data to the linkList"<<endl;
cout << "5: Delete data from the linkList"<<endl;
cout << "6: Search data from the linkList"<<endl;
cout << "100: Exit" << endl;
cout << "***************************************"<<endl;
cin >>num;
//verify the input data.
while(num!=1&&num!=2&&num!=3&&num!=4&&num!=5&&num!=6&&num!=100)
{
cout << "you have input the wrong number";
cin >> num;
}
}while(num!=100);
}
/// <summary>
/// Insert data into the lineList.
/// </summary>
void InsertDataIntoLineList(int *lineList,int length,int insertLocation,int lineListData)
{
for(int i=length-1;i>=insertLocation;i--)
{
*(lineList+i+1)=*(lineList+i);
}
*(lineList+insertLocation)=lineListData;
}
/// <summary>
/// Insert data into the linkList.
/// </summary>
void InsertDataIntoLinkList(struct node* linkNodes,int insertLocation,int lineListData)
{
struct node* p=linkNodes;
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->data = lineListData;
for(int i =0 ;i< insertLocation-1 && p->next!=NULL;i++)
{
p = p->next;
}
Node->next=p->next;
p->next=Node;
}
/// <summary>
/// Search data in the linkList.
/// </summary>
void SearchDataFromLinkList(struct node* linkNodes,int searchLocation)
{
int length=0;
struct node* p=linkNodes;
struct node* q=linkNodes;
while(p!=NULL)
{
p=p->next;
length++;
}
if(searchLocation > length || searchLocation < 0)
{
cout << "The data is not exist in the linkList";
}
else
{
for(int i=0;i < searchLocation;i++)
{
q=q->next;
}
cout << "The data you search is: ";
cout << q->data<<"/n";
}
}
/// <summary>
/// Insert data into the linkList.
/// </summary>
void DisplayDataInLinkList(struct node* linkNodes)
{
cout << "The data in the linkList are :";
linkNodes=linkNodes->next;
while(linkNodes!=NULL)
{
cout << linkNodes->data << " ";
linkNodes=linkNodes->next;
}
cout << "/n";
}
/// <summary>
/// Delete data from the lineList.
/// </summary>
void DeleteDataFromLineList(int *lineList,int length,int deleteLocation)
{
for(int i=deleteLocation-1;i<length;i++)
{
*(lineList+i)=*(lineList+i+1);
}
}
/// <summary>
/// Delete data from the linkList.
/// </summary>
void DeleteDataFromLinkList(struct node *linkList,int deleteLocation)
{
struct node* p=linkList;
struct node* q;
for(int i =1 ;i< deleteLocation && p->next!=NULL;i++)
{
p=p->next;
}
q=p->next;
p->next=q->next;
delete(q);
}
/// <summary>
/// Search data in the linelist.
/// </summary>
int SearchDataFromLineList(int *lineList,int length,int searchData)
{
for(int i=0;i<length;i++)
{
if(*(lineList+i)==searchData)
{
return i+1;
}
}
return 0;
}
///<summary>
///display all data in the lineList.
///<summary>
void DisplayAllDataInTheLineList(int *LineList,int length)
{
if(LineList)
{
cout << "The data in the current lineList are :" <<endl;
for(int i=0;i<length;i++)
{
cout << *(LineList+i)<<" ";
}
cout << endl;
}
}
///<summary>
///exit from the system.
///<summary>
void ExitFromProgram()
{
exit(0);
}