当前位置:首页 » 网购平台 » 如何用c语言链表做购物车
扩展阅读
宁波奥德赛优惠价格 2021-03-15 14:26:02
丹尼斯购物卡能挂失么 2021-03-15 14:25:58
淘宝购物指纹验证失败 2021-03-15 14:24:44

如何用c语言链表做购物车

发布时间: 2021-02-18 12:04:00

Ⅰ 用c语言写使用单链表建立一个简易商品库存表的程序

按照你题意,代码如下:(你提问题没有用编程模板,我这里代码只能直接贴,原格式都没了,你只能自己排版了 。)

(初始化数据的那段输入,只为演示,不要可以删除!)。

#include <stdio.h>

#include <malloc.h>

#include <string.h>

#include <conio.h>

#include <windows.h>

typedef struct stock

{

int id;

char name[10];

int stNum;

struct stock *next;

}STK;

void addByName(STK **stkHead,STK **stkTail,STK *stkNew,char *name);//向指定名称商品后添加节点,不存在添加在表头

void meError(void *p);//内存申请失败

int add2Tail(STK **stkHead,STK **stkTail,STK *stkNew);//向链表尾部添加新节点, 成功返回1,失败返回0

int add2Head(STK **stkHead,STK **stkTail,STK *stkNew);//向链表头部添加新节点, 成功返回1,失败返回0

STK *newSTK();//创建新节点,新建节点

STK *initSTK();//初始化链表

void printSTK(STK *stkHead);

void tjAll(STK *stkHead);//统计总库存数量

int main()

{

int i;

char name[10];

STK *stkHead=initSTK(),*stkTail=NULL;

//----------为了测试,我在下面这段初始了3个节点,不需要可以删除

i=3;

printf("请输入3个节点作为初始测试数据(不需要可以删除该段代码): ");

while(i--)

if(!add2Tail(&stkHead,&stkTail,newSTK()))

printf("添加节点失败,请重新输入! "),i++;

while(1)

{

system("cls");

printf("1、显示当前所有商品信息 ");

printf("2、统计商品总库存 ");

printf("3、向指定商品名后添加商品(名称不存在,将插入表头) ");

scanf("%d",&i);

switch(i)

{

case 1:system("cls");printSTK(stkHead);break;

case 2:system("cls");tjAll(stkHead);break;

case 3:system("cls");printf("请输入要插入位置商品的名称:"),scanf("%s",name);addByName(&stkHead,&stkTail,newSTK(),name);break;

}

printf("................按任意键继续! ");

getch();

}

return 0;

}

STK *initSTK()//初始化链表

{

STK *stkHead=(STK *)malloc(sizeof(STK));

meError(stkHead);

stkHead->next=NULL;

return stkHead;

}

void addByName(STK **stkHead,STK **stkTail,STK *stkNew,char *name)//向指定名称商品后添加节点,不存在添加在表头

{

int flag=0;

STK *stkh=*stkHead;

while(stkh->next)

{

if(strcmp(stkh->next->name,name)==0)

{

stkNew->next=stkh->next->next;

stkh->next->next=stkNew;

flag=1;

break;

}

stkh=stkh->next;

}

if(flag)

printf("新的商品已成功添加到商品%s后面! ",name);

else

{

add2Head(stkHead,stkTail,stkNew);

printf("%s不存在!新的商品已添加到表头! ",name);

}

}

void printSTK(STK *stkHead)

{

printf("当前库存情况: ");

while(stkHead->next)

{

printf("商品编号:%04d,商品名称:%9s,商品库存:%d ",stkHead->next->id,stkHead->next->name,stkHead->next->stNum);

stkHead=stkHead->next;

}

}

void tjAll(STK *stkHead)//统计总库存数量

{

int cnt=0,sum=0;

while(stkHead->next)

{

cnt++;

sum+=stkHead->next->stNum;

stkHead=stkHead->next;

}

printf("当前库存共%d种商品,总计库存数量%d ",cnt,sum);

}

STK *newSTK()//创建新节点,新建节点

{

static int stockID=1;//商品ID唯一,每次使用,自增

STK *stkNew=(STK *)malloc(sizeof(STK));

meError(stkNew);

stkNew->next=NULL;

stkNew->id=stockID++;

printf("请输入商品名称:");

scanf("%s",stkNew->name);

printf("请输入商品库存:");

scanf("%d",&stkNew->stNum);

return stkNew;

}

int add2Tail(STK **stkHead,STK **stkTail,STK *stkNew)//向链表尾部添加新节点, 成功返回1,失败返回0

{

if(!(*stkHead))

return 0;

if(!((*stkHead)->next))

add2Head(stkHead,stkTail,stkNew);

else

(*stkTail)->next=stkNew;

*stkTail=stkNew;

return 1;

}

int add2Head(STK **stkHead,STK **stkTail,STK *stkNew)//向链表头部添加新节点, 成功返回1,失败返回0

{

if(!(*stkHead))

return 0;

if(!((*stkHead)->next))

(*stkTail)=stkNew;

stkNew->next=(*stkHead)->next;

(*stkHead)->next=stkNew;

return 1;

}

void meError(void *p)//内存申请失败

{

if(p==NULL)

{

printf(" 异常:内存申请失败!回车结束程序! ");

while(getch()!=' ');

exit(0);

}

}

Ⅱ C语言中怎样用链表实现增删改查

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

typedef struct node
{
int nDate;
struct node *pstnext;
}Node;

//链表输出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}

//链表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配内存失败\r\n");
return NULL;
}
head->pstnext = NULL;

p = head;
while(cycle)
{
printf("请输入数据且当输入数据为0时结束输入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配内存失败\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}

//链表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
printf("链表中没有找到该值");
}else if(date == p->nDate)
{
printf("要查找的值%d在链表中第%d个位置\r\n", date, n);
}
return;
}

//按序号查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf("查找位置不合法\r\n");
}else if(i == 0)
{
printf("查找位置为头结点\r\n");
}else if(i == Num)
{
printf("第%d个位置数据为%d\r\n", i, p->nDate);
}
}

//在指定元素之前插入新结点
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf("插入位置不存在\r\n");
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}

}

//在指定元素之后插入新结点
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配内存失败\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
printf("插入位置不存在\r\n");
}
}

//删除指定结点
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
printf("删除位置不存在\r\n");
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}

void main()
{
int date, num; //待查找数据
int i3; //指定删除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新数据
Node *Head = NULL; //定义头结点
Node *Head_New = NULL;

//链表建立
Head = creat();
printf("输出建立的单链表\r\n");
output(Head);

//链表按值查找
printf("请输入待查找的数据\r\n");
scanf("%d", &date);
research_Date(Head, date);

//链表按序号查找
printf("请输入待查找序号\r\n");
scanf("%d", &num);
research_Number(Head, num);

//在指定第i1个元素之前插入新元素Newdate
printf("在指定第i个元素之前插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入后新链表\r\n");
output(Head);

//在指定第i2个元素之后插入新元素Newdate
printf("在指定第i个元素之后插入新元素Newdate");
printf("请输入i与元素且以逗号间隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入后新链表\r\n");
output(Head);

//指定删除i3元素
printf("删除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("删除后新链表\r\n");
output(Head);

return;
}

Ⅲ 用C语言实现建立一个单链表的过程,并实现打印链表中每一个元素,写出完整程序

这是个很简单的链表创建和输出

#include<stdio.h>

#include<stdlib.h>

typedef struct linkednode

{

char data;

struct linkednode *next;

}node,*link_list;//链表节点的结构及重命名

link_list creat()//创建一个链表返回类型是链表的首地址

{

link_list L;

node *p1,*p2;

char data;

L=(node*)malloc(sizeof(node));//开辟存储空间

p2=L;

while((data=getchar())!=' ')//输入回车键时结束输入

{

p1=(node*)malloc(sizeof(node));

p1->data=data;

p2->next=p1;

p2=p1;

}

p2->next=NULL;

return L;

}

void print(link_list L)//把链表输出

{

node *p;

p=L->next;

while(p!=NULL)

{

printf("%c",p->data);

p=p->next;

}

printf(" ");

}

void main()

{

link_list L=NULL;

char x;

printf("请输入链表节点: ");

L=creat();

print(L);

}

Ⅳ 如何用链表实现malloc函数功能,用c语言

//定义链表结抄点,包括学袭号,姓名,和指向下一结点的指针
struct node {
int num;
int name;
struct node *next;
}*linklist
// 当需要一个结点的时候,就为新结点分配内存空间
linklist p;
p=(linklist)malloc(sizeof(struct node));
//结点成员赋值
num=2004;
name=Tom;
next=NULL;

//然后用链表的指针指向该结点p就可以了
//比如有一个指针r指向链表的尾部,可以用以下方法将新结点加如链表中
r->next=p;

Ⅳ 用c语言写一个简单的链表,具体要怎么用代码实现

||struct
stu
{
char
a[10];
char
e[4];
char
c[14];
char
d[30];
struct
stu*b;
};
struct
stu
*creat(struct
stu*head,char
c)
//功能1:创建链表
{
while(c-'y'==0||c-'y'==0)
{
printf("请输入姓名,性回别,电话号码,e-mail地址答\n");
head=insert(head);
printf("是否继续输入y/n\n");getchar();
scanf("%c",&c);system("cls");
}
if(c-'n'==0||c-'n'==0)
return
head;
else
{
printf("是否继续输入y/n\n");
getchar();
scanf("%c",&c);
system("cls");
return
(creat(head,c));
}
}

Ⅵ 如何用纯C语言编写一个通用链表

以前写的一个,可参考一下。
#include <stdio.h>
#include<malloc.h>
//#define NULL 0
#define LEN sizeof(struct student)
/***********************建立链表********************/
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
/*******************输出链表 ************************/
void print(struct student *head)
{
struct student *p;
printf("\nNow, These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld%5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
/******************对链表的删除***********************/
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null! \n");
return head;
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("delete:%ld\n",num);
n=n-1;
}
else printf("%ld not enn found!\n",num);
return(head);
}
/*******************对链表的插入************************/
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return(head);
}
void main()
{
struct student *head,stu;
long del_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
head=del(head,del_num);
print(head);
printf("\ninput the inserted record:");
scanf("%ld,%f",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
}

Ⅶ C语言 用链表形式编写

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#define L sizeof(struct cargo)
struct cargo
{
long num;
char name[10];
float price;
int quantity;
struct cargo *next;
};

int n;
struct cargo *km()//输入
{
struct cargo *head;
struct cargo *p1,*p2;
n=0;
p1=p2=(struct cargo *)malloc(L);
system("color 1");
printf("请输入货物的编码,名字,价格,数量\n");
system("color 2");
scanf("%ld %s %f %d",&p1->num,&p1->name,&p1->price,&p1->quantity);
head=NULL;
while (p1->num!=0&&p1->next!=NULL)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct cargo*)malloc(L);
scanf("%ld %s %f %d",&p1->num,&p1->name,&p1->price,&p1->quantity);
}
p2->next=NULL;
return head;
}

void print(struct cargo *head)//查全部信息
{
struct cargo *p;
printf("尊敬的客户您所查的全部信息共有%d件。\n",n);
system("color 7");
p=head;
if(head!=NULL)
{
printf("具体信息如下:\n");
printf(" ------------------------------------------------------------------------------\n");
printf(" | 商品编号 | 商品名称 | 商品价格 | 商品数量 |\n");
printf(" | | | | |\n");
while (p!=NULL)
{
printf(" ---------------------------------------------------------------------\n");
printf(" | %ld | %s | %.2f | %d |\n",p->num,p->name,p->price,p->quantity);
p=p->next;
}
printf(" ---------------------------------------------------------------------\n");
}
else printf("这是一个空链表!\n");
}

void revise(struct cargo *head,long num)//修改
{
struct cargo *p;
p=head;
while (p!=NULL)
{
if(p->num==num)
{
printf("请重新输入您需要修改的商品的编号,以示确定:\n");
system("color 5");
scanf("%ld",&p->num);
//sleep(100);
printf("请您把此商品的其他信息依次输入:\n");
system("color 7");
scanf("%s %f %d",&p->name,&p->price,&p->quantity);
break;
}
p=p->next;
}
print(head);
}

delete(struct cargo *head,long num)//删除
{
struct cargo *p1,*p2;
if(head==NULL)
{
printf("It's NULL!\n");
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)
{
head=p1->next;
free(p1);
}
else
{
p2->next=p1->next;
p1=NULL;
free(p1);
}
printf("删除成功!\n");
n--;
}
else printf("没有删除的信息!\n");
print(head);
return head;
}
struct cargo*Insert(struct cargo*head,long num,struct cargo *nod)
{
struct cargo *p1;
if(head==NULL)
{
head=nod;//把内存给头
nod->next=NULL;//只有一个信息则下一个为空
n+=1;
return head;
}
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
p1=p1->next;
}
if(num==p1->num)
{
nod->next=p1->next;
p1->next=nod;
n+=1;
}
else
printf("没有所要插入的节点!\n");
print(head);
return head;
}

struct cargo*find1(struct cargo *head,long num)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空链表!\n");
return head;
}

while(p->num!=num&&p->next!=NULL)
{
p=p->next;
}
if(num==p->num)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %.2f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}

struct cargo*find2(struct cargo *head,char name[])
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空链表!\n");
return head;
}

while(p->name!=name&&p->next!=NULL)
{
p=p->next;
}
if(name==p->name)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %.2f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}

struct cargo*find3(struct cargo *head,float price)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空链表!\n");
return head;
}

while(p->price!=price&&p->next!=NULL)
{
p=p->next;
}
if(price==p->price)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}

struct cargo*find4(struct cargo *head,int quantity)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空链表!\n");
return head;
}

while(p->quantity!=quantity&&p->next!=NULL)
{
p=p->next;
}
if(quantity==p->quantity)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}

{
struct cargo *head=NULL;
struct cargo *pre;
int a,myquantity;
char ch;
char name[20];
float myprice;
long thnum;
printf(" @_@欢迎进入仓库管理系统@_@ \n");
printf("***************************************************************************\n");
printf("* *\n");
printf("* 欢 库*\n");
printf("* *\n");
printf("* *\n");
printf("* 迎 管*\n");
printf("* *\n");;
printf("* *\n");
printf("* 进 输入0代表输入信息, 如果输入四个0代表结束 理*\n");
printf("* *\n");
printf("* *\n");
printf("* 入 系*\n");
printf("* *\n");
printf("* *\n");
printf("* 仓 统*\n");
printf("* *\n");
printf("***************************************************************************\n");
system("color 5");
while(1)
{
printf("请输入您需要的功能:\n");
printf("输入1代表修改货物信息\n");
printf("输入2代表查看全部信息\n");
printf("输入3代表删除信息\n");
printf("输入4代表插入一个新的商品\n");
printf("输入5查找商品信息\n");
system("color 5");
printf("输入6保存且退出系统!\n");
system("color 6");
printf("请输入信息:\n");
printf("请做出选择[ ]\b\b\b ");
scanf("%d",&a);
if(a==0)
{
head=km();
print(head);
}
if(a==2)
{
print(head);
}
if(a==1)
{
printf("请输入您要修改的商品编号:\n");
scanf("%ld",&thnum);
revise(head,thnum);
}
if(a==3)
{
printf("请输入您要删除的商品编号:\n");
scanf("%ld",&thnum);
delete(head,thnum);
}
if(a==4)
{
printf("请输入想要插入的位置:\n");
scanf("%ld",&thnum);
pre=(struct cargo*)malloc(L);
printf("输入插入的信息:\n");
scanf("%ld %s %f %d", &pre->num,&pre->name,&pre->price,&pre->quantity);
Insert(head,thnum,pre);
}
if(a==5)
{
printf("请输入查找方式:\n");
printf("@@@@@@输入a代表用商品编号查找@@@@@@\n");
printf("@@@@@@输入b代表用商品名字查找@@@@@@\n");
printf("@@@@@@输入c代表用商品价格查找@@@@@@\n");
printf("@@@@@@输入d代表用商品数量查找@@@@@@\n");
printf("请输入信息:\n");
printf("请做出选择[ ]\b\b\b ");
scanf("%c",&ch);
if(ch=='a')
{
printf("@@@@@@输入商品编号进行查找@@@@@@\n");
scanf("%ld",&thnum);
find1(head,thnum);
}
else if(ch=='b')
{
printf("@@@@@@输入商品名字进行查找@@@@@@\n");
scanf("%s",&name);
find2(head,name);
}
else if(ch=='c')
{
printf("@@@@@@输入商品价格查找@@@@@@\n");
scanf("%f",&myprice);
find3(head,myprice);
}
else if(ch=='d')
{
printf("@@@@@@输入商品数量查找@@@@@@\n");
scanf("%d",&myquantity);
find4(head,myquantity);
}
else
{
printf("选择错误!!\n");
}
}
if(a==6)
{
printf("您已经退出仓库管理系统,谢谢您的使用!\n");
break;
}
}
system("pause");
return 0;
}

Ⅷ C语言怎么实现嵌套的链表操作

struct UserInfo{
//...
struct RecordInfo * records;
}user;

records指向user的第一个record,比如要统计某个user花了多少版钱:
struct RecordInfo * curr=user->records;
int total_price=0;
while(curr!权=NULL){
total_price+=curr->price;
curr=curr->next;
}