数据结构单链表代码(数据结构单链表代码实现)
数据结构C语言单链表的创建,插入删除和合并程序代码
你看这个应该满足要求吧。我把三种循环方式都用上了:
#includestdio.h
#includemath.h
int isprime(int n)
{
int i,t;
if(n==2)
return 1;
if(n%2==0 || n2)
return 0;
for(i=3,t=(int)sqrt(n);i=t;i+=2)
{
if(n%i==0)
return 0;
}
return 1;
}
void main()
{
int i,a,n;
i=0;
do
{
printf("Input an integer (=1):");
scanf("%d",a);
if(a=1)
break;
}while(++i3);
if(i==3) exit(0);
printf("prime submultiples:\n");
i=1;
n=0;
while(i=a)
{
if(a%i==0)
if(isprime(i))
{
printf("%d ",i);
n++;
if(n%10==0)
printf("\n");
}
i++;
}
数据结构 单链表实验代码
#include?iostream
#include?iomanip
#include?string
?
using?namespace?std;
class?studentshead
{
????public:
???studentshead(){
???first=NULL;
???}
????class?studentshead?*first;
????string?card;
????string?name;
????string?year;
????string?num;
????????friend?istream??operator??(istream?,?studentshead*?);??
????friend?ostream??operator??(ostream?,?studentshead*?);???
};
?istream?operator??(istream?is,studentshead*?stu)
?{
?isstu-cardstu-namestu-yearstu-num;
?????????}
?ostream?operator??(ostream?os,studentshead*?stu)
?{
???osstu-card'?'stu-name'?'stu-year'?'stu-num;
???}
?
int?main()
{
????char?flag='Y';
????class?studentshead?*A,*head;
????A=new?studentshead;
????head=A;
????cinA;
????while(flag!='N')
????{
????????cout"输入?(Y/N)(区分大小写)";
????????cinflag;
????????if(flag=='Y')
????????{
????????A-first=new?studentshead;
????????A=A-first;
????????cinA;??
????????}
???????else?if(flag!='Y'flag!='N')
????????cout"error?input!"endl;
????}
????A=head;
????while(A-first!=NULL)
????{
????????coutAendl;
????????A=A-first;
????????}
????coutAendl;
}
数据结构求代码 实现单链表的基本运算 要求是:依次用头插法插入a、b、c、d、e元素 求代码!!
#include?stdio.h
#include?stdlib.h
#include?string.h
#define?elemType?char
#define?status?int
#define?OVERFLOW?-1
#define?ERROR?0
#define?OK?1
/*?单链表数据结构?*/
typedef?struct?lNode?{
elemType?data;
struct?lNode?*next;
}?lNode,?*linkList;
/********************************?以下为函数声明?********************************/
void?initList?(linkList?*L); /*?初始化?*/
status?listInsertNode?(linkList?L,?elemType?e); /*?单链表第1个位置插入新元素?*/
status?listPrint?(linkList?L); /*?输出链表?*/
/********************************?以上为函数声明?********************************/
/*?初始化?*/
/*?操作结果:构造一个空的单链表L?*/
void?initList?(linkList?*L)?{
*L?=?(linkList)?malloc?(sizeof?(struct?lNode));?/*?产生头节点,并使L指向此头节点?*/
if(!*L)?/*?内存分配失败?*/
exit?(OVERFLOW);
(*L)-next?=?NULL;?/*?指针域为空?*/
}
/*?单链表指定位置插入新元素?*/
/*?操作结果:在带头结点的单链表L中第1个位置之前插入元素e?*/
status?listInsertNode?(linkList?L,?elemType?e)?{
int?j=0;
linkList?p=L,s;
/*?生成新结点,并插入L中?*/
s?=?(linkList)?malloc?(sizeof?(struct?lNode));
s-data?=?e;
s-next?=?p-next;
p-next?=?s;
return?OK;
}
/*?输出链表?*/
status?listPrint?(linkList?L)?{
linkList?p?=?L-next;?/*?p指向第一个结点?*/
while?(p!=NULL)?{
printf?("%c\n",p-data);
p?=?p-next;
}
return?OK;
}
int?main?(void)?{
linkList?L;
elemType?*e;
/*初始化链表*/
initList?(L);
/*头插法插入5个元素*/
listInsertNode?(L,?'a');
listInsertNode?(L,?'b');
listInsertNode?(L,?'c');
listInsertNode?(L,?'d');
listInsertNode?(L,?'e');
puts?("已在链表中插入a~e的5个元素!");
putchar?('\n');?
/*打印链表*/
puts?("当前链表内容为:");
listPrint?(L);
putchar?('\n');
getch?();
return?0;
}
运行结果