蓝魔
小水手

UID 25080
精华
0
积分 5
帖子 5
阅读权限 10
注册 2006-11-9 来自 xinjiang
状态 离线
|
这是我的程序,你大概可以看一下了
链式存储
# include "stdio.h"
# define MAXSIZE 100
struct node
{
int data;
struct node *next;
};
struct node l;
void create(struct node *head)
{
struct tail;
struct node * p;
int x;
head=tail=(struct node*)malloc(sizeof(struct node));
head->next=Null;
printf("please input the data:");
scanf("%4d",&x);
while (x!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=x;
p->next=Null;
tail->next=p;
printf("please input the data:");
scanf("%4d",&x);
}
}
void creatlist(struct node *head)
{
int x;
struct node *p;
head=(struct node *)malloc(sizeof(struct node));
head->next=Null;
printf("please input the data:");
scanf("%4d",&x);
while(x!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=x;
p->next=head->next;
head->next=p;
printf("please input the data:"):
scanf("%4d",&x);
}
}
int listinsert(int i, int x,struct *head)
{
int count=0;
struct node t;
struct node *p;
p=head;
while(count<i-1 && p->next!=Null)
{
p=p->next;
count++;
}
if(count==i-1)
{
t=(struct node *)malloc(sizeof(struct node));
t->data=x;
t->next=p->next;
p->next=t;
return 1;
}
else return 0;
}
int deletelist(struct node *head ,int i)
{
int count=0;
struct node *p,*q;
p=head;
while(count<i-1 && p->next!=Null)
{
p=p->next;
count++;
}
if(count = i-1)
{
q=p->next;
p->next=p->next->next;
free(q);
return 1;
}
else return 0;
}
main()
{
int x;
int a;
int i,j,k;
printf("if you want to inseart the data please press 1\n if you want to delete the data please press 2\n")
]# include "stdio.h"
# define MAXSIZE 100
struct node
{
int data;
struct node *next;
};
struct node l;
void create(struct node *head)
{
struct tail;
struct node * p;
int x;
head=tail=(struct node*)malloc(sizeof(struct node));
head->next=Null;
printf("please input the data:");
scanf("%4d",&x);
while (x!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=x;
p->next=Null;
tail->next=p;
printf("please input the data:");
scanf("%4d",&x);
}
}
void creatlist(struct node *head)
{
int x;
struct node *p;
head=(struct node *)malloc(sizeof(struct node));
head->next=Null;
printf("please input the data:");
scanf("%4d",&x);
while(x!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=x;
p->next=head->next;
head->next=p;
printf("please input the data:"):
scanf("%4d",&x);
}
}
int listinsert(int i, int x,struct *head)
{
int count=0;
struct node t;
struct node *p;
p=head;
while(count<i-1 && p->next!=Null)
{
p=p->next;
count++;
}
if(count==i-1)
{
t=(struct node *)malloc(sizeof(struct node));
t->data=x;
t->next=p->next;
p->next=t;
return 1;
}
else return 0;
}
int deletelist(struct node *head ,int i)
{
int count=0;
struct node *p,*q;
p=head;
while(count<i-1 && p->next!=Null)
{
p=p->next;
count++;
}
if(count = i-1)
{
q=p->next;
p->next=p->next->next;
free(q);
return 1;
}
else return 0;
}
main()
{
int x;
int a;
int i,j,k;
printf("if you want to inseart the data please press 1\n if you want to delete the data please press 2\n")
}
线性存储
# include "stdio.h"
# define MAXSIZE 100
struct list
{
int elem[MAXSIZE];
int nuninlist;
};
struct list l;
void init()
{
l.numinlist=0;
}
void append(int x)
{
l.elem[l.numinlist]=x;
l.numinlist++;
}
void Creatlist()
{
int y;
printf("please input the first data");
scanf("%4d",&y);
while (y!=0 && l.numinlist<MAXSIZE)
{
append(y);
printf("\n please input the second data:\n ");
scanf("4d",&y);
}
}
void insert(int i, int x)
{
int j;
if(i<0 ||i>l.numinlist || l.numinlist>=MAXSIZE)
return;
for(j=l.numinlist-1;j>i;j--)
l.elem[j+1]=l.elem[j];
l.elem=x;
l.numinlist++;
}
void delete(int i)
{
int j;
if(l.numinlist==0 ||i<0 || i>l.numinlist)
return;
for(j=i+1;j<l.numinlist-1;j++)
l.elem[j-1]=l.elem[j];
l.numinlist--;
}
int inserch(int x)
{
int i;
for(i=0;i<l.numinlist&&l.elem!=x;i++)
if(i<l.numinlist)
return 0;
else return -1;
}
man()
{
int i,a,j,x;
init();
Creatlist();
printf("\n if you want to insearch the data ,please press 1;\n if you want to delete the data please press 2;\n if you want to search the data please press 3;\n");
scanf("%4d",&a);
switch(a)
{
case 1:
printf("\n please input the position and the data you want to insert:");
scanf("%4d,%4d",&i,&x);
insert(i,x);
break;
case 2:
printf("\n please input the position you want to delete");
scanf("%4d",&j);
delete(i);
break;
case 3:
printf("\n please input the data you want to search:");
scanf("%4d",x);
inserch(x);
break;
default:printf ("\n error!\n");
}
}
|  志当存高远 |
|