主题:顺序表的建立插入删除查找课程设计
dennis99948
[专家分:0] 发布于 2006-03-21 08:58:00
谁能给我发个c源程序,能够实现顺序表的建立插入删除查找功能.
回复列表 (共7个回复)
沙发
中国台湾 [专家分:2140] 发布于 2006-03-21 18:29:00
#include<stdio.h>
#include<stdlib.h>
typedef int Status;
typedef int ElemType;
#define OK 1
#define ERROR -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
typedef struct {
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList *L){
(*L).elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!((*L).elem)) return OVERFLOW;
(*L).length=0;
(*L).listsize=LIST_INIT_SIZE;
return OK;
}/*InitList_Sq*/
Status ListInsert_Sq(SqList *L,int i,ElemType e){
ElemType *newbase, *p , *q;
if(i<1||i>L->length+1) return ERROR;
if(L->length>=L->listsize){
newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!(*newbase)) exit(OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
q=&(L->elem[i-1]);
for(p=&(L->elem[L->length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return OK;
}/*ListInsert_Sq*/
Status ListDelete_Sq(SqList *L,int i,ElemType e){
ElemType *p , *q;
if(i<1||(i>L->length)) return ERROR;
p=&(L->elem[i-1]);
e=*p;
q=L->elem+L->length-1;
for(++p;p<=q;++p)*(p-1)=*p;
--L->length;
return OK;
}/*ListDelete_Sq*/
//定义compare函数.
Status compare(ElemType x,ElemType y){
if(x==y)
return OK;
else
return 0;
}
Status LocateElem_Sq(SqList L,ElemType e){
ElemType i,*p;
i=1;
p=L.elem;
while(i<=L.length&&!(*compare)(*p++,e))
++i;
if(i<=L.length)
return i;
else return 0;
}/*LocateElem_Sq*/
void main(){
int e;
SqList L;
int select,i;
if(InitList_Sq(&L)==OVERFLOW) printf("ERROR");
else
{ printf("请输入线性表的元素:");
for( i=0;i<L.listsize;i++)
{scanf("%d",&L.elem[i]);L.length++;}
printf("插入前的线性表:\n");
for (i = 0; i< L.listsize; i++)
printf("%d ",L.elem[i]);
printf("\n");
do{
printf("\n0:To Add A Elem\n");
printf("1:To Delete A Elem\n");
printf("2:To Locate A Elem\n");
printf("3:Over\n");
scanf("%d",&select);
switch(select){
case 0:printf("请输入插入元素的位置及被插入的元素:");
scanf("%d%d",&i,&e);
ListInsert_Sq(&L,i,e);printf("The Add Position Is:%d\nTo Add A Elem Is%d\n",i,e);
printf("插入后的线性表:\n");
for(i=0;i<L.length;i++)
printf("%4d",L.elem[i]);break;
case 1:printf("请输入删除元素的位置:");
scanf("%d",&i);
ListDelete_Sq(&L,i,e);
printf("The Delete Position Is:%d\n",i);
printf("删除后的线性表:\n");
for(i=0;i<(L.length);i++)
printf("%4d",L.elem[i]);break;
case 2:printf("请输入要查找的元素.");
scanf("%d",&e);
printf("The Position Of The Elem Is:%d",LocateElem_Sq(L,e));break;
case 3:printf("OVER");
printf("\n");break;
default:printf("Please Input The Digital Number between 0-3");
printf("\n");
}
}while(select!=3);
}
}
这是一个女孩子写的 我觉得写的很好 发给你看看
板凳
北溟有渔 [专家分:40] 发布于 2006-03-22 17:02:00
嗯 有没注释啊?
(*L).elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
这句里(elemtype*)是什么意思?
3 楼
中国台湾 [专家分:2140] 发布于 2006-03-22 17:45:00
也可以写成 L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
分配一个内存空间 将地址返回给L
4 楼
哈米 [专家分:350] 发布于 2006-03-22 19:30:00
//看看合不合楼主心意
#include <stdio.h>
#define MAXNUM 100
#define NULL 0
struct SeqList;
typedef struct SeqList * PSeqList;
struct SeqList
{
int capacity;
int n;
int *element;
};
PSeqList createNullList(int m)
{
PSeqList palist=(PSeqList)malloc(sizeof(struct SeqList));
if (palist!=NULL)
{palist->element=(int*)malloc(sizeof(int)*m);
if (palist->element)
{
palist->capacity=m;
palist->n=0;
return(palist);
}
else free(palist);
}
return(NULL);
}
int isNullList(PSeqList palist)
{
return (palist->n==0);
}
int insertP(PSeqList palist,int p,int x)
{
int q;
if (palist->n==palist->capacity)
{
return(0);
}
if (isNullList(palist))
{
palist->element[0]=x;
palist->n=1;
return (1);
}
if (p<0||p>palist->n)
{
return (0);
}
for (q=palist->n-1;q>=p;q--)
palist->element[q+1]=palist->element[q];
palist->element[p]=x;
palist->n=palist->n+1;
return(1);
}
int deleteP(PSeqList palist,int p)
{
int q;
if (p<0 || p>palist->n-1)
return (0);
for (q=p;q<palist->n-1;q++)
palist->element[q]=palist->element[q+1];
palist->n=palist->n-1;
return (1);
}
void prList(PSeqList palist)
{
int q;
for (q=0;q<palist->n;q++)
printf("%d\n",palist->element[q]);
return;
}
void main()
{
PSeqList H=createNullList(MAXNUM);
if (H!=NULL)
{
int k=insertP(H,0,23);
k=insertP(H,1,15);
k=insertP(H,2,40);
k=insertP(H,2,60);
k=insertP(H,4,30);
printf("After insert 5 elements\n");
prList(H);
k=deleteP(H,1);
printf("After delete 2nd element\n");
prList(H);
}
else
{
printf("Out of space!\n");
}
}
5 楼
海上飞洪 [专家分:520] 发布于 2006-03-23 00:31:00
[quote]嗯 有没注释啊?
(*L).elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
这句里(elemtype*)是什么意思?[/quote]
把分配到的空间强制转化为ElemType类型
6 楼
北溟有渔 [专家分:40] 发布于 2006-03-23 10:03:00
谢谢楼上几位^_^
偶也写了一个 大家看看吧
刚开始学呢 写的不好
#define n0 100
typedef struct node
{
int r[n0+1];
int n;
}qulist;
void insert(qulist *l,int x,int i)
{
int j;
if(l->n==n0)
{
printf("full!\n");
return;
}
if (i<1||i>l->n+1)
{
printf("possion error!\n");
return;
}
for(j=l->n;j>=i;j--)
l->r[j+1]=l->r[j];
l->r[i]=x;
l->n++;
}
void delete(qulist *l,int i)
{
int j;
if (l->n==0)
{
printf("empty list nothing to deltet!\n");
return;
}
if (i<1||i>l->n)
{printf("error!\n");
return;
}
for (j=i;j<l->n;j++)
l->r[j]=l->r[j+1];
(l->n)--;
return;
}
qulist *creat()
{
int i,j;
qulist *l;
l=malloc(sizeof(qulist));
for(i=1;i<=n0;i++)
{
l->n=0;
printf("enter element %d:",i);
scanf("%d",&j);
l->r[i]=j;
l->n=i;
if (l->r[i]==0)
{
l->n=(l->n)-1;
printf("entering finished\n");
break;
}
}
return l;
}
void printlist(qulist *l)
{
int i;
printf("entered list elements are: ");
for(i=1;i<=l->n;i++)
printf("%d ",l->r[i]);
printf("listlong=%d\n",l->n);
return;
}
void locate(qulist *l,int i)
{
if(i>0&&i<=l->n)
printf("the elemetn in Position %d you want is: %d",i,l->r[i]);
else printf("error!");
return;
}
main()
{
int x,i,d,lo;
qulist *l;
l=creat();
printlist(l);
getch();
printf("please enter the element to insert: ");
scanf("%d",&x);
printf("enter the position: ");
scanf("%d",&i);
insert(l,x,i);
printlist(l);
getch();
printf("enter the position to delete: ");
scanf("%d",&d);
delete (l,d);
printlist(l);
getch();
printf("please enter the position you want to locate:");
scanf("%d",&lo);
locate(l,lo);
getch();
free(l);
}
7 楼
xueshangdezi [专家分:30] 发布于 2006-03-23 23:48:00
作者:哈米 和 北溟有渔
你两位的程序有点问题啊,能否再改进一点呢?
我来回复