顺序表实例

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值