#2
word1232016-10-25 22:34
#include <stdio.h>
#include <stdlib.h> #define size_list 100 #define TRUE 0 #define FALSE -1 typedef struct list { int *plist; int listsize; int listlength; }list; list initlist(int n) { list l; int i; l.plist = (int *)malloc(size_list*sizeof(int)); l.listlength = 0; l.listsize = size_list; //初始储存容量 printf("please input %d value:",n); for(i=1;i<=n;i++) { scanf("%d",&(l.plist[i-1])); l.listlength++; //---------------------------------计数 // printf("%d\n",(l.plist[i-1])); } return l; } void destroylist(list l) { // list l; free(l.plist); // } list clearlist() { list l; l.listlength = 0; return l; } int listempty(list l) { if(l.listlength ==0) return TRUE; else return FALSE; } int listfull(list l) { // list l; if(l.listlength ==l.listsize) return TRUE; else return FALSE; } list insertlist(int i,int e) { list l; int j; if(i<=0||i>l.listlength) exit(0); else { for(j=l.listlength;j>=i;j--) l.plist[j] = l.plist[j-1]; e = l.plist[i-1]; } l.listlength++; return l; } list deletelist(int i,int e) { list l; int j; if(i<=0||i>l.listlength) exit(0); else { e = l.plist[i-1]; for(j=i;j<l.listlength;j++) l.plist[j-1] = l.plist[j]; l.listlength--; } return l; } void traverselist(list l) { int i; for(i=1;i<=l.listlength;i++) //------------------------------------个数 { printf("%d",(l.plist[i-1])); } // return l; // printf("%d",visit(l.plist[i-1])); } int main() { list l; int a; l = initlist(3); traverselist(l); scanf("%d",&a); return 0; } //其它函数可能还有点问题,自己看一下 |
//顺序表的实现
#include <stdio.h>
#include <stdlib.h>
#define size_list 100
#define TRUE 0
#define FALSE -1
typedef struct list
{
int *plist;
int listsize;
int listlength;
}list;
list initlist(int n)
{
list l;
int i;
l.plist = (int *)malloc(size_list*sizeof(int));
l.listlength = 0;
l.listsize = size_list; //初始储存容量
printf("please input %d value:",n);
for(i=1;i<=n;i++)
{
scanf("%d",&(l.plist[i-1]));
// printf("%d\n",(l.plist[i-1]));
}
return l;
}
void destroylist(list l)
{
// list l;
free(l.plist); //
}
list clearlist()
{
list l;
l.listlength = 0;
return l;
}
int listempty(list l)
{
if(l.listlength ==0)
return TRUE;
else
return FALSE;
}
int listfull(list l)
{
// list l;
if(l.listlength ==l.listsize)
return TRUE;
else
return FALSE;
}
list insertlist(int i,int e)
{
list l;
int j;
if(i<=0||i>l.listlength)
exit(0);
else
{
for(j=l.listlength;j>=i;j--)
l.plist[j] = l.plist[j-1];
e = l.plist[i-1];
}
l.listlength++;
return l;
}
list deletelist(int i,int e)
{
list l;
int j;
if(i<=0||i>l.listlength)
exit(0);
else
{
e = l.plist[i-1];
for(j=i;j<l.listlength;j++)
l.plist[j-1] = l.plist[j];
l.listlength--;
}
return l;
}
void traverselist(list l)
{
int i;
for(i=1;i<l.listlength;i++)
{
printf("%d",(l.plist[i-1]));
}
return l;
// printf("%d",visit(l.plist[i-1]));
}
int main()
{
list l;
int a;
l = initlist(3);
traverselist(l);
scanf("%d",&a);
return 0;
}