解讀鏈表的順序表示和實現_.Net教程
推薦:淺談ASP.NET中顯示Linq To SQL輸出的SQL語句最近在使用Linq To SQL的時候,為了了解不同Linq語句對性能造成的不同影響,需要獲得Linq To SQL生成的SQL語句。 如果是在桌面程序中,只需要 _context.Log = Console.Out; 即可在控制臺輸出SQL語句。可是在ASP.NET中又該怎么辦呢? 這時我想起了StringWriter
/*List.h*/
#ifndef _LIST_H
#define _LIST_H
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
template <class T>
class List
{
public:
List(); //構造函數:構造一個空的線性表
//~List(); //析構函數
void DestroyList(); //銷毀線性表
void ClearList(); //將表重置為空表
bool ListEmpty(); //若為空表存在返回True
int ListLength(); //返回表中數據元素個數
T GetElem(int i,T &e); //用e返回表中第i個元素的值
int LocateElem(T e); //返回表中第一個e元素的位置
T PriorElem(T cur_e,T &pre_e); //返回前驅
T NextElem(T cur_e,T &next_e); //返回后繼
void ListInsert(int i,T e); //在第i個元素插入值e
T ListDelete(int i,T &e); //刪除第i個元素的值并返回
void ListTraverse(void visit()); //對每個元素進行visit()
private:
T *elem;
int length;
int listsize;
};
//構造一個空的線性表
template <class T>
List<T>::List()
{
elem=(T *)malloc(LIST_INIT_SIZE*sizeof(T)); /*分配空間*/
if(!elem)
throw "Allocation failed";
else
{
length=0;
listsize=LIST_INIT_SIZE;
}
}
//銷毀線性表
template <class T>
void List<T>::DestroyList()
{
free(elem);
}
//將表重置為空表
template <class T>
void List<T>::ClearList()
{
length=0;
}
//若為空表存在返回True
template <class T>
bool List<T>::ListEmpty()
{
return length ? true :false;
}
//返回表中數據元素個數
template <class T>
int List<T>::ListLength()
{
return length;
}
//用e返回表中第i個元素的值
template <class T>
T List<T>::GetElem(int i,T &e)
{
if(i<1||i>length)
throw "Index out of bounds";
else
e=elem[i-1];
return e;
}
//返回表中第一個e元素的位置
template <class T>
int List<T>:: LocateElem(T e)
{
for(int i=0;i<length;i++)
{
if(elem[i]==e)
return i+1;
}
cout<<"表中不存在值為"<<e<<"的元素!"<<endl;
return 0;
}
//返回前驅
template <class T>
T List<T>::PriorElem(T cur_e,T &pre_e)
{
int i=LocateElem(cur_e);
if(i>1)
{
pre_e=elem[i-2];
return pre_e;
}
else
return NULL;
}
//返回后繼
template <class T>
T List<T>::NextElem(T cur_e,T &next_e)
{
int i=LocateElem(cur_e);
if(i>0&&i<length)
{
next_e=elem[i];
return next_e;
}
else
return NULL;
}
//在第i個元素插入值e
template <class T>
void List<T>::ListInsert(int i,T e)
{
if(i<0||i>length+1) cout<<"error!";
else if(i==length)
{
elem = (T *)realloc(elem,(length+LISTINCREMENT)*sizeof(T));
}
for(int j=length;j>=i;j--) elem[length]=elem[length-1];
elem[i-1]=e;
length++;
}
//刪除第i個元素的值并返回
template <class T>
T List<T>::ListDelete(int i,T &e)
{
if(length==0) return NULL;
if(i<0||i>length) return NULL;
e=elem[i-1];
for(int j=i;j<length;j++) elem[j-1]=elem[j];
length--;
return e;
}
//對每個元素進行visit()
template <class T>
void List<T>::ListTraverse(void visit())
{
for(i=0;i<length;i++) visit(elem[i]);
}
#endif
//程序測試文件,http://blog.ourys.com/原創,做人好厚道,轉載請表明出去
#include<iostream>
#include "List.h"
using namespace std;
int main()
{
List<int> list;
for(int i=0;i<10;i++) list.ListInsert(i+1,i*(1+i));
int a[10],b[10];
for(int i=0;i<list.ListLength();i++) cout<<list.GetElem(1+i,a[i])<<endl;
cout<<list.ListLength()<<endl;
cout<<list.LocateElem(90)<<endl;
list.NextElem(0,b[0]);
cout<<b[0]<<endl;
cout<<list.ListDelete(4,b[1])<<endl;
for(int i=0;i<list.ListLength();i++) cout<<list.GetElem(1+i,a[i])<<endl;
return 0;
}
分享:怎樣在ASP.net中做網站訪問量統計一位QQ好友問我個人網站訪問量是如何統計的,由于本人初學做網站,水平有限,所介紹的方法可能是笨方法,但在我網站上能夠湊合著使用,希望大家多多指教。 一、建立一個數據表IPStat用于存放用戶信息 我在IPStat表中存放的用戶信息只包括登錄用戶的IP(IP_Ad
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發送Email實例(可帶附件)
- js實現廣告漂浮效果的小例子
- asp.net Repeater 數據綁定的具體實現
- Asp.Net 無刷新文件上傳并顯示進度條的實現方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- asp.net頁面傳值測試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-解讀鏈表的順序表示和實現。