主题:[讨论]顺序表有关问题?
abc2006
[专家分:0] 发布于 2006-12-25 17:10:00
想要几个例子,有高手能帮我写一下吗??
先谢谢了!!!
(1)用C语言设计一个顺序表的基本操作演示程序
(2)用C语言设计一个单链表的基本操作演示程序
最后更新于:2006-12-25 17:11:00
回复列表 (共7个回复)
沙发
雪光风剑 [专家分:27190] 发布于 2006-12-25 17:26:00
- -明明是讨论的标题,又来求代码
所有讲数据结构的书上都有相关代码的,你自己整一个主函数就可以了
板凳
77706127 [专家分:30] 发布于 2006-12-27 21:15:00
老严的书很多啊,怎么不买本来看,死啃书没什么多大用!!!不过你要先把书上的意思看懂了!!!
3 楼
happyplane [专家分:40] 发布于 2007-01-04 16:17:00
顺序表的操作,在TC里调试通过的,呵呵
#include<stdio.h>
#define ERROR 0
#define OK 1
typedef struct
{int a[10];
int length;
}Sqlist;
Sqlist Slist;
main()
{Sqlist *p;
int SqlistInsert(int e,int i,Sqlist *L);
int SqlistDel(int e,Sqlist *L);
int print(Sqlist *L);
int SqlistLoc(int e,Sqlist *L);
int i;
p=&Slist;
printf("your numbers(8):\n");
for(i=0,p->length=0;i<8;i++)
{scanf("%d",&p->a[i]);
p->length++;
}
menu();
}
menu()
{Sqlist *p;
char c;
int n=1,j;
int m,s;
while(n)
{
print(p);
printf("\t\t\tmenu\n");
printf("1 insert a number\n");
printf("2 delete a number\n");
printf("3 locate a number\n");
printf("4 exit\n");
printf("please choose:");
c=getchar();clrscr();
if(c=='1') {printf("please insert(number,place)\n");scanf("%d%d",&m,&s);SqlistInsert(m,s,p);}
if(c=='2') {printf("the deleted number(location) \n");scanf("%d",&m);SqlistDel(m,p);}
if(c=='3') {printf("you want to find ? \n");scanf("%d",&m);SqlistLoc(m,p);}
if(c=='4') n=0;
}
}
int SqlistInsert(int e,int i,Sqlist *L)
{int j,n;
n=L->length;
if(n>=10) {printf("overflow!!\n");getch();return ERROR; }
if(i<1||i>n+1) {printf("not exist\n");printf("\n\npress any key to continue.\n\n");getch(); return ERROR;}
else { for(j=L->length;j>=i-1;j--)
L->a[j]=L->a[j-1];
L->a[i-1]=e;
L->length++;
print(L);
printf("\n\npress any key to continue.\n\n");
getch(); return OK;
}
}
int SqlistDel(int i,Sqlist *L)
{int n,e,j;
n=L->length;
if(i<1||i>n) {printf("not exist\n");printf("\n\npress any key to continue.\n\n");getch(); return ERROR;}
else {e=L->a[i-1];
for(j=i;j<=n-1;j++)
L->a[j-1]=L->a[j];
Slist.length--;
print(L);
printf("\nthe deletde number is %d\npress any key to continue.\n\n",e); getch();return OK;
}
}
int SqlistLoc(int e,Sqlist *L)
{int i,flag=0;
for(i=0;i<L->length;i++)
if(L->a[i]==e)
{flag=1;
printf("%d is found.It's the %d number\n",e,i+1);
break;}
if(flag==0) printf("not found!!\n");
printf("\n\npress any key to continue.\n\n");
getch();
}
print(Sqlist *L)
{int j;
for(j=0;j<L->length;j++)
printf("%d ",L->a[j]);
printf("\n");
}
4 楼
happyplane [专家分:40] 发布于 2007-01-04 16:18:00
链表的相关操作
#define NULL 0
#define LEN sizeof(struct List)
#include <stdio.h>
struct List
{int n;
struct List *next;
};
int length=0;
main()
{char ch;
int n=1,m,s;
char menu(void);
struct List *creat(void);
struct List insert(struct List *head,int e,int i);
struct List delete(struct List *head,int e);
struct List locate (struct List *head,int e);
struct List *head;
void print(struct List *head);
head=creat();
do {ch=menu();clrscr();
switch(ch)
{case '1':print(head);printf("\n\nplease insert(number,place)\n");
scanf("%d%d",&m,&s);insert(head,m,s);break;
case '2':print(head);printf("\n\nthe deleted number \n");
scanf("%d",&m);delete(head,m);break;
case '3':print(head);printf("\n\nyou want to find ? \n");
scanf("%d",&m);locate(head,m);break;
case '4': n=0;
}
}while(n);
}
char menu(void)
{
char c;
clrscr();
printf("\t\t\tmenu\n");
printf("1 insert a number\n");
printf("2 delete a number\n");
printf("3 locate a number\n");
printf("4 exit\n");
printf("please choose:");
c=getchar();
return c;
}
struct List *creat(void)
{struct List *head,*p1,*p2;
head=(struct List*)malloc (LEN);
p1=p2=(struct List*)malloc (LEN);
printf("input your records(end by 0)\n");
scanf("%d",&p1->n);
while(p1->n!=0)
{length=length+1;
if(length==1) head->next=p1;
else p2->next=p1;
p2=p1;
p1=(struct List*)malloc (LEN);
scanf("%d",&p1->n);
}
p2->next=NULL;
return (head);
}
struct List insert(struct List *head,int e,int i)
{
struct List *s,*p;
int j=0;
p=head;
s=(struct List*)malloc(LEN);
s->n=e;
if(i>length+1) printf("error!\n");
else{
while ((p->next!=NULL) && (j<i-1))
{ p=p->next; j++;}
if(!p->next)
{p->next=s;s->next=NULL;}
else {s->next=p->next;
p->next=s;}
length++;
}
print(head);
getch();
}
struct List delete(struct List *head,int e)
{
struct List *p1,*p2;
p1=p2=head->next;
while((p1->n!=e)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if(p1->n!=e) printf("not exist!!\n");
else { if(head->next==p1)
head->next=p1->next;
else p2->next=p1->next;
length--;
}
print(head);
getch();
}
struct List locate (struct List *head,int e)
{
int count=1;
struct List *p;
p=head->next;
while((p->n!=e)&&(p->next!=NULL))
{ p=p->next;count++; }
if((p->next==NULL)&&(p->n!=e))
printf("Not found\n");
else
printf("%d is found\nIt's located in the %d place\n",e,count);
printf(" \n\n Press any key to continue\n");
getch();
}
void print(struct List *head)
{struct List *p;
printf("The list is as follows:\n");
p=head->next;
while(head!=NULL&&p->n!=0)
{printf("%d ",p->n);
p=p->next;
}
printf("\n\n\n Press any key to continue\n");
getch();
}
也通过了的
要加油哦
5 楼
happyplane [专家分:40] 发布于 2007-01-04 16:21:00
都是原创的哦,呵呵.
6 楼
xust [专家分:10] 发布于 2007-11-04 23:47:00
#include<iostream.h>
#define MAXSIZE 100
typedef struct Linear_list
{
int elem[MAXSIZE];
int last;
}SeqList;
SeqList *InitList()
{
SeqList *L;
L=new(SeqList);
L->last=-1;
return L;
}
int LocatList(SeqList *L,int x)
{
int i=0;
while(i<=L->last&&L->elem[i]!=x)
i++;
if(i>L->last)return -1;
else return i;
}
int InsertList(SeqList *L,int i,int x)
{
int j;
if(L->last==MAXSIZE-1)
{
cout<<"表已满";
return -1;
}
if(i<1||i>L->last+2)
{
cout<<"the place is wrong";
return 0;
}
for(j=L->last;j>=j-1;j--)
L->elem[j+1]=L->elem[j];
L->elem[i-1]=x;
L->last++;
return 1;
}
int DeleteList(SeqList *L,int i)
{
int j;
if(i<1||i>L->last+1)
{
cout<<"不存在第i个元素";
return 0;
}
for(j=i;j<=L->last;j++)
L->elem [j-1]=L->elem [j];
L->last --;
return 1;
}
7 楼
azhou [专家分:20] 发布于 2007-11-12 22:38:00
好
高手啊
我来回复