當前位置:首頁 » 編程語言 » c語言編寫鏈表

c語言編寫鏈表

發布時間: 2023-08-10 02:54:48

Ⅰ 如何用c語言編寫一個鏈表

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

struct Node
{
int data;//數據域
struct Node * next;//指針域
};

/*************************************************************************************
*函數名稱:Create
*函數功能:創建鏈表.
*輸入:各節點的data
*返回值:指針head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函數名稱:insert
*函數功能:在鏈表中插入元素.
*輸入:head 鏈表頭指針,p新元素插入位置,x 新元素中的數據域內容
*返回值:無
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函數名稱:del
*函數功能:刪除鏈表中的元素
*輸入:head 鏈表頭指針,p 被刪除元素位置
*返回值:被刪除元素中的數據域.如果刪除失敗返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函數名稱:print
*函數功能:列印鏈表中的元素
*輸入:head 鏈表頭指針
*返回值:無
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函數名稱:main
*函數功能:主函數創建鏈表並列印鏈表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}

Ⅱ 怎樣創建一個線性鏈表(C語言)

/*線性鏈表的構建*/
#include<stdio.h>
#include<stdlib.h>

typedefstructLnode
{
intdata;
structLnode*next;
}Lnode;

intmain()
{
Lnode*H,*p1,*p2,*p3,*p4;
H=(Lnode*)malloc(sizeof(Lnode));
p1=(Lnode*)malloc(sizeof(Lnode));
p2=(Lnode*)malloc(sizeof(Lnode));
p3=(Lnode*)malloc(sizeof(Lnode));
p4=(Lnode*)malloc(sizeof(Lnode));

p1->data=132;
p1->next=p2;
p2->data=942;
p2->next=p3;
p3->data=158;
p3->next=182;
p4->data=231;
p4->next=NULL;

printf("%d,%d ",p1->data,p3->data);
printf("%d",p1->next->data);

return0;
}

Ⅲ 用C語言編寫一個鏈表

看完你下面的追問 其實 意思是
讓你把一個已有的 單鏈表
變成反向的單鏈表 對吧

Ⅳ C語言如何創建單鏈表

C語言創建單鏈表如下:

#include"stdio.h"

#include"stdlib.h"

#include"malloc.h"

#include "iostream.h"

typedef struct node

{

intdata;

node * next;

}node , * List;

void create(int n)

{

int c;

List s,L;

L=(List)malloc(sizeof(node));

L->next=NULL;

printf("請輸入第1個數據:");

scanf("%d",&c);

L->data=c;

for(int i=2;i<=n;i++)

{

s=(List)malloc(sizeof(node));

printf("請輸入第%d個數據:",i);

scanf("%d",&c);

s->data=c;

s->next=L;

L->next =s;

}

printf("鏈表創建成功!");

}

void main()

{

int n;

printf("請你輸入鏈表的個數:");

scanf("%d",&n);

create(n);

}

Ⅳ C語言 關於鏈表的創建

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

typedefintelemtype;
typedefstructLnode{
elemtypedata;
Lnode*next;
}Lnode;

Lnode*CreatList(Lnode*Head){
Head=(Lnode*)malloc(sizeof(Lnode));
Head->next=NULL;
Lnode*p=Head;
printf("請輸入元素的個數:");
inti,n;
scanf("%d",&n);
for(i=0;i<n;++i){
p->next=(Lnode*)malloc(sizeof(Lnode));
printf("請輸入第%d個元素:",i+1);
scanf("%d",&p->next->data);
p=p->next;
}
p->next=NULL;
returnHead;
}

voidAllList(Lnode*head){
Lnode*p=head->next;
while(p){
printf("%d",p->data);
p=p->next;
}
printf(" ");
}

intmain(){
Lnode*head=NULL;
head=CreatList(head);
AllList(head);
return0;
}

Ⅵ c語言!!!程序設計:建立一個學生信息鏈表,包括學號,姓名,成績.(實現添加,刪除,查詢,排序,平均)

代碼如下:

/*用c語言鏈表編寫一個學生信息系統程序,要求輸出學生的學號,姓名,性別,學號,姓名,成績(實現添加,刪除,查詢,排序,平均)*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <stdlib.h>

using namespace std;

const int n=5;

/*

* nodeEntry : 節點數據類型

* nodeADT : 節點結構

* linkADT : 鏈表結構

*/

typedef struct Student

{

int num;

char name[30];

char sex;

float score1;//語文

float score2;//數學

float score3;//英語

//struct Student *next;

}Student;

typedef struct linkCDT {

nodeADT head;

}*linkADT;

/*

* InitLink : 初始化鏈表

* CreateNode : 創建節點

* AppendLink : 添加數據

*/

nodeADT CreateNode(Student entry) {

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

p->entry=entry,p->next=0;

return p;

}

/*

SortLink : 排序鏈表

//按學號排序

void SortLinkID(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)

if (pHead->entry.num>=p->entry.num)

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

//按英語成績排序

void SortLinkEnglish(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)

if (pHead->entry.score3>=p->entry.score3)

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按姓名的字典序進行排序

void SortLinkName(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)

if (pHead->entry.name[0]>=p->entry.name[0])

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

}

//按姓名的長度進行排序

void SortLinkNameLength(linkADT link) {

nodeADT pHead,pRear,p,tp;

if (!link) return;

for (pHead=link->head,pRear=0;pHead;pHead=pHead->next) {

for (tp=pHead,p=pHead->next;p;tp=p,p=p->next)

if (strlen(pHead->entry.name)>=strlen(p->entry.name))

tp->next=p->next,p->next=pHead,pHead=p,p=tp;

if (!pRear) link->head=pHead;

else pRear->next=pHead;

pRear=pHead;

}

循環鏈表是與單鏈表一樣

是一種鏈式的存儲結構,所不同的是,循環鏈表的最後一個結點的指針是指向該循環鏈表的第一個結點或者表頭結點,從而構成一個環形的鏈。

循環鏈表的運算與單鏈表的運算基本一致。所不同的有以下幾點:

1、在建立一個循環鏈表時,必須使其最後一個結點的指針指向表頭結點,而不是象單鏈表那樣置為NULL。此種情況還使用於在最後一個結點後插入一個新的結點。

2、在判斷是否到表尾時,是判斷該結點鏈域的值是否是表頭結點,當鏈域值等於表頭指針時,說明已到表尾。而非象單鏈表那樣判斷鏈域值是否為NULL。

以上內容參考:網路-鏈表

Ⅶ 如何用C語言創建一個鏈表,實現增、刪、改、查

#includex0dx0a#includex0dx0a#include x0dx0a//先定義一種student類型,表示一個學生的信息,如下:x0dx0atypedef struct studentx0dx0a{x0dx0aint num; //表示學號x0dx0achar name[30]; //表示姓名x0dx0afloat score; //表示分數x0dx0a}student;x0dx0a//定義一種NODE類型,表示一個結點信息,如下:x0dx0atypedef struct nodex0dx0a{x0dx0astudent st; //表示一個學生的信息x0dx0astruct node *next; //表示一個NODE類型的指針x0dx0a}NODE;x0dx0a//1、寫出建立一頌老個帶頭結點的線性鏈表的函數,侍櫻亂其中每個結點包括學號、姓名、分數三個數據域。函數形式如下:x0dx0aNODE *creat_link(int direction)x0dx0a{x0dx0aNODE *head,*p,*tail;x0dx0aint xh,i=1;x0dx0aif(direction==1) //當direction的值為1時,新建立的結點連到尾部x0dx0a{x0dx0atail=head=(NODE *)malloc(sizeof(NODE));x0dx0ahead->next=NULL;x0dx0aprintf("請輸入第%d個學生的學號:",i);x0dx0ascanf("%d",&xh);x0dx0awhile(xh>0) //從鍵盤臨時輸入學生情況,當輸入的學號非正,則鏈表建立完畢x0dx0a{x0dx0ap=(NODE *)malloc(sizeof(NODE));x0dx0ap->st.num=xh;x0dx0aprintf("請輸入第%d個學生的姓名:",i);x0dx0ascanf("%s",p->st.name);x0dx0aprintf("請輸入第%d個學生的成績:",i);x0dx0ascanf("%f",&p->st.score);x0dx0ap->next=NULL;x0dx0atail->next=p;x0dx0atail=p;x0dx0ai=i+1;x0dx0aprintf("請輸入第%d個學生的學號:",i);x0dx0ascanf("%d",&xh);x0dx0a}x0dx0a}x0dx0aelse if(direction==0) //當direction為0時,新建立的結點成為第一個結點x0dx0a{x0dx0ahead=(NODE *)malloc(sizeof(NODE));x0dx0ahead->next=NULL;x0dx0aprintf("請輸入第%d個學生的學號:",i);x0dx0ascanf("%d",&xh);x0dx0awhile(xh>0) //從鍵盤臨時輸入學生情況,老檔當輸入的學號非正,則鏈表建立完畢x0dx0a{x0dx0ap=(NODE *)malloc(sizeof(NODE));x0dx0ap->st.num=xh;x0dx0aprintf("請輸入第%d個學生的姓名:",i);x0dx0ascanf("%s",p->st.name);x0dx0aprintf("請輸入第%d個學生的成績:",i);x0dx0ascanf("%f",&p->st.score);x0dx0ap->next=head->next;x0dx0ahead->next=p;x0dx0ai=i+1;x0dx0aprintf("請輸入第%d個學生的學號:",i);x0dx0ascanf("%d",&xh);x0dx0a}x0dx0a}x0dx0areturn head;x0dx0a}x0dx0a//2、寫出輸出上述鏈表各結點數據域值的函數。該函數對應的函數需要一個形參,表示鏈表的頭指針,形式如下:x0dx0avoid print_link(NODE *head)x0dx0a{x0dx0aNODE *p;x0dx0ap=head->next;x0dx0aprintf("%-10s%-20s%-10s\n","學號","姓名","分數");x0dx0awhile(p!=NULL)x0dx0a{x0dx0aprintf("%-10d%-20s%-10.1f\n",p->st.num,p->st.name,p->st.score);x0dx0ap=p->next;x0dx0a}x0dx0a//該函數能輸出head所指的鏈表的所有結點值,輸出形式如下:x0dx0a/*本函數輸出線性表sq中所有數據,形式如下:x0dx0a學號 姓名 分數x0dx0a12 張三 234.5x0dx0a18 李四 987.7x0dx0a??? ??? ??.*/x0dx0a}x0dx0a//3、寫出在鏈表中刪除結點的函數x0dx0aint del_link(NODE *head,char name[])x0dx0a{x0dx0aNODE *p,*p1;x0dx0ap=head->next;x0dx0ap1=head;x0dx0awhile(p!=NULL)x0dx0a{x0dx0aif(strcmp(p->st.name,name)!=0)x0dx0a{x0dx0ap1=p;x0dx0ap=p->next;x0dx0a}x0dx0aelsex0dx0a{x0dx0abreak;x0dx0a}x0dx0a}x0dx0aif(p!=NULL)x0dx0a{x0dx0ap1->next=p->next;x0dx0afree(p);x0dx0areturn 1;x0dx0a}x0dx0aelsex0dx0a{x0dx0areturn 0;x0dx0a}x0dx0a//刪除head所指的鏈表中,名字為name的結點,刪除成功返回1,不成功返回0x0dx0a}x0dx0a//4、寫出在鏈表中插入結點的演算法x0dx0aint insert(NODE *head,student x,int wz)x0dx0a{x0dx0aNODE *p=head;x0dx0aint i=0,jg;x0dx0aif(wz<=0)x0dx0a{x0dx0ajg=0;x0dx0a}x0dx0aelsex0dx0a{x0dx0awhile(inext;x0dx0a}x0dx0aif(p==NULL)x0dx0a{x0dx0ajg=0;x0dx0a}x0dx0aif(i=wz-1)x0dx0a{x0dx0a//找到wz前面的節點,p指向它x0dx0aNODE *q;x0dx0aq=(NODE *)malloc(sizeof(NODE));x0dx0aq->st.num=x.num;x0dx0astrcpy(q->st.name,x.name);x0dx0aq->st.score=x.score;x0dx0aq->next=p->next;x0dx0ap->next=q;x0dx0ajg=1;x0dx0a}x0dx0a}x0dx0areturn jg;x0dx0a//該函數能夠在wz這個結點之前,插入一個新結點,新結點的數據域為x。插入成功返回1,不成功返回0。x0dx0a}x0dx0a//5、寫出主函數,分別調用上面演算法所對應的程序,建立鏈表,並輸出鏈表的值。x0dx0avoid main()x0dx0a{x0dx0aNODE *head; //定義指針變數headx0dx0aint wz; //表示插入位置x0dx0achar xm[30];x0dx0astudent st; //定義一個變數st,用來表示一個學生的信息x0dx0ahead=creat_link(1);x0dx0aprint_link(head); //調用函數建立鏈表,並把返回值送給head;x0dx0a//調用函數,輸出鏈表中各個結點的值x0dx0a//輸入一個學生的有關信息,送給變數st的有關成員x0dx0aprintf("\n\n請輸入要插入的位置:");x0dx0ascanf("%d",&wz); //輸入wz的值x0dx0aprintf("請輸入要插入的學生的學號:");x0dx0ascanf("%d",&st.num);x0dx0aprintf("請輸入要插入的學生的姓名:");x0dx0ascanf("%s",st.name);x0dx0aprintf("請輸入要插入的學生的成績:");x0dx0ascanf("%f",&st.score); x0dx0a//調用函數,在鏈表中把學生st的值作為一個結點插入,如果插入成功,輸出新鏈表x0dx0aif(insert(head,st,wz)==1)x0dx0a{x0dx0aprintf("\n插入成功,新表為:\n");x0dx0aprint_link(head);x0dx0a}x0dx0aelsex0dx0a{x0dx0aprintf("插入不成功");x0dx0a}x0dx0a//調用函數,在鏈表中刪除一個指定結點的值,如果刪除成功,輸出新鏈表x0dx0aprintf("\n\n請輸入要刪除的學生的姓名:");x0dx0agetchar();x0dx0agets(xm);x0dx0aif(del_link(head,xm)==1)x0dx0a{x0dx0aprintf("\n刪除成功,新表為:\n");x0dx0aprint_link(head);x0dx0a}x0dx0aelsex0dx0a{x0dx0aprintf("刪除不成功");x0dx0a}x0dx0a}

Ⅷ 用C語言編程實現單鏈表的基本操作

第二個顯示為什麼?為什麼什麼東西都沒有、
#include<stdio.h>
typedef
struct
sample
{
char
ch;
struct
sample
*next;
}LNode;
LNode
*Createlist(LNode
*head)
//創建單鏈表,頭插入法
{
LNode
*p;
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf("aply
error\n");
head->next=NULL;
head->ch
=
'\0';
int
i=
0;
printf("請一次輸入A-Z:\n");
for(i
=
0
;
i
<
26;
++i)
{
p=(LNode
*)malloc(sizeof(LNode));
if(!p)
printf("aply
error\n");
scanf("%c",&p->ch);
p->next
=
head->next;
head->next=p;
}
return
head;
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head->next,*r
=
head->next;
while(p->next)
{
p
=
p->next;
}
int
i
=
0
;
for(i
=
0
;
i
<
3;
i++)
{
p->next
=
r;
p
=
p->next;
q
=
q->next;
r
=
q;
}
p->next
=NULL;
head->next
=
q;
return
head;
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head->next;
while(p->next!=NULL)
{
printf("%c\t",p->ch);
p=p->next;
}
printf("%c\n",p->ch);
}
int
main(void)
{
LNode
*head;
head
=
Createlist(head);//鏈表初始化
ListPrint(head);
//列印單鏈表數據
head
=
EncryptList(head);
ListPrint(head);
return
0;
}
看看。

熱點內容
開源庫編譯管理員 發布:2025-02-06 09:39:14 瀏覽:914
臉書怎麼注冊安卓 發布:2025-02-06 09:36:47 瀏覽:381
車用安卓導航無線打不開什麼原因 發布:2025-02-06 09:27:50 瀏覽:790
安卓與蘋果如何互相傳送文件 發布:2025-02-06 09:27:40 瀏覽:25
華為伺服器盤符如何分配 發布:2025-02-06 09:26:41 瀏覽:560
傳奇h5源碼下載 發布:2025-02-06 09:26:06 瀏覽:78
編譯uclibc 發布:2025-02-06 09:09:04 瀏覽:152
用gcc編譯16位匯編 發布:2025-02-06 09:06:07 瀏覽:823
什麼低端安卓手機不卡 發布:2025-02-06 09:03:32 瀏覽:13
我的世界伺服器卡領地 發布:2025-02-06 08:50:45 瀏覽:256