clist訪問
❶ 如何在linux c 實現list
c語言沒有類的概念。C++有現成的List類, #include<list>即可。
如果要自己實現可以參考C++數據結構的書籍,是最基本的練習。
這里實現一個簡單的常式,請參考:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
#include<stdio.h>
#include<string>
#include "math.h"
template<class T> class List{
public:
List() //構造函數
{
pFirst = NULL;
}
void Add(T& t) //在Link表頭添加新結點
{
if(pFirst == NULL)
{
pFirst = new Node;
*(pFirst->pT) = t;
}
else
{
Node* pNewNode = new Node;
*(pNewNode->pT) = t;
pNewNode->pNext = pFirst;
pFirst = pNewNode;
}
}
void Remove(T& t) //在Link中刪除含有特定值的元素
{
Node* pNode = pFirst;
if(*(pNode->pT) == t)
{
pFirst = pFirst->pNext;
delete pNode;
return;
}
while(pNode != NULL)
{
Node* pNextNode = pNode->pNext;
if(pNextNode!=NULL)
{
if(*(pNextNode->pT) == t)
{
pNode->pNext = pNextNode->pNext;
delete pNextNode;
return;
}
}
else
return;//沒有相同的
pNode = pNode->pNext;
}
}
T* Find(T& t) //查找含有特定值的結點
{
Node* pNode = pFirst;
while(pNode != NULL)
{
if(*(pNode->pT) == t)
{
return pNode->pT;
}
pNode = pNode->pNext;
}
return NULL;
}
void PrintList() // 列印輸出整個鏈表
{
if(pFirst == NULL)
{
cout<<"列表為空列表!"<<endl;
return;
}
Node* pNode = pFirst;
while(pNode != NULL)
{
cout<<*(pNode->pT)<<endl;
pNode = pNode->pNext;
}
}
~List()
{
Node* pNode = pFirst;
while(pNode != NULL)
{
Node* pNextNode = pNode->pNext;
delete pNode;
pNode = pNextNode;
}
}
protected:
struct Node{
Node* pNext;
T* pT;
Node()
{
pNext = NULL;
pT = new T;
}
~Node()
{
delete pT;
}
};
Node *pFirst; //鏈首結點指針
};
class Student
{
public:
char id[20]; //學號
char name[20]; //姓名
int age; //年齡
Student()
{
}
~Student()
{
}
Student(const char* pid, const char* pname, int _age)
{
strcpy(id, pid);
strcpy(name, pname);
age = _age;
}
bool operator==(const Student& stu)
{
return strcmp(id, stu.id) == 0 && strcmp(id, stu.id) == 0 && age==stu.age;
}
Student& operator=(const Student& stu)
{
strcpy(id, stu.id);
strcpy(name, stu.name);
age = stu.age;
}
friend ostream& operator<< (ostream &out,const Student& stu);
};
ostream & operator<< (ostream &out,const Student& stu)
{
out<<"id:"<<stu.id<<"\tname:"<<stu.name<<"\tage:"<<stu.age<<endl;
}
int main()
{
List<Student> stuList;
cout<<"添加學生前:"<<endl;
stuList.PrintList();
Student stu1("1", "張三", 18);
Student stu2("2", "李四", 18);
Student stu3("3", "王五", 18);
Student stu4("4", "至尊寶", 18);
Student stu5("5", "豬八戒", 18);
Student stu6("6", "唐僧", 18);
Student stu7("7", "沙和尚", 18);
Student stu8("8", "觀音", 18);
stuList.Add(stu1);
stuList.Add(stu2);
stuList.Add(stu3);
stuList.Add(stu4);
stuList.Add(stu5);
stuList.Add(stu6);
stuList.Add(stu7);
stuList.Add(stu8);
cout<<"添加學生後:"<<endl;
stuList.PrintList();
Student stu11("1", "張三", 18);
Student* pStu = stuList.Find(stu11);
cout<<"查找到的同學是:"<<*pStu;
stuList.Remove(stu11);
cout<<"\n\n刪除第一個後:"<<endl;
stuList.PrintList();
return 0;
}
❷ c語言鏈表的建立和順序訪問各節點的數據域
#include<stdio.h>
#include<stdlib.h>
typedefstructstudent
{
intscore;
structstudent*next;
}student;
student*creatlist()
{
inti=0;
student*head,*p,*q;
head=(student*)malloc(sizeof(student));
p=head;
scanf("%d",&i);
while(i!=-1)
{
q=(student*)malloc(sizeof(student));
q->score=i;
p->next=q;
p=q;
scanf("%d",&i);
}
p->next=NULL;
returnhead;
}
voidprint(student*head)
{
if(!head)return;
student*p=head->next;
while(p)
{
printf("%d",p->score);
p=p->next;
}
}
intmain()
{
student*head;
head=creatlist();
print(head);
system("pause");
return0;
}
❸ C#中List<>的用法
List<>是泛型,尖括弧里可以放任何類,初始化的實例可以添加尖括弧里類的實例,用法類似數組。
using System;
using System.Collections.Generic;
public class A
{
public static void Main()
{
//創建一個list<string>對象Cats
List<string>Cats= new List<string>();
//為Cats添加項
Cats.Add("Cat1");
Cats.Add("Cat2");
Cats.Add("Cat3");
//遍歷Cats列表並輸出
foreach(string cat in Cats)
{
Console.WriteLine(cat);
}
}
(3)clist訪問擴展閱讀:
泛型的好處
1、避免了強制類型轉換而造成代碼可讀性差。
2、既然有了類型強制轉換,問題來了:類型強制轉換可能會用到裝箱和拆箱過程,耗時。
3、再由於有強制類型轉換,在編譯的時候可能不會包錯,但是運行代碼的時候有可能會因為轉換失敗而出現錯誤。這就是我們說的非安全代碼。
}
}
class Cls
{ }
簡單來說,泛型就是限制了操作類型,添加到 ArrayList 中的任何引用或值類型都將隱式地向上強制轉換為 Object。如果項是值類型,則必須在將其添加到列表中時進行裝箱操作,在檢索時進行取消裝箱操作。
強制轉換以及裝箱和取消裝箱操作都會降低性能;在必須對大型集合進行循環訪問的情況下,裝箱和取消裝箱的影響非常明顯。
❹ VC 中list和Clist的區別,以及怎麼用
list是在stl庫中的
Clist是在MFC庫中的
都是用來實現鏈表結構的,具體的介面可能稍有不用,總體差不多,看msdn就可以了
❺ C++裡面的通過下標訪問元素指的是什麼 順序容器list可以通過下標訪問嗎舉例說明 謝謝
像數組或者vector之類的容器,可以使用下標訪問,list並沒有提供下標訪問
比如說有一個int型數組a,它有1,3,5三個元素,這三個元素會從0開始被編號,這個編號就是下標。
也就是說,1是數組a的0號下標所對應的元素,3是數組a的1號下標所對應的元素,5是數組a的2號下標所對應的元素。
如果你要訪問1就可以直接使用a[0],訪問3直接使用a[1]。。。以此類推,這就是下標訪問。
❻ 如何按索引訪問嵌套list的元素
使用二維數組的方式,list[0][0] 表示1, list[0][1] 表示『A』, 以此類推
list=[[1,'A'],[2,'B'],[2,'C'],[3,'B'],[4,'A'],[6,'B']]
i=0
for i in range(0,6):
print list[i][1]
i=i+1
❼ VC++中使用CList需要包含什麼頭文件嗎
當然要包含了#include"afxtempl.h"
❽ C++中如何從CList中得到其中任意一個元素
Example
// Define myList.
CList<CString,CString&> myList;
// Add three elements to the list.
myList.AddTail(CString("XYZ"));
myList.AddTail(CString("ABC"));
myList.AddTail(CString("123"));
// Verify the first element (index 0).
ASSERT(CString("XYZ") == myList.GetAt(myList.FindIndex(0)));
// Verify the third element (index 2).
ASSERT(CString("123") == myList.GetAt(myList.FindIndex(2)));
❾ CList是線程安全的嗎
不是的,只能單線程訪問
❿ c++語言:關於友元類:如果有兩個類CNode和CList,在CNode中聲明CList是自己的友
你知道的不對。
友元聲明的含義是,A是本類的友員,可以訪問本類的所有資源。所以在CNode中聲明CList是友元,只允許CList訪問CNode的成員,不能反過來。