数据结构实验报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
数
据
结
构
实
验
报
告姓名:wangqiang
学号:*********
1线性表的应用
#include
#include
typedef struct node{
int value;
struct node *next;
}NODE;
NODE *createlink(int n){
NODE *head=NULL,*p=NULL,*q=NULL;
int i=1;
head=p=(struct node*)malloc(sizeof(struct node));
p->value=i;
for(i=2;i<=n;i++){
q=(struct node*)malloc(sizeof(struct node));
if(q==0) return 0;
p->next=q;
p=q;
p->value=i;
}
p->next=head;
return head;
}
void jose(NODE *p,int n,int m){
int i,j,g=0;
NODE *q=NULL;
for(i=1;i<=n;i++){
for(j=1;j p=p->next;} q=p->next; p->next=q->next; if(g%5==0) {g++;printf("\n");} else g++; printf("%3d:%3dout ",i,q->value-1); free(q); } printf("\n"); p->next=NULL; } int main( ){ int m=0; int n=0; scanf("%d",&m); scanf("%d",&n); NODE *head=NULL; head=createlink(n); jose(head,n,m); return 0; 2栈和队列的应用 #include"stdio.h" #include"string.h" #include"stdlib.h" #define StackSize 100 //假定预分配的栈空间最多为100个元素#define MaxLength 100 //最大的字符串长度 typedef int DataType; //假定栈元素的数据类型为整数 typedef struct { DataType data[StackSize]; int top; }SeqStack; void Initial(SeqStack*S); int IsEmpty(SeqStack*S); int IsFull(SeqStack*S); void Push(SeqStack*S, DataType x); DataType Pop(SeqStack*S); DataType Top(SeqStack*S); void PrintMatchedPairs(char *expr); void main(void) { char expr[MaxLength]; printf("请输入符号个数小于%d的表达式:\n",MaxLength); gets(expr); printf("括号对是:\n"); PrintMatchedPairs(expr); return; } //置栈空 void Initial(SeqStack*S) { S-> top= -1; } //判断栈是否空 int IsEmpty(SeqStack*S) { return S-> top== -1; } //判断栈是否满 int IsFull(SeqStack*S) { return S-> top== StackSize-1; } //进栈 void Push(SeqStack*S, DataType x) { if(IsFull(S)) { printf("栈上溢!"); exit(1); } S-> data[++ S-> top]= x; return; } //出栈 DataType Pop(SeqStack*S) { if(IsEmpty(S)) { printf("栈为空!"); return -1; } return S-> data[S-> top--];//栈顶指针加1后将x入栈 } //取栈顶元素 DataType Top(SeqStack*S) { if(IsEmpty(S)) { printf("栈为空!"); exit(1); } return S-> data[S-> top]; }