c语言数据结构菜鸟教程(数据结构 用C语言实现顺序表的建立及遍历)

本文目录
数据结构 用C语言实现顺序表的建立及遍历
#include 《stdio.h》
#include 《malloc.h》
typedef struct nlist
{
int *np;
int len;//已使用地址个数
int maxlen;//最大地址个数
}NLT;
NLT *createlist();//创建顺序表
int addtolist(NLT *nlist);//向顺序表插入元素
void pList(NLT *nlist);//遍历顺序表
int main()
{
int i;
NLT *nlist=createlist();
if(nlist)
{
for(i=0;i《nlist-》maxlen;i++)
addtolist(nlist);
pList(nlist);
}
return 0;
}
void pList(NLT *nlist)//遍历打印,空格分割,最后一个数后面没有空格
{
int i;
for(i=0;i《nlist-》len-1;i++)
printf("%d ",nlist-》np);
printf("%d",nlist-》np);
}
NLT *createlist()
{
NLT *nlist=NULL;
nlist=(NLT *)malloc(sizeof(NLT));
scanf("%d",&nlist-》maxlen);
nlist-》np=(int *)malloc(sizeof(int)*nlist-》maxlen);
if(!nlist || !nlist-》np)
{
printf("内存申请失败!\n");
return NULL;
}
nlist-》len=0;
return nlist;
}
int addtolist(NLT *nlist)
{
if(nlist-》len《nlist-》maxlen)//如果存储空间未满,保存元素,保存成功返回1 失败返回0
{
scanf("%d",&nlist-》np);
nlist-》len++;
return 1;
}
else //这里可以写当存储满的之后,空间扩容,本题用不到所以我不写了
return 0;
}
C语言中的数据结构问题
/*
你好, 程序思路是:
先把输入的东西一起入栈和入队列.
如Q, a, b, c, b, a
S, a, b, c ,b, a
等用户输入@后, Q出队列, S出栈,
判断两个出栈的内容是否一致, 是否同是为空, 如果不是, 说明不是回文.
// 此程序运行正确.
*/
#include 《stdio.h》
#include 《stdlib.h》
#include 《malloc.h》
#include 《string.h》
#define MAXSIZE 100
/*#include "debug_zhu.h"*/
typedef char DataType;
typedef struct{
DataType data;
int top;
}SeqStack,*PSeqStack;
typedef struct{
DataType data;
int front,rear;
}SeqQueue,*PSeqQueue;
PSeqStack Init_SeqStack(void)
{
PSeqStack S;
S=(PSeqStack)malloc(sizeof(SeqStack));
bzero(S, sizeof( SeqStack));
if(S)
S-》top=-1;
return S;
}
char Push_SeqStack(PSeqStack S,DataType x)
{
if (S-》top==MAXSIZE-1){
return 0;
} else {
S-》top++;
S-》data=x;
return 1;
}
}
char Empty_SeqStack(PSeqStack S)
{
if(S-》top==-1){
return 1;
} else{
return 0;
}
}
char Pop_SeqStack(PSeqStack S,DataType *x)
{
if(Empty_SeqStack(S)){
return -1;// 失败为 -1// zhuxh 出栈和出队列的返回值要一致.
} else {
*x=S-》data;
S-》top--;
return 0;// 成功为 0 // zhuxh
}
}
PSeqQueue Init_SeqQueue()
{
PSeqQueue Q;
Q=(PSeqQueue)malloc(sizeof(SeqQueue));
bzero(Q, sizeof( SeqQueue));
if(Q){
Q-》front=0;
Q-》rear=0;
}
return Q;
}
char Empty_SeqQueue(PSeqQueue Q)
{
if(Q && Q-》front==Q-》rear)
return (1);
else
return (0);
}
char In_SeqQueue(PSeqQueue Q,DataType x)
{
if( (Q-》rear+1)%MAXSIZE == Q-》front) {
printf("队满");
return -1;
} else {
Q-》rear=(Q-》rear+1)%MAXSIZE;
Q-》data=x;
return 1;
}
}
char Out_SeqQueue(PSeqQueue Q,DataType *x)
{
if(Empty_SeqQueue(Q))
{
printf("队空");
return -1;
} else {
Q-》front=(Q-》front+1)%MAXSIZE;
*x=Q-》data;
return 0;
}
}
int compare(char ch)
{
char ch_Statck;
char ch_Queue;
PSeqStack S;
S = Init_SeqStack();
Push_SeqStack(S,ch);
PSeqQueue Q;
Q = Init_SeqQueue();
In_SeqQueue(Q, ch);
bool bIn = true;// 入栈.
while( (ch = getchar())!=’@’) {
Push_SeqStack(S, ch);
In_SeqQueue(Q, ch);
}
printf(" Queue = %s \n", Q-》data + 1 );
printf(" stack = %s \n", S-》data );
int ret = -1;
do{
// 请确保 Out_SeqQueue 和 Pop_SeqStack 返回值一致.
if( Out_SeqQueue(Q, &ch_Queue ) == 0 && Pop_SeqStack(S, &ch_Statck ) == 0 ){
if( ch_Queue != ch_Statck ){
/*printf("111!!!!!!!! \n");*/
break;
}
}else{
/*printf("222 !!!!!!!! \n");*/
break;
}
/*printf(" ch_Queue = %c , ch_Statck = %c", ch_Queue, ch_Statck );*/
}while (1);
if( (ch_Queue == ch_Statck) && (Empty_SeqQueue(Q) && Empty_SeqStack(S))){
printf("\n 匹配\n");
}else {
// 未完成比较
printf("\n 不匹配\n");
}
// 要记得内存释放.
free( Q );
Q = NULL;
free(S);
S = NULL;
}
int main(int argc, char ** argv )
{
char a;
printf("请输入字符串");
scanf("%c",&a);
compare(a);
return 0;
}
请C语言版数据结构高手帮帮忙!
//参照书本,经测试,此代码可以。使用了一维数组实现循环队列。
#include《stdio.h》
#define MaxSize 10 //预设队列大小
int queue;
int front=0,rear=0; //队列头,队列尾
int qlen=0;
//判断是否队空
bool isEmpty()
{
if(front==rear)return true;
return false;
}
//判断是否队满
bool isFull()
{
if(qlen==MaxSize-1)return true;
return false;
}
void EnQueue() //进队列
{
if(isFull()==true)
{
printf("队列满了");
return ;
}
printf("输入进入队列的元素:");
scanf("%d",&queue);
rear=(rear+1)%MaxSize;
qlen++;//长度增加
}
void DeQueue() //出队列
{
if(isEmpty()==true)
{
printf("队列为空!\n");
return;
}
printf("出队列的元素:%d",queue);
front=(front+1)%MaxSize;
qlen--;//长度减少
}
void print() //打印当前队列
{
int i;
if(qlen==0)
{
printf("队列为空!\n");
return;
}
printf("\n当前队列为:");
if(front》rear)
{
for(i=front-1;i《MaxSize;i++)
{
printf("%d ",queue);
}
for(i=0;i《rear;i++)
{
printf("%d ",queue);
}
}
else
{
for(i=front;i《rear;i++)
{
printf("%d ",queue);
}
}
}
int main()
{
int servse=-1; //服务标识
while(servse!=0)
{
printf("\n1.元素进队\n");
printf("2.元素出列\n");
printf("3.打印队列\n");
printf("0.退出\n");
printf("选择:");
scanf("%d",&servse);
switch(servse)
{
case 1:EnQueue();break;
case 2:DeQueue();break;
case 3:print();break;
case 0:break;
}
}
}
用C语言实现数据结构中常用算法,如对链表的操作、查找、排序等
调试过的
没什么大的毛病
#include 《stdio.h》
#include 《stdlib.h》
#include 《iostream》
typedef int ElemType;
typedef struct LNode {
ElemType date;
struct LNode *next;
}linklist,*link;
/*构造链表*//////////////////////////////////////
void IinitList(link &L)
{
if(L)delete L;
L= (link)malloc(sizeof(LNode)) ;
if (!L) exit(1);
L-》next=NULL;
cout《《"链表已经建立\n";
}
//////////////////////////////////////////////////////
// /*删除结点*/// //////////////////////////////////////////////
int listdelete(link &L,int i,ElemType &e)
{
link p,q; int j;
p=L;j=0;
while(p-》next&&j《i-1)
{
p=p-》next;++j;
}
q=p-》next;
p-》next=q-》next;
e=q-》date;free(q);
cout《《"链表已经删除\n";
return 1;
}
////////////////////////////////////////////// /////////
// /*插入结点*/////////////// ///////////////////////
int listinsert(link &L,int i,ElemType e)
{
link p,q;
int j;
p=L;j=0;
while(p&&j《i-1)
{
p=p-》next;++j;
}
q= (link)malloc(sizeof(LNode));
q-》date=e;
q-》next=p-》next;
p-》next=q; cout《《"链表已经插入\n";
return 1;
}
/////////////////////////////////////////////////////
////*显示数据*///////// ////////////////////////////////
void show(link l)
{ link p; int j;
p=l;j=0;
cout《《"链表的值为:\n";
while(p-》next)
{
cout《《p-》next-》date《《endl;
p=p-》next;
}
}
//////////////////////// /////////////////////////////////
//////销毁链表////// ////////////////////////////////////////
void destorylinst(link &L)
{
while(L)
{ link p=L;
L=L-》next;
free(p) ;
}
L=NULL;
}
////// 打印表头///////////////////////////////////////
void print()
{
cout《《"------------------------\n";
cout《《"------------------------\n";
}
////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
////查找结点//// ////////////////////////////////////////
void lookfor(link l,int e)
{
if(l==NULL)
cout《《"链表未建立,请先构造链表\n" ;
else{
link p; int i=0,j=0;
p=l-》next;
cout《《"你查找值的位置是:\n " ;
while(p)
{ if(p-》date==e)
{ j++;
cout《《i+1《《endl;
}
p=p-》next; i++;
}cout《《"查找完毕\n";
if(j==0)
cout《《"你查找的值不在链表中 、\n";
} }
void putline(link &l)
{
if(l==NULL ||l-》next==NULL )
cout《《"链表未建立或是空的,请先构造链表\n" ;
else{
link p,q;
p=l-》next;
while(p!=NULL)
{
q=p-》next;
while(q!=NULL)
{
if(p-》date》q-》date)
{ ElemType t;
t=p-》date;
p-》date=q-》date;
q-》date=t;
}
q=q-》next;
}
p=p-》next;
} cout《《"链表已经排序 \n";
}
}
/////////////////////////////// //////////////////
///////////////////////////////////////////////////
//////测试函数///// /////////////////////
void main()
{ link L=NULL; int k;
while(1)
{
cout《《"按0退出\n"《《"按1建立\n"《《"按2插入\n"《《"按3删除\n"
《《"按4清空链表\n"《《"按5查找\n"《《"按6进行排续\n" ;
print();
int a,i,j;
cin》》a;
switch(a)
{ case 0: if(L!=NULL)
destorylinst(L) ;
exit(1);
case 1:
IinitList(L);
k=0;
print();
show(L) ;
cout《《"空的链表\n";
cout《《"链表长度为: "《《k《《endl;
print();
cout《《"是否要给链表插入值:y----n\n";
char yy;
yy=getchar();
if(yy==’y’)
{
cout《《"请输入值!按回车键后输入下一个,输入0再按回车结束\n";
int bb;
cin》》bb;
while(bb!=0)
{ k++;
listinsert(L,k,bb) ;
cin》》bb;
}
print();
show(L) ; cout《《"链表长度为: "《《k《《endl;
}
else break;
print();
break;
case 2:
if(L!=NULL)
{
cout《《"输入位置:\n";
cin》》i;
while(i》k+1 || i《1)
{
cout《《"位置错误,重新输入插入位置\n" ;
cin》》i;
}
cout《《"输入植;\n";
cin》》j;
listinsert(L,i,j) ;
k++;
print();
show(L);
cout《《"链表长度为:"《《k《《endl;
print();
}
else
{ cout《《"链表不存在,请先建链表\n";
print(); }
break;
case 3:
if(L!=NULL)
{
cout《《"输入位置:\n";
cin》》i;
while(i》k || i《1)
{
cout《《"位置错误,重新输入删除位置\n" ;
cin》》i;
}
listdelete (L,i,j);
cout《《"你删除的是:\n";
cout《《j《《endl ;
k--; print();
show(L);
cout《《"链表长度为:"《《k《《endl;
print();
}
else {
cout《《"链表不存在,请先建链表\n";
print();
}
break;
case 4:
destorylinst(L) ;
cout《《"链表已经清空\n";
print();
break;
case 5:
print();
cout《《"输入要查找的值;\n";
int z;
cin》》z;
lookfor(L,z);
print();
break;
case 6:
putline(L);
if(L!=NULL)
show(L);
print();
break;
default:
break ;
}
}
delete L;
}

更多文章:
excel+条件格式设置好第一行批量(excel条件格式批量)
2026年8月31日 07:00
国内外贸电商有没有什么好系统可以建站的?做电商软件的企业有哪些
2026年7月3日 07:30
获取request对象(java怎么获取request对象)
2025年5月31日 06:15
工作流技术有哪些(翼发云OA办公系统用的什么技术来实现可视化工作流的)
2025年8月2日 22:15
index第二个参数的含义(如何使用index和match函数:)
2025年12月18日 13:00
alter table rename to是什么命令(如何给列重命名 SQL)
2026年8月28日 21:30
oracle19c32位客户端下载(oracle32位客户端以64位运行怎么解决)
2026年5月16日 14:00
get the actors positioned(高中英语作文:落下的书本 The Left Books)
2026年7月23日 16:15














