當前位置:首頁 » 編程語言 » c語言如何建立一個列表
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言如何建立一個列表

發布時間: 2022-07-25 10:09:12

『壹』 c語言如何定義一個單項列表

#include<iostream>
usingnamespacestd;
structA
{
intx;
A*next;
};
A*Head;//頭指針
intk=0;//記錄創建鏈表個數
A*J_L();//創建鏈表
voidB_J(A*head,intx);//比較大小
voidshow(A*head);//輸出
voidmain()
{
A*t;
t=J_L();
B_J(t,k);
show(t);
}
voidB_J(A*head,intx)
{
A*m,*n;
intswap;
for(inti=0;i<(x-1);i++)
{
n=head;
m=n->next;
for(intj=0;j<(x-i-1);j++)
{
if((n->x)>(m->x))
{swap=n->x;n->x=m->x;m->x=swap;}
n=n->next;
m=n->next;
}
}
}
voidshow(A*head)
{
A*t=head;
while(t!=NULL)
{
cout<<t->x<<endl;
t=t->next;
}
}
A*J_L()
{
cout<<"請輸入,0退出";
A*p=newA;
k++;
A*d=Head=p;
cin>>p->x;
if(p->x==0)
{
k--;
deletep;Head=0;p=0;
}
while(1)
{
cout<<"請輸入,0退出";
p=newA;
k++;
d->next=p;
cin>>p->x;
if(p->x==0)
{k--;deletep;d->next=0;p=0;break;}
d=p;
}
returnHead;
}

『貳』 給出建立 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語言生成數字列表的問題

如果生成的列表是用來列印的:
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語言小難題,時用結構體變數來建立動態內存空間的,建一個3個學生數據的單向動態列表

//正確的代碼在相應注釋裡面
struct Student *creat()
{
n=0;
head=NULL;
p1=p2=(struct student*) malloc (LEN);
scanf("%ld,%5.1f",&p1->num,&p1->score);//scanf("%ld,%f",&p1->num,&p1->score);
//scanf不能使用精度
while (p1->num!=0)
{
n=n+1;
if (n=1) head=p1;//if (n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Student *) malloc(LEN);
scanf("%ld,%5.1f",&p1->num,&p1->score);//scanf("%ld,%f",&p1->num,&p1->score);

}
p2->next=NULL;
return (head);
}

『伍』 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語言創建列表寫程序,標明每一步是做什麼的,越詳細越好,我完全步懂這東西,謝謝

順序表?是數組還是鏈表? 給你寫個簡單的數組吧

#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語言把兩個有序線性列表合並成一個有序線性鏈表(不超過10行的程序)

#include<stdio.h>
#include <malloc.h>
#include<limits.h>
#define MAX 1024
void merge(int a[],int p,int q,int b[],int r,int s,int c[])
{int i,j,n1,n2,n,k,m;
n1=q-p+1;
n2=s-r+1;
int *l=(int *)malloc(sizeof(int)*(n1+1));
for(i=0;i<n1;i++)
l[i]=a[p+i-1];

int *t=(int *)malloc(sizeof(int)*(n2+1)); for(j=0;j<n2;j++)
t[j]=b[r+j-1];

l[n1]=INT_MAX;t[n2]=INT_MAX;
i=0;
j=0;
for(k=0;k<n1+n2;k++)
{if(l[i]<t[j]){c[k]=l[i];
i++;}
else {c[k]=t[j];
j++;}}
for(i=0;i<n1+n2;i++)
printf("%d ",c[i]);
printf("\n");
}

int main()
{
int i;
int arr[] = {4,5,9,1000,1001};
int arr2[] = {1,7,8,999,1003,1005};
int *res = (int*)malloc(sizeof(int) * 100);
merge(arr,1,5,arr2,1,6,res);
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;/*指向下一個結點*/
}
}

『玖』 幫忙寫個循環列表,用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語言或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;
    }