當前位置:首頁 » 編程語言 » c語言單鏈表程序代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言單鏈表程序代碼

發布時間: 2022-07-23 11:40:37

❶ 用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>
#include<string.h>
struct people
{
char name[10];
int age;
struct people * next;
};
int main()
{
struct people * head=NULL;
struct people * prev , * current;
int flag=1;
while(flag!=0)
{
printf("請輸入學生姓名,年齡:(年齡輸入0結束所有輸入工作) ");
current=(struct people *)malloc(sizeof(struct people));
if(head==NULL)
head=current;
else
prev->next=current;
current->next=NULL;
scanf("%s",&current->name);
scanf("%d",&current->age);
prev=current;
flag=current->age;
}
printf("Output: ");
if(head==NULL)
printf("無資料。 ");
else
{
current=head;
while(current->next!=NULL)
{
printf("姓名:%s 年齡:%d ",current->name,current->age);
current=current->next;
}
}
}


至於排序,斷開舊鏈表,將前後指針鏈接到新的節點就好

如果還有問題歡迎再問哈

❸ 求一個C語言建立鏈表的代碼

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 10
typedef struct node *link;
struct node {
int item;
link next;
};
link NODE(int item, link next)
{
link t = malloc(sizeof *t);
t->item = item;
t->next = next;
return t;
}
void insert_node(link head, int item)
{
link t;
for (t = head; t->next; t = t->next)
if (item <= t->next->item)
break;
t->next = NODE(item, t->next);
}
void delete_node(link head, link del_node)
{
link t;
for (t = head; t->next; t=t->next)
if (del_node == t->next){
t->next = t->next->next;
free(del_node);
return;
}
}
void destroy(link head)
{
link q, p;
for (p=head; p; p = q) {
q = p->next;
free(p);
}
}
void show_list(link head)
{
link t;
for (t = head->next; t; t = t->next)
printf("%3d", t->item);
printf("\n");
}
link search_node(link head, int item)
{
link t;
for (t = head; t->next; t = t->next)
if (item == t->next->item)
return t->next;
return NULL;
}
int main(void)
{
int i;
link p = NULL;
/*init head*/
link head = NODE(0, NULL);
srand(time(NULL));
for (i = 0; i < N; i++)
insert_node(head, rand()%100);
insert_node(head, 10);
show_list(head);
printf("delete the node of item is 10\n");
p = search_node(head, 10);
delete_node(head, p);
show_list(head);
destroy(head);
return 0;
}

給你了 求最佳答案

❹ 數據結構C語言單鏈表的創建,插入刪除和合並程序代碼

你看這個應該滿足要求吧。我把三種循環方式都用上了:
#include<stdio.h>
#include<math.h>

int isprime(int n)
{
int i,t;
if(n==2)
return 1;
if(n%2==0 || n<2)
return 0;
for(i=3,t=(int)sqrt(n);i<=t;i+=2)
{
if(n%i==0)
return 0;
}
return 1;
}

void main()
{
int i,a,n;

i=0;
do
{
printf("Input an integer (>=1):");
scanf("%d",&a);
if(a>=1)
break;
}while(++i<3);

if(i==3) exit(0);

printf("prime submultiples:\n");

i=1;
n=0;
while(i<=a)
{
if(a%i==0)
if(isprime(i))
{
printf("%d ",i);
n++;
if(n%10==0)
printf("\n");
}
i++;
}

❺ c語言單鏈表代碼

#include<stdio.h>

#include<stdlib.h>

typedefstructnode

{

intdata;

structnode*next;

}node,*link;

linkcreate(linkhead)

{

inttemp;

linkp,q;

q=head=p=(node*)malloc(sizeof(node));

while(scanf("%d",&temp),temp)

{

p->data=temp;

if(p!=head)

{

q->next=p;

q=p;

}

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

}

q->next=NULL;

returnhead;

}

voidshow(linkhead)

{

linkp=head;

while(p)

{

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

p=p->next;

}

}

voidmain()

{

linkhead;

head=create(head);

show(head);

}

❻ C語言 單鏈表插入的代碼是

在給定的單鏈表的第i位上插入值為n的節點。
#include <stdio.h>
#include<malloc.h>
#define N 5
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
}linklist;

linklist *Creatlist(linklist*L){
L=(linklist*)malloc(sizeof(linklist));
L->next=NULL;
return L;
}
int Judge(linklist *L){
if(L->next==NULL)
{
printf("建表成功...\n");
}
else
printf("建表失敗.\n");
return 0;
}
int Input(linklist *L,int x,linklist *r){
int i;
linklist *p;
p=(linklist*)malloc(sizeof(linklist));
p->data=x;
p->next=NULL;
r->next=p;
printf("%d ",p->data);
return 0;
}
int Insert1(linklist *L,int i){
linklist *p,*q,*r,*t;
int j=1,item;
p=L->next;
q=L;
r=L;
if(L->next==NULL)
{
printf("表空.\n");
return 0;
}
else
{
while(p!=NULL&&j<i)
{
q=p;
p=p->next;
j++;
}
if(p==NULL)
{
printf("%d不在表的范圍內.\n");
return 0;
}
else
{
t=(linklist*)malloc(sizeof(linklist));
t->next=NULL;
printf("請輸入item:");
scanf("%d",&item);
t->data=item;
t->next=p;
q->next=t;
}
printf("在第%d位上插入值為%d的節點後的所有數據為\n",i,item);
for(j=0;j<N+1;j++)
{
r=r->next;
printf("%d ",r->data);
}
printf("\n");
return 1;
}
}
int main()
{
int i,item,k;
linklist *L,*r;
printf("單向鏈表的創建(包括初始化)與輸出\n");
L=Creatlist(L);
Judge(L);
printf("表中的數據為:");
r=L;
Input(L,11,r);
r=r->next;
Input(L,32,r);
r=r->next;
Input(L,17,r);
r=r->next;
Input(L,46,r);
r=r->next;
Input(L,9,r);
r=r->next;
printf("\n");
printf("在給定的單鏈表的第i位上插入值為item的節點\n");
printf("請輸入i:");
scanf("%d",&i);
Insert1(L,i);
return 0;
}

在給定單鏈表的值為m的節點的前面插入一個值為n的節點
#include <stdio.h>
#include<malloc.h>
#define N 5
typedef int elemtype;
typedef struct node
{
elemtype data;
struct node *next;
}linklist;

linklist *Creatlist(linklist*L){
L=(linklist*)malloc(sizeof(linklist));
L->next=NULL;
return L;
}
int Judge(linklist *L){
if(L->next==NULL)
{
printf("建表成功...\n");
}
else
printf("建表失敗.\n");
return 0;
}
int Input(linklist *L,int x,linklist *r){
int i;
linklist *p;
p=(linklist*)malloc(sizeof(linklist));
p->data=x;
p->next=NULL;
r->next=p;
printf("%d ",p->data);
return 0;
}
int Insert2(linklist *L,int item){
linklist *p,*q,*r,*t;
int j=1,m;
p=L->next;
r=L;
q=L;
if(L->next==NULL)
{
printf("表空.\n");
return 0;
}
else
{
while(p!=NULL)
{
if(p->data!=item)
{
q=p;
p=p->next;
}
else
break;
}
if(p==NULL)
{
printf("%d不在表的范圍內.\n");
return 0;
}
else
{
t=(linklist *)malloc(sizeof(linklist));
t->next=NULL;
printf("請輸入m:");
scanf("%d",&m);
t->data=m;
q->next=t;
t->next=p;
}
}
printf("在值為%d的節點的前面插入一個值為%d的節點後的所有數據為\n",item,m);
for(j=0;j<N+1;j++)
{
r=r->next;
printf("%d ",r->data);
}
printf("\n");
return 1;
}
int main()
{
int i,item,k;
linklist *L,*r;
printf("單向鏈表的創建(包括初始化)與輸出\n");
L=Creatlist(L);
Judge(L);
printf("表中的數據為:");
r=L;
Input(L,11,r);
r=r->next;
Input(L,32,r);
r=r->next;
Input(L,17,r);
r=r->next;
Input(L,46,r);
r=r->next;
Input(L,9,r);
r=r->next;
printf("\n");
printf("在給定單鏈表的值為item的節點的前面插入一個值為m的節點\n");
printf("請輸入item:");
scanf("%d",&item);
Insert2(L,item);
return 0;
}

❼ c語言 單鏈表源代碼

#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
struct LNode
{
ElemType data;
struct LNode *next;
};

//***********************************************************置空表setnull()
void setnull(struct LNode **p)
{
*p=NULL;
}

//************************************************************求長度length()
int length(struct LNode **p)
{
int n=0;
struct LNode *q=*p;
while (q!=NULL)
{
n++;
q=q->next;
}
return(n);
}

//*************************************************************取結點get()
ElemType get(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p;
while (j<i && q!=NULL) /**//*查找第i個結點*/
{
q=q->next;j++;
}
if (q!=NULL) /**//*找到了第i個結點*/
return(q->data);
else
{
printf("位置參數不正確!\n");
return NULL;
}
}

//************************************************************按值查找locate()
int locate(struct LNode **p,ElemType x)
{
int n=0;
struct LNode *q=*p;
while (q!=NULL && q->data!=x) /**//*查找data域為x的第一個結點*/
{
q=q->next;
n++;
}
if (q==NULL) /**//*未找到data域等於x的結點*/
return(-1);
else /**//*找到data域等於x的結點*/
return(n+1);
}

//**********************************************************插入結點insert()
void insert(struct LNode **p,ElemType x,int i)
{
int j=1;
struct LNode *s,*q;
s=(struct LNode *)malloc(sizeof(struct LNode)); /**//*建立要插入的結點s*/
s->data=x;
q=*p;
if (i==1) /**//*插入的結點作為頭結點*/
{
s->next=q;
*p=s;
}
else
{
while (j<i-1 && q->next!=NULL) /**//*查找第i-1個結點*/
{
q=q->next;j++;
}
if (j==i-1) /**//*找到了第i-1個結點,由q指向它*/
{
s->next=q->next; /**//*將結點s插入到q結點之後*/
q->next=s;
}
else
printf("位置參數不正確!\n");
}
}

//*********************************************************刪除結點del()
void del(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p,*t;
if (i==1) /**//*刪除鏈表的頭結點*/
{
t=q;
*p=q->next;
}
else
{
while (j<i-1 && q->next!=NULL) /**//*查找第i-1個結點*/
{
q=q->next;j++;
}

if (q->next!=NULL && j==i-1) /**//*找到第i-1個結點,由q指向它*/
{
t=q->next; /**//*t指向要刪除的結點*/
q->next=t->next; /**//*將q之後的結點刪除*/
}
else printf("位置參數不正確!\n");
}
if (t!=NULL) /**//*在t不為空時釋放該結點*/
free(t);
}

//********************************************************顯示鏈表display()
void display(struct LNode **p)
{
struct LNode *q;
q=*p;
printf("單鏈表顯示:");
if (q==NULL) /**//*鏈表為空時*/
printf("鏈表為空!");
else if (q->next==NULL) /**//*鏈表只有一個結點時*/
printf("%c\n",q->data);
else { /**//*鏈表存在一個以上的結點時*/
while (q->next!=NULL) /**//*顯示前面的結點*/
{
printf("%c→",q->data);q=q->next;
}

printf("%c",q->data); /**//*顯示最後一個結點*/
}

printf("\n");
}

void main()
{
struct LNode *head;
setnull(&head);
insert(&head,'a',1);
insert(&head,'b',2);
insert(&head,'a',2);
insert(&head,'c',4);
insert(&head,'d',3);
insert(&head,'e',1);
display(&head);
printf("單鏈表長度=%d\n",length(&head));
printf("位置:%d 值:%c\n",3,get(&head,3));
printf("值:%c 位置:%d\n",'a',locate(&head,'a'));
printf("刪除第1個結點:");
del(&head,1);
display(&head);
printf("刪除第5個結點:");
del(&head,5);
display(&head);
printf("刪除開頭3個結點:");
del(&head,3);
del(&head,2);
del(&head,1);
display(&head);
}

/**//*
運行結果:
單鏈表顯示:e→a→a→d→b→c
單鏈表長度=6
位置:3 值:a
值:a 位置:2
刪除第1個結點:單鏈表顯示:a→a→d→b→c
刪除第5個結點:單鏈表顯示:a→a→d→b
刪除開頭3個結點:單鏈表顯示:b
*/

❽ 求C語言單鏈表創建及訪問程序代碼

下面是我寫的代碼:

頭文件(SLNode.h)

#include<stdlib.h>

typedef int DataType;

typedef struct node
{
DataType data;
struct node *next;
}SLNode;

void SLNode_Initiate(SLNode **head)
{
if((*head=(SLNode *)malloc(sizeof(SLNode)))==NULL)
exit(1);
(*head)->next=NULL;
}

int SLNode_Length(SLNode *head)
{
int i=0;
SLNode *p;
p=head->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}

int SLNode_Insert(SLNode *head,int pos,DataType add)
{
SLNode *p,*s;
int i;
if(pos>SLNode_Length(head)||pos<0)
{
printf("the pos in not exsit");
return 0;
}
p=head;
for(i=0;i<pos;i++)
p=p->next;
if((s=(SLNode *)malloc(sizeof(SLNode)))==NULL)
return 0;
s->data=add;
s->next=p->next;
p->next=s;
return 1;
}

int SLNode_Delete_Pos(SLNode *head,int del)
{
SLNode *p,*s;
int i;
if(del>=SLNode_Length(head) || del<0)
{
printf("the pos not esxit\n");
return 0;
}
p=head;
for(i=0;i<del;i++)
p=p->next;
s=p->next;
p->next=s->next;
free(s);
return 1;
}

int SLNode_Delete_Data(SLNode *head,DataType del)
{
SLNode *p,*s;
int length=SLNode_Length(head);
p=head;
while(p->next!=NULL)
{
s=p->next;
if(s->data==del)
{
p->next=s->next;
free(s);
}
p=p->next;
}
if(length==SLNode_Length(head))
{
printf("can't find the number\n");
return 0;
}
return 1;
}

int SLNode_Get(SLNode *head,int pos,DataType *get)
{
SLNode *p=head;
int i;
if(pos>=SLNode_Length(head) || pos<0)
{
printf("thd pos is not exsit\n");
return 0;
}
for(i=0;i<=pos;i++)
p=p->next;
*get=p->data;
return 1;
}

void SLNode_Destroy(SLNode **head)
{
SLNode *p,*s;
p=*head;
while(p!=NULL)
{
s=p;
p=p->next;
free(s);
}
*head=NULL;
}

void SLNode_Print(SLNode *head)
{
SLNode *p;
p=head->next;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}

CPP文件:

#include<stdio.h>
#include"SLNode.h"

void Menu()
{
puts("*************************************");
puts("A: Add node\n");
puts("B: Delete node by pos\n");
puts("C: Delete node by data\n");
puts("D: Get node by pos\n");
puts("E: Print node\n");
puts("X: Exit the programe");
puts("*************************************");
}

int main()
{
char select;
int insert;
int get;
int del;
int pos;
SLNode *node;
SLNode_Initiate(&node);
Menu();
while(scanf("%c",&select))
{
fflush(stdin);
switch(select)
{
case 'A':
printf("please enter a number which is insert and it's pos:");
scanf("%d%d",&insert,&pos);
SLNode_Insert(node,pos,insert);
break;
case 'B':
printf("please enter the pos of the number which you want to delete:");
scanf("%d",&pos);
SLNode_Delete_Pos(node,pos);
break;
case 'C':
printf("please enter the number which you want to delete:");
scanf("%d",&del);
SLNode_Delete_Data(node,del);
break;
case 'D':
printf("please enter the pos of the number which you want to get:");
scanf("%d",&pos);
SLNode_Get(node,pos,&get);
break;
case 'E':
SLNode_Print(node);
break;
case 'X':
SLNode_Destroy(&node);
return 0;
default:
printf("please select a correct select\n");
}
printf("\n\n");
fflush(stdin);
system("pause");
system("cls");
Menu();
}
return 0;
}

❾ 用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);

}