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

c語言鏈表建立

發布時間: 2024-05-06 16:17:33

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語言實現建立一個單鏈表的過程,並實現列印鏈表中每一個元素,寫出完整程序

這是個很簡單的鏈表創建和輸出

#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);

}

㈢ C語言,創建一個鏈表並賦值1、2、3、4、5,麻煩把全部程序寫下

// DLink.cpp : 定義控制台應用程序的入口點。

//

#include "stdafx.h"

#include "malloc.h"

typedef struct LNode

{

int data;

struct LNode *next;

}Dlink;

int _tmain(int argc, _TCHAR* argv[])

{

Dlink *l,*s1;

Dlink *s=(Dlink *)malloc(sizeof(Dlink));

l=s;

for(int i=0;i<5;i++)

{

s->data=i+1;

s1=(Dlink *)malloc(sizeof(Dlink));

s->next=s1;

s=s1;

}

s->next=NULL;

//數據就保存到以l為頭結點的鏈表中了

return 0;

}

(3)c語言鏈表建立擴展閱讀:

對於非線性的鏈表,可以參見相關的其他數據結構,例如樹、圖。另外有一種基於多個線性鏈表的數據結構:跳錶,插入、刪除和查找等基本操作的速度可以達到O(nlogn),和平衡二叉樹一樣。

其中存儲數據元素信息的域稱作數據域(設域名為data),存儲直接後繼存儲位置的域稱為指針域(設域名為next)。指針域中存儲的信息又稱做指針或鏈。

由分別表示,,…,的N 個結點依次相鏈構成的鏈表,稱為線性表的鏈式存儲表示,由於此類鏈表的每個結點中只包含一個指針域,故又稱單鏈表或線性鏈表。

㈣ C語言鏈表的使用方法

D
答案D設置完,p就從鏈表中丟掉了。
p就是一個指向結構體node的指針。
p->next就是p包含的執行下一個node的指針,在本題,就是q。

熱點內容
php配置mail 發布:2024-05-19 11:52:37 瀏覽:906
歐洲國家的雲伺服器 發布:2024-05-19 11:43:30 瀏覽:44
左游手柄助手2腳本 發布:2024-05-19 11:40:28 瀏覽:1002
挖礦需要什麼配置 發布:2024-05-19 11:38:02 瀏覽:895
eclipse導出ant腳本 發布:2024-05-19 11:20:28 瀏覽:99
如何改變vivo手機賬戶密碼 發布:2024-05-19 10:56:07 瀏覽:377
sql的length函數 發布:2024-05-19 10:55:15 瀏覽:546
資料庫管理系統設計報告 發布:2024-05-19 10:49:50 瀏覽:685
linux怎麼將驅動編譯進內核 發布:2024-05-19 10:23:47 瀏覽:768
c語言讀程序題 發布:2024-05-19 10:13:52 瀏覽:675