當前位置:首頁 » 編程語言 » list在c語言中的用法
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

list在c語言中的用法

發布時間: 2023-03-02 08:17:41

1. c語言中creatlist的用法

1、createlist不是庫函數,一般是數據結構中實現新建一個鏈表的自定義函數。因此沒有什麼用法好說的,關鍵是看自己怎麼定義。

2、常式:

NODE*creatlist(inta[])
{NODE*h,*p,*q;inti;
h=(NODE*)malloc(sizeof(NODE));
h->next=NULL;
for(i=0;i<N;i++)
{q=(NODE*)malloc(sizeof(NODE));
q->data=a[i];
q->next=NULL;
if(h->next==NULL)h->next=p=q;
else{p->next=q;p=q;}}
returnh;
}

2. C語言中的list是指什麼求一個簡單的list代碼

C語言中沒有list
list是C++中的一個類
具體使用可以從網上查一下,有很多應用

3. 怎樣在C語言中正確運用鏈表鏈表的使用需要注意哪些要點

1.使用鏈表時候,先確認要使用的是單向鏈表,還是雙向鏈表,或者是循環鏈表。一定要初始化。
2.添加節點時候,要注意是否隊列已滿。
3.刪除節點的時候,要注意隊列是否為空。
4.要有可以判斷鏈表是否為空的函數。
5.要有可以判斷鏈表節點個數的函數。

4. 如何用C語言或C++實現一個List類

  1. C語言沒有類的概念。C++有現成的List類, #include<list>即可。

  2. 如果要自己實現可以參考C++數據結構的書籍,是最基本的練習。

    這里實現一個簡單的常式,請參考:

    #include<iostream>
    #include<fstream>
    #include<stdlib.h>
    #include<string.h>
    usingnamespacestd;
    #include<stdio.h>
    #include<string>

    #include"math.h"

    template<classT>classList{
    public:
    List()//構造函數
    {
    pFirst=NULL;
    }

    voidAdd(T&t)//在Link表頭添加新結點
    {
    if(pFirst==NULL)
    {
    pFirst=newNode;
    *(pFirst->pT)=t;
    }
    else
    {
    Node*pNewNode=newNode;
    *(pNewNode->pT)=t;
    pNewNode->pNext=pFirst;
    pFirst=pNewNode;
    }
    }

    voidRemove(T&t)//在Link中刪除含有特定值的元素
    {
    Node*pNode=pFirst;
    if(*(pNode->pT)==t)
    {
    pFirst=pFirst->pNext;
    deletepNode;
    return;
    }
    while(pNode!=NULL)
    {
    Node*pNextNode=pNode->pNext;
    if(pNextNode!=NULL)
    {
    if(*(pNextNode->pT)==t)
    {
    pNode->pNext=pNextNode->pNext;
    deletepNextNode;
    return;
    }
    }
    else
    return;//沒有相同的

    pNode=pNode->pNext;
    }
    }
    T*Find(T&t)//查找含有特定值的結點
    {
    Node*pNode=pFirst;
    while(pNode!=NULL)
    {
    if(*(pNode->pT)==t)
    {
    returnpNode->pT;
    }
    pNode=pNode->pNext;
    }
    returnNULL;
    }
    voidPrintList()//列印輸出整個鏈表
    {
    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;
    deletepNode;
    pNode=pNextNode;
    }
    }
    protected:
    structNode{
    Node*pNext;
    T*pT;

    Node()
    {
    pNext=NULL;
    pT=newT;
    }
    ~Node()
    {
    deletepT;
    }
    };
    Node*pFirst;//鏈首結點指針
    };

    classStudent
    {
    public:
    charid[20];//學號
    charname[20];//姓名
    intage;//年齡
    Student()
    {
    }
    ~Student()
    {
    }
    Student(constchar*pid,constchar*pname,int_age)
    {
    strcpy(id,pid);
    strcpy(name,pname);
    age=_age;
    }
    booloperator==(constStudent&stu)
    {
    returnstrcmp(id,stu.id)==0&&strcmp(id,stu.id)==0&&age==stu.age;
    }
    Student&operator=(constStudent&stu)
    {
    strcpy(id,stu.id);
    strcpy(name,stu.name);
    age=stu.age;
    }
    friendostream&operator<<(ostream&out,constStudent&stu);
    };
    ostream&operator<<(ostream&out,constStudent&stu)
    {
    out<<"id:"<<stu.id<<" name:"<<stu.name<<" age:"<<stu.age<<endl;
    }

    intmain()
    {
    List<Student>stuList;
    cout<<"添加學生前:"<<endl;
    stuList.PrintList();

    Studentstu1("1","張三",18);
    Studentstu2("2","李四",18);
    Studentstu3("3","王五",18);
    Studentstu4("4","至尊寶",18);
    Studentstu5("5","豬八戒",18);
    Studentstu6("6","唐僧",18);
    Studentstu7("7","沙和尚",18);
    Studentstu8("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();


    Studentstu11("1","張三",18);
    Student*pStu=stuList.Find(stu11);
    cout<<"查找到的同學是:"<<*pStu;

    stuList.Remove(stu11);
    cout<<" 刪除第一個後:"<<endl;
    stuList.PrintList();

    return0;
    }

5. C語言的形參struct list 和 list有什麼區別嗎

對於結構體類型變數定義,struct
list和list在C++中是相同的,都是正確的。
但在C語言中,如果沒有經過重定義處理,則會編譯報錯。如:
typedef struct list
{
int data;
struct list *next;
} list ;這樣之後,可以使用struct
list

list
來定義變數了,如:
struct
list
*
link
;
list
*
link1
;

6. c語言中,int i,j,list(10),這里的list(10)代表什麼意思

在C++中才可以寫list(10),並且list的值被初始化為10.但是有一個問題:list是一個容器類型,你這樣寫應該會有問題。就像vector一樣。

7. C語言的形參struct list 和 list有什麼區別嗎

對於結構體類型變數定義,struct list和list在C++中是相同的,都是正確的。

但在C語言中,如果沒有經過重定義處理,則會編譯報錯。如:

typedefstructlist
{
intdata;
structlist*next;
}list;

這樣之後,可以使用struct list 或 list 來定義變數了,如:

struct list * link ;

list * link1 ;

8. c語言中的struct list是什麼意思,它代表什麼東西。怎麼使用

struct
friends_list
f;
就是定義簡單的結構體變數f
friends[count]
=
f;
就是把結構體變數f賦值給結構體數組friends的第count個元素

9. C語言中鏈表主要應用於哪些實際問題的解決

鏈表可以解決很多實際問題,比如數據結構課程上講的多項式運算、求解約瑟夫問題,操作系統原理中的內存管理器實現等等。舉一個在Windows通過鏈表搜索文件的實例,代碼如下(vc6.0中編譯通過)。

#include<stdio.h>
#include<windows.h>
structDirList{
chartable[256];
DirList*pNext;
};
DirList*first,*newlist,*last;
//加入文件夾鏈表
voidAddList(char*list)
{
newlist=newDirList;
strcpy(newlist->table,list);
newlist->pNext=NULL;
//假如文件鏈表為空,那麼第一個和最後一個節點都指向新節點
if(first==NULL)
{
first=newlist;
last=newlist;
}
//不為空,則原來最後一個節點指向新節點
else
{
last->pNext=newlist;
last=newlist;
}
}
//查找文件,並把找到的文件夾加入文件夾鏈表
voidFindFile(char*pRoad,char*pFile)
{
charFileRoad[256]={0};
charDirRoad[256]={0};
charFindedFile[256]={0};
charFindedDir[256]={0};
strcpy(FileRoad,pRoad);
strcpy(DirRoad,pRoad);
strcat(DirRoad,"\*.*");
WIN32_FIND_DATAfindData;
HANDLEhFindFile;
hFindFile=FindFirstFile(DirRoad,&findData);
if(hFindFile!=INVALID_HANDLE_VALUE)
{
do
{
if(findData.cFileName[0]=='.')
continue;
//假如是文件夾,則假如文件夾列表
if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
strcpy(FindedDir,pRoad);
strcat(FindedDir,"\");
strcat(FindedDir,findData.cFileName);
//加入文件夾列表
AddList(FindedDir);
memset(FindedDir,0x00,256);
}
//繼續查找
}while(FindNextFile(hFindFile,&findData));
}
strcat(FileRoad,"\");
strcat(FileRoad,pFile);
//查找要查找的文件
hFindFile=FindFirstFile(FileRoad,&findData);
if(hFindFile!=INVALID_HANDLE_VALUE)
{
do
{
strcpy(FindedFile,pRoad);
strcat(FindedFile,"\");
strcat(FindedFile,findData.cFileName);
//輸出查找到的文件
printf("%s ",FindedFile);
memset(FindedFile,0x00,256);
}while(FindNextFile(hFindFile,&findData));
}
}
intSeachFile(char*Directory,char*SeachFile)
{
DirListNewList;
strcpy(NewList.table,Directory);
NewList.pNext=NULL;
last=&NewList;
first=&NewList;
while(true)
{
DirList*Find;
//假如鏈表不為空,提取鏈表中的第一個節點,並把第一個節點指向原來第二個
if(first!=NULL)
{
//提取節點
Find=first;
//並把第一個節點指向原來第二個
first=first->pNext;
//在提取的節點的目錄下查找文件
FindFile(Find->table,SeachFile);
}
//為空則停止查找
else
{
printf("文件搜索完畢 ");
return0;
}
}
return0;
}
intmain(intargc,char*argv[])
{
if(argc!=3){
printf("程序名文件目錄要搜索的文件名 ");
return0;
}
SeachFile(argv[1],argv[2]);
return0;
}

執行效果如下,測試搜索c:windows目錄中的記事本程序notepad.exe。