数据结构实验一报告.docVIP

  • 10
  • 0
  • 约2.44千字
  • 约 5页
  • 2015-08-11 发布于重庆
  • 举报
数据结构实验一报告.doc

湖南师范大学工程与设计4~2015学年第二学期 实验(一) 实验时间 2015-06-15 实验地点 中栋606 实验题目 线性表的存储及操作 实验目的 1.掌握顺序存储结构和链式存储结构的特点; 2.掌握常见算法。 实验内容 已知两个按元素值有序的线性表A和B,编程实现:将A和B有序归并成一个按元素值有序的线性表。 算法基本思想: 定义动态一维数组 .对两线性表进行比较,较小的值放到新的线性表,若比较过程中有一线性表的值为0,则结束比较,将另一线性表的值全部防放到线性表。 3.显示新的线性表 二、结构定义: Sturct Node { Int data ; Struct Node *next; } ; Typedef Struct Node ListNode; 三|、算法描述: Typedef Struct Node ListNode ; ListNode *creatList(int data); Void compare(ListNode *head1 , ListNode *head2 , int c[20]); void main() { int c[20]={0} ; ListNode *ad,*bd; ad=creatList(4) ; bd=creatList(7) ; compare(ad,bd,c); } 程序清单: 链式存储: #include stdio.h #includestdlib.h struct Node { int a ; struct Node * next ; }; typedef struct Node ListNode; ListNode *creatList(int n) { int i=0,j=0,aa; ListNode *head ; ListNode *p,*pr ; head=(ListNode*)malloc(sizeof(ListNode)); head-next=NULL; pr=head ; for(i ; in ; i++) { p=(ListNode*)malloc(sizeof(ListNode); printf(please enter a number : ); scanf(%d , aa); p-a=aa; pr-next=p ; pr=p ; pr-next=NULL ; } return head ; } void compare(ListNode * head1 ,ListNode * head2 , int c[20]) { int i=0 ,ji; head1=head1-next ; head2=head2-next ; while(head1 !=NULL head2 != NULL) { if ((head1-a) = (head2-a)) { c[i] = head2-a ; i++ ; head2=head2-next ; else { c[i] = head1-a ; i++ ; head1=head1-next; } } if (head1==NULL) { while(head2 != NULL) { c[i]=head2-a ; i++ ; head2=head2-next; } } if (head2==NULL) { while(head1 != NULL) { c[i]=head1-a ; i++ ; head1=head1-next ; } } for(ji=0 ; ji11 ; ji++) { printf( %d ,c[ji]); } } void main() { int c[20]={0} ; ListNode *ad,*bd; ad=creatList(4) ; bd=creatList(7) ; compare(ad,bd,c); } 运行结果: please enter a number : 3 please enter a number : 5 please enter a number : 8 please enter a number : 3 please enter a number : 2 please enter a number : 6 plea

文档评论(0)

1亿VIP精品文档

相关文档