社区
C语言
帖子详情
求简单C程序,速给分!!
luke5678
2007-06-29 01:12:08
编写一个无表头结点的单链表函数,单链表结构为:
struct link
{int data;
strut link *next;
}
输入数据以0为结束条件
...全文
257
5
打赏
收藏
求简单C程序,速给分!!
编写一个无表头结点的单链表函数,单链表结构为: struct link {int data; strut link *next; } 输入数据以0为结束条件
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
luke5678
2007-06-29
打赏
举报
回复
谢谢大家
jixingzhong
2007-06-29
打赏
举报
回复
根据你的要求,写的一个简单的例子,
包括建立和输出:
#include <stdio.h>
#include <stdlib.h>
typedef struct link
{
int data;
struct link *next;
}node;
node *create(node **h)
{
int i;
*h=(node *)malloc(sizeof(node));
node *tmp=*h, *t;
printf("Please input data, (0 = exit):");
scanf("%d", &i);
while(i != 0)
{
tmp->data = i;
t = tmp;
tmp = (node *)malloc(sizeof(node));
t->next=tmp;
printf("Please input data, (0 = exit):");
scanf("%d", &i);
}
t->next=NULL;
free(tmp);
}
void print(node *h)
{
while(h!=NULL)
{
printf("%d->", h->data);
h = h->next;
}
printf("NULL\n");
}
int main(int argc, char* argv[])
{
node *head=NULL;
create(&head);
print(head);
system("pause");
return 0;
}
bargio_susie
2007-06-29
打赏
举报
回复
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct link)
struct link
{
int data;
struct link *next;
};
struct link *create()
{
int num;
struct link *head = NULL;
struct link *p;
scanf("%d", &num);
while(num != 0)
{
struct link *node = (struct link *)malloc(LEN);
node->next = NULL;
node->data = num;
if (head == NULL)
{
head = node;
p = head;
}
else
p->next = node;
p = node;
scanf("%d", &num);
}
return(head);
}
void print(struct link *head)
{
struct link *p = head;
while(p != NULL)
{
printf("%-3d",p->data);
p = p->next;
}
}
int main()
{
struct link *head1;
printf("input a link(with 0 end):\n");
head1=create();
printf("output link is:\n");
print(head1);
printf("\n");
return 0;
}
LZ快点结帖。。。。^_^
xlbdan
2007-06-29
打赏
举报
回复
为什么要一个不带表头 的呢? 操作比较麻烦的
theendname
2007-06-29
打赏
举报
回复
#include <iostream>
using namespace std;
//结构的定义部分
typedef int ElemType;
typedef struct LNode{
ElemType data;
LNode *next;
} *LinkList;
//结构的生成操作
void InitList(LinkList &L)
{
L=NULL;
}
//结构的清理操作
void ClearList(LinkList &L)
{
while(L)
{
LNode *P=L;
L=L->next;
delete P;
}
}
//结构的销毁操作
void DestroyList(LinkList &L)
{
ClearList(L);
}
//结构的状态查看
bool ListEmpty(LinkList L)
{
return L==NULL;
}
int ListLength(LinkList L)
{
LNode *P=L;
int len=0;
while(P)
{
len++;
P=P->next;
}
return len;
}
void ListOutput(LinkList L)
{
cout<<"(";
LNode *P=L;
while(P&&P->next)
{
cout<<P->data<<",";
P=P->next;
}
if(P) cout<<P->data;
cout<<")"<<endl;
}
void ListInfo(LinkList L)
{
cout<<" ";
ListOutput(L);
if(ListEmpty(L)) cout<<" Empty:Yes"<<endl;
else cout<<" Empty:No"<<endl;
cout<<" Length="<<ListLength(L)<<endl;
}
//结构的查改增删
bool GetElem(LinkList L,int pos,ElemType &e)
{
if(pos<1) return false;
LNode *P=L;
int i=1;
while(P&&i<pos)
{
i++;
P=P->next;
}
if(P)
{
e=P->data;
return true;
}
return false;
}
bool ElemEqual(ElemType e1,ElemType e2)
{
return e1==e2;
}
bool ElemPlus(ElemType e1,ElemType e2)
{
return e1>e2;
}
bool ElemMinus(ElemType e1,ElemType e2)
{
return e1<e2;
}
LNode *LocateElem(LinkList L,ElemType e,bool (*compare)(ElemType,ElemType))
{
LNode *P=L;
while(P&&!(*compare)(P->data,e)) P=P->next;
return P;
}
bool PutElem(LinkList &L,int pos,ElemType e)
{
if(pos<1) return false;
LNode *P=L;
int i=1;
while(P&&i<pos)
{
i++;
P=P->next;
}
if(P)
{
P->data=e;
return true;
}
return false;
}
bool ListInsert(LinkList &L,int pos,ElemType e)
{
if(pos<0) return false;
LNode *P=L;
int i=1;
while(P&&i<pos)
{
i++;
P=P->next;
}
if(pos>0&&!P) return false;
LNode *Q=new LNode;
Q->data=e;
if(pos){
Q->next=P->next;
P->next=Q;
}
else{
Q->next=L;
L=Q;
}
return true;
}
bool ListDelete(LinkList &L,int pos,ElemType &e)
{
if(!L||pos<1) return false;
LNode *P=L;
if(pos==1) L=L->next;
else{
LNode *Q=L;
int i=1;
while(Q&&i<pos-1)
{
i++;
Q=Q->next;
}
if(!Q||!Q->next) return false;
P=Q->next;
Q->next=P->next;
}
e=P->data;
delete P;
return true;
}
//结构的功能测试
int main()
{
cout<<"无头结点的单链表结构的测试:"<<endl;
LinkList A;
cout<<"***************************************"<<endl;
cout<<"生成操作:InitList(A)"<<endl;
InitList(A);
ListInfo(A);
cout<<"***************************************"<<endl;
cout<<"插入操作:ListInsert(A,0,2)"<<endl;
ListInsert(A,0,2);
ListInfo(A);
cout<<"***************************************"<<endl;
cout<<"插入操作:ListInsert(A,1,7)"<<endl;
ListInsert(A,1,7);
ListInfo(A);
cout<<"***************************************"<<endl;
cout<<"插入操作:ListInsert(A,2,8)"<<endl;
ListInsert(A,2,8);
ListInfo(A);
cout<<"***************************************"<<endl;
cout<<"插入操作:ListInsert(A,2,21)"<<endl;
ListInsert(A,2,21);
ListInfo(A);
cout<<"***************************************"<<endl;
cout<<"插入操作:ListInsert(A,4,15)"<<endl;
ListInsert(A,4,15);
ListInfo(A);
cout<<"***************************************"<<endl;
cout<<"删除操作:ListDelete(A,5,e)"<<endl;
ElemType e;
ListDelete(A,5,e);
ListInfo(A);
cout<<" e="<<e<<endl;
cout<<"***************************************"<<endl;
cout<<"更新操作:PutElem(A,4,39)"<<endl;
PutElem(A,4,39);
ListInfo(A);
cout<<"***************************************"<<endl;
cout<<"查找操作:GetElem(A,2,e)"<<endl;
GetElem(A,2,e);
ListInfo(A);
cout<<" e="<<e<<endl;
cout<<"***************************************"<<endl;
cout<<"定位操作:LocateElem(A,15,ElemEqual)"<<endl;
ListInfo(A);
cout<<" Address="<<LocateElem(A,15,ElemEqual)<<endl;
cout<<"***************************************"<<endl;
cout<<"定位操作:LocateElem(A,6,ElemPlus)"<<endl;
ListInfo(A);
LNode *R=LocateElem(A,6,ElemPlus);
cout<<" Address="<<R<<endl;
if(R) cout<<" "<<R->data<<endl;
cout<<"***************************************"<<endl;
cout<<"定位操作:LocateElem(A,8,ElemMinus)"<<endl;
ListInfo(A);
cout<<" Address="<<LocateElem(A,8,ElemMinus)<<endl;
cout<<"***************************************"<<endl;
cout<<"清空操作:ClearList(A)"<<endl;
ClearList(A);
ListInfo(A);
cout<<"***************************************"<<endl;
cout<<"销毁操作:DestroyList(A)"<<endl;
DestroyList(A);
ListInfo(A);
cout<<"按任意键,结束...";
cin.get();
return 0;
}
最速下降法
简单
C
程序
简单
的最速下降法学习
程序
,很小的一个C
程序
,用来学习
简单
算法,对初学者有用!
51单片机c语言
程序
设计-可调电子钟
单片机设计c语言设计 速学教程实例 进阶篇 之可调电子钟日历钟
C语言及
程序
设计初步
课程针对没有任何
程序
设计基础的初学者,全面介绍C语言及利用C语言进行
程序
设计的方法。课程注重知识的传授,更关注学习者能够通过实践的方式,真正学会利用C语言解决问题,奠定
程序
设计的基础。为此,专门设计了...
28byj步进减速电机调速c
程序
本
程序
适用于四相五线步进减速电机。
程序
付有注释,
简单
易懂,望采纳
实用51单片机控制直流马达
程序
(含电风扇4档调速
程序
)
此资源主要是51单片机控制直流马达
程序
,可扩展为调速电风扇的
程序
编写,本
程序
以51C语言为基础,用keilc软件编写,并通过proteus软件仿真,本资源中有直接编写好的
程序
和proteus仿真图,下载后可直接演示功能,此...
C语言
69,373
社区成员
243,080
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章