loc=locat L(L,e);if(loc==-1)printf("n未找到%d",loc);elseprintf("ln已找到,元素位置是%d",loc),out_ L(L);) break;// switchprintf("In -");while(k>=1 && k<5);再见!");printf("nprintf("n打回车键,返回。");ch=getcharO); // main建立线性链表(11,22,33)LNode *creat_L(){LNode *h,*p,*s; ElemType x;11分配头结点h=(LNode *)malloc(sizeof(LNode);h->next=NULL;p=h;printf("/n输入数据元素(-111结束)data=?");scanf("%d",&x);I输入第一个数据元素while( xI=-111)1输入-111,结束循环/分配新结点( s=(LNode *)malloc(sizeof(LNode);s->data=x, s->next=NULL;p->next=s; p=s,printf("data=?(-111end)");scanf("%d",&x);//输入下一个数据return(h); Il creat_ L//输出单链表中的数据元素void out L(LNode *L){ LNode *p, char ch,12
12 loc=locat_L(L,e); if (loc==-1) printf("\n 未找到 %d",loc); else printf("\n 已找到,元素位置是 %d",loc); out_L(L);} break; } // switch printf("\n -"); }while(k>=1 && k<5); printf("\n 再见!"); printf("\n 打回车键,返回。"); ch=getchar(); } // main // 建立线性链表 ( 11,22 ,33 ) LNode *creat_L( ) { LNode *h,*p,*s; ElemType x; h=(LNode *)malloc(sizeof(LNode)); //分配头结点 h->next=NULL; p=h; printf("\n 输入数据元素(-111 结束)data=?"); scanf("%d",&x); // 输入第一个数据元素 while( x!=-111) // 输入-111,结束循环 { s=(LNode *)malloc(sizeof(LNode)); // 分配新结点 s->data=x; s->next=NULL; p->next=s; p=s; printf("data=?( -111 end) "); scanf("%d",&x); //输入下一个数据 } return(h); } // creat_L //输出单链表中的数据元素 void out_L(LNode *L) { LNode *p; char ch;
p=L->next;printf("ln单链表为:Inn");while(p!=NULL) (printf("%5d",p->data); p=p->next;3;printf("nln打回车键,继续。");ch=getcharO);} // out_link在i位置插入元素evoid insert L(LNode *L,int i, ElemType e){ LNode *s,*p, intj;1找第i-1个结点p=L;j=0;while(p!=NULL && j<i-1) (p=p->next; j++;)if(p==NULL Ij>i-1) printf("n i ERROR !");else ( s=(LNode *)malloc(sizeof(LNode);s->data=e;s->next=p->next,p->next=s;3 // insert_ L//查找值为e的元素,返回它的位置int locat_L(LNode *L,ElemType e)( LNode *p; int j-1;p=L->next;while(p!=NULL && p->data!=e) (p=p->next; j++;)if(p!-NULL)return(G); else return(-1);} /locat_L删除第i个位置的元素,返回它的值ElemType delete_L(LNode *L,int i)13
13 p=L->next; printf("\n 单链表为:\n\n"); while(p!=NULL) { printf("%5d",p->data); p=p->next; }; printf("\n\n 打回车键,继续。"); ch=getchar(); } // out_link // 在 i 位置插入元素 e void insert_L(LNode *L,int i, ElemType e) { LNode *s,*p; int j; p=L; // 找第 i-1 个结点 j=0; while(p!=NULL && j<i-1) { p=p->next; j++; } if(p==NULL || j>i-1) printf("\n i ERROR !"); else { s=(LNode *)malloc(sizeof(LNode)); s->data=e; s->next=p->next; p->next=s; } } // insert_L // 查找值为 e 的元素, 返回它的位置 int locat_L(LNode *L,ElemType e) { LNode *p; int j=1; p=L->next; while(p!=NULL && p->data!=e) {p=p->next; j++;} if(p!=NULL)return(j); else return(-1); } //locat_L // 删除第 i 个位置的元素, 返回它的值 ElemType delete_L(LNode *L,int i)
{LNode *p,*s;int j=0;ElemType x;p=L;while(p->next!=NULL&&j<i-1)(p=p->next,j++: s=p->next;x=s->data,p->next=s->next,free(s);return x;(二)设计实验2、约瑟夫环算法设有n个人坐在圆桌周围,从第s个人开始报数,报到m的人出列,然后再从下一个人开始报数,报到m的人又出列,--如此重复,直到所有的人都出列为止。要求按出列的先后顺序输出每个人的信息。实验程序:#include<stdlib.h>#include<stdio.h>typedef struct node(int data,struct node *next;}LNode,mainO(LNode *Create(int,int);LNode *GetNode (LNode*);int Print(LNode *,int),LNode *p,14
14 {LNode *p,*s; int j=0; ElemType x; p=L; while(p->next!=NULL&&j<i-1) { p=p->next; j++;} s=p->next; x=s->data; p->next=s->next; free(s); return x ; } (二)设计实验 2、约瑟夫环算法 设有 n 个人坐在圆桌周围,从第 s 个人开始报数,报到 m 的人出列,然后再从下一 个人开始报数,报到 m 的人又出列,┅如此重复,直到所有的人都出列为止。要求按出 列的先后顺序输出每个人的信息。 实验程序: #include <stdlib.h> #include <stdio.h> typedef struct node {int data; struct node *next; }LNode; main() {LNode *Create(int,int); LNode *GetNode (LNode*); int Print(LNode *,int); LNode *p;
int n,k,m;do(printf("input person number(n)"),scanf("%d",&n), while(n<=0);do(printf("input begin person numbers(1--%d)",n);scanf("%d",&k); while(k<=0|k>n);do(printf("input instreval number(m)");scanf("%d",&m);while(m<=0);p=Create(n,k);print(p,m),return 0;1;LNode *Create(int n,int k)(int start=k-1;LNode *s,*p,*I=0,*t;if (start==0)start=n;while(n!=0)(s=(LNode*)malloc(sizeof(LNode);if(I-0)p=s;if(n=start) t=s;s->data=n,s->next=l;[=s,n--,15
15 int n,k,m; do {printf("input person number(n)"); scanf("%d",&n); }while(n<=0); do {printf("input begin person numbers(1-%d)",n); scanf("%d",&k); }while(k<=0||k>n); do {printf("input instreval number(m)"); scanf("%d",&m); }while(m<=0); p=Create(n,k); print(p,m); return 0; }; LNode *Create(int n,int k) {int start=k-1; LNode *s,*p,*l=0,*t; if (start==0)start=n; while(n!=0) {s=(LNode*)malloc(sizeof(LNode)); if(l==0)p=s; if(n==start) t=s; s->data=n; s->next=l; l=s; n-;
p->next=l;return t,LNode *GetNode(LNode *p){LNode *q;for(q=p;q->next!=p;q=q->next);q->next=p->next,free(p);return(g);print (LNode *p,int m)(int i,printf ("out number:In");while(p->next!=p)(for (i=1;i<=m;i++)p=p->next;printf("%d",p->data);p=GetNode(p);1printf("%d/n",p->data);return 0;三、思考题线性表的两种存储结构各自的优缺点?1.4小结通过本章学习,掌握线性表的逻辑结构特征,顺序存储和链式存储的特点以及在两种存储结构下基本操作的实现,并应用它解决实际问题。16
16 } p->next=l; return t; } LNode *GetNode(LNode *p) {LNode *q; for(q=p;q->next!=p;q=q->next); q->next=p->next; free(p); return(q); } print (LNode *p,int m) {int i; printf ("out number:\n"); while(p->next!=p) {for (i=1;i<=m;i++) p=p->next; printf("%d",p->data); p=GetNode(p); } printf("%d\n",p->data); return 0; } 三、思考题 线性表的两种存储结构各自的优缺点? 1.4 小结 通过本章学习,掌握线性表的逻辑结构特征,顺序存储和链式存储的特点以及在两 种存储结构下基本操作的实现,并应用它解决实际问题