线性表的定义
----- ----- --- --- ---- ------- ----- ---- --- --
线性表是一种最简单的数据结构,它是一个有限的元素的系列,可以是数字,字符,文件;除了第一个和最后一个只有唯一的前继或后继,其它的元素都只有只唯一的前继和后继。
线性表在逻辑结构上呈线性的,它的物理存储可以以顺序存储或链式存储来实现。以顺序存储的叫顺序表;以链式存储的叫做线性链表.
---------------------------------------------------
顺序表:在物理上是连续存储的。
线性链表:在物理上任意的存储单元,可以相邻,可以不相邻。每个存结点包含数据域和指针域。用指针相连从而保持了线性表的逻辑上线性关系。
[注]:
头指针与头结点的区别:
头指针只相当于结点的指针域,头结点即整个线性链表的第一个结点,它的数据域可以放数据元素,也可以放线性表的长度等附加信息,也可以不存储任何信息。
第一题:利用顺序结构实现线性表的基本运算
考核内容:
1. 利用顺序结构定义线性表数据类型 seqlist
2. 实现线性表的基本运算,包括:线性表的建立、输出、插入、删除。各函数的说明如下:
3. 建立 input ( seqlist *list ) 输入线性表 list 各元素的数据,建立线性表
4. 输出 output (seqlist *list ) 输出线性表 list 各元素的值
5. 插入 insert (seqlist *list , int pos , datatype data ) 在线性表 list 的第 pos 个位置上插入数据值为 data 的一个新元素。
6. 删除 delete (seqlist *list , int pos ) 删除线性表 list 的第 pos 个位置上的元素。
7. 算法分析,分析时间复杂度和空间复杂度
参考答案:
头文件:sqlist.h
#include " stdio.h "
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
#define UNDERFLOW -1
#define MAXSIZE 100
typedef int Elemtp;
typedef struct {
Elemtp elem[MAXSIZE];
int len;
}Sqlist;
源文件 sqlist.c
main()
{
Sqlist * L;
int x,i,y,aa=1 ;
while (aa)
{
printf("/n/n" );
printf(" 1.Creat orderlist./n" );
printf(" 2.Insert number./n" );
printf(" 3.Delete number./n" );
printf(" 4.Print the orderlist./n" );
printf(" 0.EXIT./n/n" );
printf("Please select mark!(1/2/3/4/0):" );
scanf("%d",& x);
switch (x)
{
case 1 :
L= creat_Sqlist();
break ;
case 2 :
printf("Input the number! x=" );
scanf("%d",& y);
printf("Input the place! i=" );
scanf("%d",& i);
Sq_insert(L,i,y);
break ;
case 3 :
printf("Input the place! i=" );
scanf("%d",& y);
Sq_delete(L,y);
break ;
case 4 :
prt_list(L);
break ;
case 0 :
return 0 ;
default :
aa=0 ;
break ;
}
}
}
Sqlist * L;
Sq_insert(Sqlist *L ,int i, Elemtp x)
{
int j;
if(i<0 || i>L-> len)
return ERROR;
if ( L->len==MAXSIZE-1 )
return OVERFLOW;
for (j=L->len;j>i-1;-- j)
L->elem[j]=L->elem[j-1 ];
L->elem[i-1]= x;
++L-> len;
return OK;
}
int Sq_delete (Sqlist *L,int i)
{
int j;
if (i<1 || i>L-> len)
return ERROR;
if (L->len==0 )
return UNDERFLOW;
for (j=i-1;j<=L->len-1;j++ )
L->elem[j]=L->elem[j+1 ];
--L-> len;
return OK;
}
Sqlist * creat_Sqlist()
{
int i=0 ,x;
L=(Sqlist *)malloc(sizeof (Sqlist));
if(!L-> elem) exit(OVERFLOW);
printf("Input the numbers! /n" );
do
{
scanf( "%d",& x);
L->elem[i]= x;
i++ ;
}
while(x!=0 );
L->len=i-1 ;
return L;
}
prt_list(Sqlist * L)
{
int i;
for(i=0;i<L->len;++ i)
printf("%5d",L-> elem[i]);
printf("/n" );
}
第二题:利用链式结构实现线性表的基本运算
考核内容:
1. 利用链式结构定义线性表数据类型 linklist
2. 实现线性表的基本运算,包括:线性表的建立、输出、插入、删除等。各函数的说明如下:
3. 建立 creat ( linklist *list ) 输入线性表 list 各元素的数据,建立线性表
4. 输出 output (linklist *list ) 输出线性表 list 各元素的值
5. 取结点 get ( linklist *list, int i) 返回线性表中的第 i 个结点
6. 插入 insert (linklist *list , int pos , datatype data ) 在线性表 list 的第 pos 个位置上插入数据值为 data 的一个新元素。
7. 删除 delete (linklist *list , int pos ) 删除线性表 list 的第 pos 个位置上的元素。
8. 算法分析,分析时间复杂度和空间复杂度
参考答案:
#include "stdlib.h"
struct link{
int data;
struct link * next;
}linklist;
linklist *head,* p;
void main()
{
int n,pos; char ch;
void build( int n );
linklist * get ( int pos);
void insert ( int pos );
void delete_node ( int pos );
void output( void );
head= NULL;
printf("input the number of node:/t" );
scanf("%d",& n);
build(n);
getchar();
printf("/n1--insert 2--delete 0--exit/t" );
ch= getchar();
while(ch!='0' )
{
switch (ch)
{
case '1' :
printf("input the insert position : /t" );
scanf("%d",& pos);
insert( pos );
output();
break ;
case '2' :
printf("input the delete position : /t" );
scanf("%d",& pos);
delete_node( pos );
output();
break ;
case '0' :
break ;
default :
printf("input error! input again!" );
}
getchar();
printf("/n1--insert 2--delete 0--exit/t" );
ch= getchar();
}
}
void build( int n )
{
linklist *q; char ch;
int i;
printf("/n input %2d data: /t" ,n);
head=(struct link *)malloc(sizeof(struct link));
q= head;
scanf("%d",&q-> data);
for(i=2;i<=n;i++ )
{
q->next=(struct link *)malloc(sizeof(struct link));
q=q-> next;
scanf("%d",&q-> data);
}
q->next= NULL;
}
linklist* get( int pos )
{
linklist * p;
int i=0 ;
if ( pos <1 )
{
printf("position out of scope. /n" );
return (NULL);
}
p= head;
while ( p->next && i< pos)
{
i++ ;
p=p-> next;
}
return (p);
}
void insert( int pos )
{
int i=1 ;
struct link * q;
p=get (pos);
if (p)
{
q=(struct link *)malloc(sizeof(struct link));
printf("/n input insert data: /t" );
scanf("%d",&q-> data);
q->next=p-> next;
p->next= q;
}
}
void delete_node(int n,int pos)
{
int i=1 ;
struct link * q;
p=get (pos);
if (p)
{
q=p-> next;
p->next=q-> next;
free(q);
}
}
void output( )
{
struct link * q;
q= head;
while (q!= NULL)
{
printf("%3d",q-> data);
q=q-> next;
}
}