❶ 如何用c語言或C++實現一個List類
C語言沒有類的概念。C++有現成的List類, #include<list>即可。
如果要自己實現可以參考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;
}
❷ 給出建立 n個結點的單項列表的實現代碼用C語言
#include <stdio.h> #include <stdlib.h> struct BiTreeNode { char data; struct BiTreeNode *rchild; struct BiTreeNode *lchild; }; void Create(struct BiTreeNode *&Tnode) //先序創建2叉鏈表 { char ch; scanf("%c",&ch); if(ch=='#') { Tnode=NULL; } else { Tnode=new BiTreeNode; Tnode->data=ch; Create(Tnode->lchild); Create(Tnode->rchild); } } void PreOrder(struct BiTreeNode *&Tnode) //先序遍歷2叉鏈表 { struct BiTreeNode *p; p=Tnode; if(Tnode) { printf("%c ",Tnode->data); PreOrder(Tnode->lchild); PreOrder(Tnode->rchild); } } void InOrder(struct BiTreeNode*&Tnode)//中序遍歷2叉表 { struct BiTreeNode *p; p=Tnode; if(Tnode) { InOrder(Tnode->lchild); printf("%c ",Tnode->data); InOrder(Tnode->rchild); } } void PostOrder(struct BiTreeNode *&Tnode)//後序遍歷2叉鏈表 { struct BiTreeNode *p; p=Tnode; if(Tnode) { PostOrder(Tnode->lchild); PostOrder(Tnode->rchild); printf("%c ",Tnode->data); } } int main() { struct BiTreeNode *Tnode; Create(Tnode); PreOrder(Tnode); printf("\n"); InOrder
❸ c語言!!!程序設計:建立一個學生信息鏈表,包括學號,姓名,成績.(實現添加,刪除,查詢,排序,平均)
代碼如下:
/*用c語言鏈表編寫一個學生信息系統程序,要求輸出學生的學號,姓名,性別,學號,姓名,成績(實現添加,刪除,查詢,排序,平均)*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int n=5;
/*
* nodeEntry : 節點數據類型
* nodeADT : 節點結構
* linkADT : 鏈表結構
*/
typedef struct Student
{
int num;
char name[30];
char sex;
float score1;//語文
float score2;//數學
float score3;//英語
//struct Student *next;
}Student;
typedef struct linkCDT {
nodeADT head;
}*linkADT;
/*
* InitLink : 初始化鏈表
* CreateNode : 創建節點
* AppendLink : 添加數據
*/
nodeADT CreateNode(Student entry) {
nodeADT p=(nodeADT)malloc(sizeof*p);
p->entry=entry,p->next=0;
return p;
}
/*
SortLink : 排序鏈表
//按學號排序
void SortLinkID(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.num>=p->entry.num)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
//按英語成績排序
void SortLinkEnglish(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.score3>=p->entry.score3)
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的字典序進行排序
void SortLinkName(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (pHead->entry.name[0]>=p->entry.name[0])
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
}
//按姓名的長度進行排序
void SortLinkNameLength(linkADT link) {
nodeADT pHead,pRear,p,tp;
if (!link) return;
for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {
for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)
if (strlen(pHead->entry.name)>=strlen(p->entry.name))
tp->next=p->next,p->next=pHead,pHead=p,p=tp;
if (!pRear) link->head=pHead;
else pRear->next=pHead;
pRear=pHead;
}
循環鏈表是與單鏈表一樣
是一種鏈式的存儲結構,所不同的是,循環鏈表的最後一個結點的指針是指向該循環鏈表的第一個結點或者表頭結點,從而構成一個環形的鏈。
循環鏈表的運算與單鏈表的運算基本一致。所不同的有以下幾點:
1、在建立一個循環鏈表時,必須使其最後一個結點的指針指向表頭結點,而不是象單鏈表那樣置為NULL。此種情況還使用於在最後一個結點後插入一個新的結點。
2、在判斷是否到表尾時,是判斷該結點鏈域的值是否是表頭結點,當鏈域值等於表頭指針時,說明已到表尾。而非象單鏈表那樣判斷鏈域值是否為NULL。
以上內容參考:網路-鏈表
❹ C語言如何建立一個列表,編程怎麼編,舉例說一下,謝謝啦
首先定義一個鏈表。
struct node
{
int id;
struct node * next;
};
接下來寫一些操作的函數(插入,刪除等等)。
插入函數:
struct node* insert(struct node* pNode, int iTemp)
{
//判斷 pNode 是否為空。
if(pNode==NULL)
{
//這個節點是空,返回錯誤。
return NULL;
}
// 創建一個新的節點。
struct node* tempNode = (struct node*)malloc(sizeof(struct node));
tempNode->id= iTemp;
if(pNode->next == NULL)
{
pNode->next = tempNode;
tempNode->next = NULL;
}else
{
struct node * pNext = pNode->next;
pNode->next = tempNode;
tempNode->next = pNext;
}
return tempNode;
}
int main()
{
struct node* head = (struct node*)malloc(sizeof(struct node));
head->id = 0;
head->next = NULL;
struct node * ptemp;
ptemp = head;
for( int i=1; i<10; i++)
{
struct node* temp = insert(ptemp,i);
ptemp = temp;
}
return 0;
}
❺ 幫忙寫個循環列表,用c語言寫 創造一個循環列表,只要3個量(A.num=100,B.num=200,c.num=300),有輸出,
#include stdio.h
int main(void)
{
int num, div, sum;
num=6;
printf("500以內的完全數有:\n");
do{
div = 1;sum=0;
do{
if(num%div==0)
{ sum += div;}
div++;
}
while(divnum);
if(sum == div)
{printf("%d\n", num);}
num++;
}while(num500);
return 0;
}
❻ 一個有關用C語言生成數字列表的問題
如果生成的列表是用來列印的:
int i,n; char fmt[80]; scanf("%d,%d",&n,&k);
sprintf(fmt,"%%0%dd,",n); for ( i=0;i<k;i++ ) printf(fmt,i+1);
如果生成的列表需要保存:
int i,n,k; char fmt[80],mlist[1024][10];
scanf("%d,%d",&n,&k); if ( k>1024 ) k=1024; if ( n>9 ) n=9;
sprintf(fmt,"%%0%dd,",n); for ( i=0;i<k;i++ ) sprintf(mlist[i],fmt,i+1);
❼ 循環列表問題,創造一個單循環列表,要求輸出各個元素,用C語言完成。 為什麼我輸入 1 2 3 ,輸出是 0 1 2
參數調用不對,C語言沒有引用的,列印時打了頭節點但沒打最後一個節點,所以是0 1 2
改正:
typedef struct Lnode{
int data;
struct Lnode *next;
}Lnode,*LinkList;
Lnode *a;
void CreateList(LinkList *L,int n)
{
Lnode *p,*head;
*L=(LinkList)malloc(sizeof(Lnode));
head=*L;
(*L)->next=head;
int i;
for(i=0;i<n;i++)
{
p=(LinkList)malloc(sizeof(Lnode));
scanf("%d",&p->data);
p->next=head;
(*L)->next=p;
(*L)=p;
}
}
void print(LinkList *L)
{
Lnode *head;
head=*L;
(*L)=(*L)->next;
while((*L)!=head)
{
(*L)=(*L)->next;
printf("%d",(*L)->data);
}
}
int main(int argc, char *argv[])
{
int m;
scanf("%d",&m);
CreateList(&a,m);
print(&a);
system("PAUSE");
return 0;
}
❽ 幫我c語言創建列表寫程序,標明每一步是做什麼的,越詳細越好,我完全步懂這東西,謝謝
順序表?是數組還是鏈表? 給你寫個簡單的數組吧
#include<stdio.h>
int insert(int Buff[],int nlen)
{
int a = 0;
int Temp1 = 0;
int Temp2 = 0;
while (a < nlen)
{
if (a == 3)//遍歷到第4位,在第4位添加元素88
{
Temp1 = Buff[a];
Buff[a] = 88;
}
if (a > 3)
{
Temp2 = Buff[a];
Buff[a] = Temp1;
Temp1 = Temp2;
}
a++;
}
return a;
}
int search(int buff[],int len,int c)//c是要查找的數據
{
int a = 0;
while (a<len)
{
if (buff[a] == c)
{
return a;
}
a++;
}
return 0;
}
int main()
{
int i = 0;
int L[6] = {22,33,44,55,66};//初始化數組L
int bol = insert(L,6);
int d = search(L,6,55);
int e = search(L,6,77);
return 0;
}
❾ 求寫C語言 創建鏈表實例子。要最基本的 包括注釋。
題目:創建固定長度的單向鏈表
程序分析:鏈表是動態分配存儲空間的鏈式存儲結構,
其包括一個「頭指針」變數,其中第0個結點稱為整個鏈表的頭結點,頭結點中存放一個地址,該地址指向一個元素,頭結點一般不存放具體數據,只是存放第一個結點的地址。
鏈表中每一個元素稱為「結點」,每個結點都由兩部分組成:存放數據元素的數據域和存儲直接後繼存儲位置的指針域。指針域中存儲的即是鏈表的下一個結點存儲位置,是一個指針。多個結點鏈接成一個鏈表。
最後一個結點的指針域設置為空(NULL),作為鏈表的結束標志,表示它沒有後繼結點。
使用結構體變數作為鏈表中的結點,因為結構體變數成員可以是數值類型,字元類型,數組類型,也可以是指針類型,這樣就可以使用指針類型成員來存放下一個結點的地址,使其它類型成員存放數據信息。
在創建列表時要動態為鏈表分配空間,C語言的庫函數提供了幾種函數實現動態開辟存儲單元。
malloc()函數實現動態開辟存儲單元:
malloc函數原型為:void *malloc(unsigned int size);
其作用是在內存的動態存儲區中分配一個長度為size的連續空間,函數返回值是一個指向分配域起始地址的指針(類型為void)。如果分配空間失敗(如,內存空間不足),則返回空間指針(NULL)
#include<stdio.h>
#include<malloc.h>
structLNode
{
intdata;
structLNode*next;
};
/*上面只是定義了一個結構體類型,並未實際分配內存空間
只有定義了變數才分配內存空間*/
structLNode*creat(intn)
{
inti;
structLNode*head,*p1,*p2;
/*head用來標記鏈表,p1總是用來指向新分配的內存空間,
p2總是指向尾結點,並通過p2來鏈入新分配的結點*/
inta;
head=NULL;
for(i=1;i<=n;i++)
{
p1=(structLNode*)malloc(sizeof(structLNode));
/*動態分配內存空間,並數據轉換為(structLNode)類型*/
printf("請輸入鏈表中的第%d個數:",i);
scanf("%d",&a);
p1->data=a;
if(head==NULL)/*指定鏈表的頭指針*/
{
head=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
p2->next=NULL;/*尾結點的後繼指針為NULL(空)*/
}
returnhead;/*返回鏈表的頭指針*/
}
voidmain()
{
intn;
structLNode*q;
printf("請輸入鏈表的長度:/n");
scanf("%d",&n);
q=creat(n);/*鏈表的頭指針(head)來標記整個鏈表*/
printf("/n鏈表中的數據:/n");
while(q)/*直到結點q為NULL結束循環*/
{
printf("%d",q->data);/*輸出結點中的值*/
q=q->next;/*指向下一個結點*/
}
}