数据结构第一次上机实验内容
注意:请把实验答案传到电子邮箱:luanch302@163.com,请写上姓名和学号。 第一部分:程序分析题
1、认真阅读分析下面的程序,并上机运行本程序,简述其功能。 程序如下:
#include
{ char name[MaxLength]; int length;
struct LIST *next; }LIST;
LIST a,l,b,c,*p,*head;
void print_list() /* Print all the node of the list. */ {do
{printf(\p=p->next;
}while(p!=NULL); }
insert_element(LIST l,int i,char x) {int k;
if(i<1||i>l.length+1) return(0);
if(l.length>=MaxLength)
{printf(\return(-1); }
for(k=l.length;k>=i-1;k--) l.name[k+1]=l.name[k]; l.name[i-1]=x; l.length++; return(1); }
delete_element(LIST l,int i) {int k;
if(i>l.length||i<1)
{printf(\return(0); }
for(k=i;k LIST *insert_node(LIST *head,char x[MaxLength],int k) {LIST *p,*pre,*s; int j=1; p=head; pre=NULL; while((p!=NULL)&&(j if(j!=k) {printf(\return(0); } s=(LIST *)malloc(sizeof(LIST)); if(s==NULL) printf(\strcmp(s->name,x); if(pre==NULL) /* When k=1. */ {s->next=head; head=s; }else {s->next=pre->next; pre->next=s; } return(head); } delete_node(LIST *head,int i) {LIST *p,*q; q=NULL; p=head; while(i--!=0&&p!=NULL) {q=p; p=p->next; } if(p==NULL) {printf(\return(0); }else {if(q==NULL) head=head->next; else q->next=p->next; free(p); } } main() {int i,choice; char x[MaxLength],element,listname; LIST l; clrscr(); strcpy(a.name,\strcpy(b.name,\strcpy(c.name,\head=&a; a.next=&b; b.next=&c; c.next=NULL; p=head; printf(\print_list(); printf(\chose the function:\\n1.Insert a element in a sequenlist.\\n2.Delete a element in a sequenlist.\\n3.Insert a node in the linklist.\\n4.Delete a node in the link list.\\n5.Quit the programe.\\n\scanf(\switch(choice) {case 5:{printf(\break;} case 1:{printf(\scanf(\ printf(\scanf(\ printf(\scanf(\if(listname=='a') l=a; if(listname=='b') l=b; if(listname=='c') l=c; insert_element(l,i,element); print_list(); } case 2:{printf(\ scanf(\ printf(\scanf(\ if(listname=='a') l=a; if(listname=='b') l=b; if(listname=='c') l=c; delete_element(l,i); print_list(); } case 3:{printf(\scanf(\ printf(\scanf(\ insert_node(head,x,i); print_list(); } case 4:{printf(\scanf(\delete_node(head,i); print_list(); } } } 2、认真阅读分析下面的程序,并上机运行本程序,简述其功能,并写出运行结果。 #include \void main() {int a[20],i,j,x,y; for(i=0;i<20;i++) a[i]=0; printf(\请输入10个非零整数:\\n\for(i=0;i<10;i++) scanf(\ printf(\请输入\\\在x之前插入y\\\中的x,y:\\n\scanf(\ printf(\插入之前顺序表为:\for(i=0;i<10;i++) printf(\printf(\ for(i=10;i>=x;i--) a[i+1]=a[i]; a[i]=y; printf(\插入之后顺序表为:\for(i=0;i<11;i++) printf(\ printf(\ printf(\请输入\\\删除第y个数据\\\中的y:\\n\scanf(\ printf(\删除之前顺序表为:\for(i=0;i<11;i++) printf(\printf(\ for(i=y;i<11;i++) a[i]=a[i+1]; printf(\删除之后顺序表为:\for(i=0;i<10;i++) printf(\printf(\} 键入:1 2 3 4 5 6 7 8 9 10 输出: 3、认真阅读分析下面的程序,并上机运行本程序,简述其功能,并写出运行结果。 #include \#include \void main() {int x,y; struct node {int data; struct node * next; }*p,*q,*head; printf(\请输入10个正整数:\\n\scanf(\ head=(struct node *)malloc(sizeof(struct node)); p=head; p->data=x;p->next=null; for(i=0;i<9;i++) {q=(struct node *)malloc(sizeof(struct node)); scanf(\ q->data=x; q->next=null; p->next=q; p=q; } printf(\请输入在x之后插入y\\\中的x,y:\\n\; scanf(\ printf(\插入之前链表为:\p=head; while(p!=null) {printf(\