C语言线性表的实现

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#include<stdio.h>
#include<stdlib.h>

/*
线性链表的实现

ADT 线性表(List)
Data

Operation
    CreatList(*L); 初始化操作,建立一个空的线性链表L。
    ListEmpty(L); 若线性链表L为空,返回true, 否则返回false。
    ClearList(*L); 将线性表L清空
    GetElem(L, i, e); 将线性表L中的第i个元素值返回给e。
    LocatElem(L,e); 在线性表L中查找与给定值e相等的元素,如果查找成功, 返回该元素在表中位置表示成功;否则返回0表示失败。
    InsertList(*L, i, e); 在线性L表的第i个位置插入e。
    DelList(*L, i, *e); 删除线性表L中的第i个元素,并用e返回其值。
    ListLength(L); 返回线性表L的长度。
    Print(L); 打印线性表L的全部元素。
*/
typedef struct Node //链表数据结构
{
    int data;
    struct Node *next;
}Node, *LinkList;


int CreatList(LinkList *L, int n)
{
    LinkList p, r;
    int i;
    *L = (LinkList)malloc(sizeof(Node) );
    r = *L;
    for(i=0; i<n; i++)
    {
        p = (Node *)malloc(sizeof(Node));
        p->data = i;
        r->next = p;
        r = p;
    }
    r->next = NULL;
}

int ListEmpty(LinkList L)
{
    LinkList p;
    int count;
    p = L->next;
    while(p)
        count ++;
    if(count>0) return TRUE;
    else return FALSE;
}

int ClearList(LinkList *L)
{
    LinkList p, q;
    p = (*L)->next;
    while(p)
    {
        q = p->next;
        free(p);
        p = q;
    }
    (*L)->next = NULL;
    return OK;
}

int InsertNode(LinkList *L, int i, int e)
{
    LinkList p;
    Node *s;
    p = *L;
    int j = 1;
    while(p && j<i)
    {
        p = p->next;
        ++j;
    }
    if(!p || j>i)
        return ERROR;
    s = (Node *)malloc(sizeof(Node));
    s->data = e;
    s->next = p->next;
    p->next = s;
    return OK;
}

int GetElem(LinkList L, int i)
{
    LinkList p;
    p = L->next;
    int j = 1;
    int m;
    while(p && j<i)
    {
        p = p->next;
        ++j;
    }
    if(!p || j>i)
        return ERROR;
    m = p->data;
    printf("%d", m);
    return OK;
}

int DelNode(LinkList *L, int i, int *e)
{
    LinkList p, r;
    p = *L;
    int j = 1;
    while(p->next && j<i)
    {
        p = p->next;
        ++j;
    }
    if(!(p->next) || j>i)
        return ERROR;

    r = p->next;
    p->next = r->next;
    *e = r->data;
    free(r);
    return OK;
}

int Print(LinkList L)
{
    LinkList p;
    p = L->next;
    int m, i;
    while(p)
    {
        m = p->data;
        printf("%d ", m);
        p = p->next;
    }
}

int LocateElem(LinkList L, int e, int *count)
{
    LinkList p;
    p = L;
    while(p)
    {
        p = p->next;
        (*count) ++;
        if(p->data == e)
        return TRUE;
    }

    return FALSE;
}

int ListLength(LinkList L, int *count)
{
    LinkList p;
    p = L;
    while(p)
    {
        p = p->next;
        (*count) ++;
    }
    return OK;

}

int main()
{
    int e;
    int location=0;
    int length=0;
    LinkList L;
    CreatList(&L, 10);
    Print(L);
    printf("\n");
    InsertNode(&L, 3, 100);
    Print(L);
    printf("\n");
    GetElem(L, 3);
    printf("\n");
    DelNode(&L, 9, &e);
    Print(L);
    printf("\n");
    printf("%d\n", e);
    LocateElem(L, 100, &location);
    printf("%d\n", location);
    ListLength(L, &length);
    printf("%d\n", length);
}
  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用结构体来表示货品信息,例如: ``` typedef struct { int id; // 货品编号 char name[20]; // 货品名称 int quantity; // 货品数量 float price; // 货品单价 } Product; ``` 然后使用数组来存储货品信息,例如: ``` Product products[100]; // 最多存储100个货品 int numProducts = 0; // 当前存储的货品数量 ``` 可以实现以下功能: 1. 添加货品 ``` void addProduct(int id, char *name, int quantity, float price) { if (numProducts >= 100) { printf("货品数量已达上限\n"); return; } products[numProducts].id = id; strcpy(products[numProducts].name, name); products[numProducts].quantity = quantity; products[numProducts].price = price; numProducts++; } ``` 2. 删除货品 ``` void deleteProduct(int id) { int i; for (i = 0; i < numProducts; i++) { if (products[i].id == id) { break; } } if (i == numProducts) { printf("找不到编号为%d的货品\n", id); return; } for (; i < numProducts - 1; i++) { products[i] = products[i + 1]; } numProducts--; } ``` 3. 修改货品 ``` void modifyProduct(int id, char *name, int quantity, float price) { int i; for (i = 0; i < numProducts; i++) { if (products[i].id == id) { break; } } if (i == numProducts) { printf("找不到编号为%d的货品\n", id); return; } strcpy(products[i].name, name); products[i].quantity = quantity; products[i].price = price; } ``` 4. 查询货品 ``` void queryProduct(int id) { int i; for (i = 0; i < numProducts; i++) { if (products[i].id == id) { printf("编号:%d,名称:%s,数量:%d,单价:%.2f\n", products[i].id, products[i].name, products[i].quantity, products[i].price); return; } } printf("找不到编号为%d的货品\n", id); } ``` 5. 显示所有货品 ``` void displayProducts() { int i; for (i = 0; i < numProducts; i++) { printf("编号:%d,名称:%s,数量:%d,单价:%.2f\n", products[i].id, products[i].name, products[i].quantity, products[i].price); } } ``` 以上就是用C语言线性表实现文具店的货品管理的基本思路。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值