‘壹’ 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类
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;
}