当前位置:首页 » 编程语言 » 用c语言创建一个列表
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

用c语言创建一个列表

发布时间: 2022-08-22 12:45:53

❶ 如何用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;
    }

❷ 给出建立 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;/*指向下一个结点*/
}
}