#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 100
typedef struct node
{
char id[20];
char name[20];
int age;
}*NODE;
NODE n=NULL;
NODE student[size];
int len=0;
int AddNode(NODE);
int Printindex(int);
void ShowNode(void);
int Addindex(NODE,int);
int Deleteindex(int);
void Free(void);
int AddNode(NODE add)
{
if (len<size)
{
student[len] = add;
len +=1;
return(0);
}
else{
printf("out index\n");
return(1);
}
}
int Addindex(NODE add, int index)
{
if (index>len)
{
printf("please input correct index low len = %d\n",len);
return(1);
}
else
{
int curindex=len;
while(curindex!=index){
student[curindex+1]=student[curindex];
curindex-=1;
}
student[index]=add;
len+=1;
return(0);
}
}
int Deleteindex(int index)
{
NODE p ;
int curindex=index;
if (index>=len)
{
printf("out index...\n");
return(1);
}
else{
p =student[index];
free(p);
p=NULL;
while(curindex !=len){
student[curindex]=student[curindex+1];
curindex+=1;
}
len-=1;
return(0);
}
}
int Printindex(int index)
{
NODE p;
if (index>=len)
{
printf("out index...\n");
return(1);
}
else{
p = student[index];
printf("%-10s: %-10s: %3d\n", p->id, p->name, p->age);
return(0);
}
}
void ShowNode(void)
{
NODE p;
int index=0;
if (len >0)
{
while(index<len){
p = student[index];
printf("%-10s: %-10s: %3d\n", p->id, p->name, p->age);
index+=1;
}
}
else{
printf("Empty!");
}
}
void Free(void)
{
NODE p;
while(len!=0){
p = student[len-1];
free(p);
p=NULL;
len-=1;
}
}
int main(int argc, char const *argv[])
{
FILE *file;
char buffer[128];
char *temp;
char op;
int index;
if (argc != 2)
{
fprintf(stderr, "error\n");
exit(EXIT_FAILURE);
}
file = fopen(argv[1], "rt");
if (file == NULL)
{
fprintf(stderr, "Cannot open/find %s\n", "Lab1test.DAT");
getchar();
exit(EXIT_FAILURE);
}
while (!feof(file))
{
if (fgets(buffer, 127, file) == NULL)
break;
buffer[strlen(buffer) - 1] = '\0';
n = (NODE)malloc(sizeof(struct node));
temp = strtok(buffer, ",");
strcpy(n->id, temp);
temp = strtok(NULL, ",");
strcpy(n->name, temp);
temp = strtok(NULL, ",");
n->age = atoi(temp);
if (AddNode(n) != 0)
{
fprintf(stderr, "Error adding node. Aborting\n");
getchar();
exit(EXIT_FAILURE);
}
}
ShowNode();
while(1)
{
printf("you can input: P D I S or Q \n");
scanf("%s",&buffer);
/**/
buffer[strlen(buffer)] = '\0';
op = buffer[0];
if (op == 'P')
{
index =atoi( &buffer[1]);
if(Printindex(index)!=0)
printf("please input again...\n");
}
else if (op== 'D')
{
index = atoi(&buffer[1]);
if (Deleteindex(index)!=0)
printf("please input again...\n");
}
else if (op == 'I')
{
n = (NODE)malloc(sizeof(NODE));
strtok(buffer, ",");
index = atoi(&buffer[1]);
temp = strtok(NULL,",");
strcpy(n->id, temp);
temp = strtok(NULL, ",");
strcpy(n->name, temp);
temp = strtok(NULL, ",");
n->age = atoi(temp);
if(Addindex(n,index)!=0)
printf("please input again...\n");
}
else if (op=='Q')
break;
else if (op=='S')
ShowNode();
else
printf("please input the correct operation\n");
}
ShowNode();
getchar();
printf("starting to free NODE... \n");
Free();
printf("show the NODE: ");
ShowNode();
getchar();
return 0;
}
顺序表实例
最新推荐文章于 2022-10-10 09:13:42 发布