#include"1.h"
void main()
{
char a[5]={'a','b','c','d','e'}
int n=5
char f='f',b='a',e
SqList sq
InitList(sq) //初始化表
CreateList(sq,a,n) //传入数据
DispList(sq) //输出表
printf("sq.length=%d\n",ListLength(sq)) //输出表长
if(ListEmpty(sq)) //判断是否为空表
printf("sq是空表\n")
else
printf("sq不是空表\n")
printf("a在第%d位\n",LocateElem(sq,b)) //按元素值查找
ListInsElem(sq,f,4) //在第4个位置上插入f元素
DispList(sq) //输出表
printf("\n")
DelElem(sq,3,e) //删除第三个元素
DispList(sq) //输出表
}
这是头文件
1.h
#include<stdio.h>
const MaxSize=100
typedef struct SqList
{
char elem[MaxSize]
int length
}SqList,*PSqList
SqList sq
void InitList(SqList &sq) //初始化线性表
{
sq.length=0
}
void CreateList(SqList &sq,char a[],int n) //建立表
{
int i
for(i=0i<ni++)
sq.elem[i]=a[i]
sq.length=n
}
int ListLength(SqList sq) //求表长
{
return(sq.length)
}
char GetElem(SqList sq,int i) //求第i个元素
{
if(i<1||i>sq.length)
return 0
return sq.elem[i-1]
}
int LocateElem(SqList sq,char x) //按元素值查找
{
int i=0
while(i<sq.length&&sq.elem[i]!=x)
i++
if(i>sq.length)
return 0
else return i+1
}
int ListInsElem(SqList &sq,char x,int i) //插入元素在第i个位置
{
int j
if(i<1||i>sq.length+1)return 0
for(j=sq.lengthj>=ij--)
sq.elem[j]=sq.elem[j-1]
sq.elem[i-1]=x
sq.length++
return 1
}
int DelElem(SqList &sq,int i,char &e) //删除第i个元素
{
int j
if (i<1||i>sq.length)return 0
e=sq.elem[i]
for(j=ij<sq.lengthj++)
sq.elem[j-1]=sq.elem[j]
sq.length--
return 1
}
int ListEmpty(SqList &sq) //判断表是否为空
{
return(sq.length==0)
}
void DispList(SqList sq) //输出表
{
int i
if(ListEmpty(sq))return
for(i=0i<sq.lengthi++)
printf("%c\t",sq.elem[i])
}
#include"stdio.h"
#include
"malloc.h"
#define
null
0
struct
node
/*定义结构体*/
{int
data
struct
node
*next
}
struct
node
*head
struct
node
*p
struct
node
*s
void
creat()
/*创建单链表*/
{int
ch
head=(struct
node
*)malloc(sizeof(struct
node))
head->next=null
p=head
printf("请输入数据:
")
scanf("%d",&ch)
while(ch!=-1)
{s=(struct
node
*)malloc(sizeof(struct
node))
s->data=ch
s->next=null
p->next=s
p=s
printf("请输入数据:
")
scanf("%d",&ch)
}
}
void
outline()
/*输出单链表*/
{p=head->next
while(p!=null)
{printf("%d
",p->data)
p=p->next
}
printf("\n")
}
int
locate(int
x)
/*按值查找*/
{p=head->next
while((p!=null)&&(p->data!=x))
p=p->next
if(p==null)
return
0
else
return
(p->data)
}
main()
/*主函数*/
{int
a=0
creat()
outline()
printf("请输入你要找的数:\n")
scanf("%d",&a)
printf("你要找的数的下标是:
%d\n",locate(a))
printf("结点个数是:
%d\n",countnode())
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)