Ⅰ 用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;
}