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

c語言鏈表實驗報告

發布時間: 2022-08-06 11:04:35

1. c語言程序設計實驗報告

代碼:

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

char stu_no[10][10];
int c_math[10],c_en[10],c_computer[10],point[10],average[10];
int i,j,max;
char c;

void input()
{
for(i=0;i<=9;i++) /*輸入學生成績*/
{
printf("請輸入學號:");
scanf("%s",&stu_no[i]);
printf("\n請輸入數學成績:");
scanf("%d",&c_math[i]);
printf("\n請輸入英語成績:");
scanf("%d",&c_en[i]);
printf("\n請輸入計算機基礎成績:");
scanf("%d",&c_computer[i]);
}
for(i=0;i<=10;i++) /*計算總分跟平均分*/
{
point[i]=c_math[i]+c_en[i]+c_computer[i];
average[i]=point[i]/3;
}

}

void paixu()
{

printf("成績按從高到低排列為:\n");
printf("\n學號 數學 英語 計算機基礎 總分 平均分\n");
for (i=0;i<=10;i++)
{ for(j=1;j<=10;j++)
if (point[i]>point[j])
max=i;
printf("%s,d,%d,%d,%d,%d,%d\n",stu_no[max],c_math[max],c_en[max],c_computer[max],point[max],average[max]);

}
}

void main()
{

input();

paixu();

}

2. 用C語言編寫一個鏈表

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

3. 如何用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;
}

4. 求助:數據結構實驗報告(c語言版)

/* 標准文檔模板 */

#include "Stdio.h"
#include "Conio.h"
#define SIZE 50
struct student
{
int ID;
char name[20];
int score[3];//三門課的成績
float avg;
};
int count=4; //用來記錄總條數 因為已有四條記錄所以初始化為4,再錄入記錄時從第四條開始記錄
void input(struct student stu[]) /*實現信息錄入功能*/
{
int i;
int sum; //總分
char ch;
do
{
sum=0;
printf("請輸入學生ID:\n");
scanf("%d",&stu[count].ID);
printf("請輸入學生name:\n");
scanf("%s",&stu[count].name);
for(i=0;i<3;i++)
{
printf("請輸入第%d門成績",i+1);
scanf("%d",&stu[count].score[i]);
sum=sum+stu[count].score[i];//計算總分
}
stu[count].avg=sum/3.0f; //計算平均分
count++; //累計總記錄條數
printf("是否繼續錄入信息?(y/n)");
fflush(stdin);
scanf("%c",&ch);
}while(ch=='Y'||ch=='y');
}
void view(struct student stu[]) /*實現信息瀏覽功能*/
{
int i;
printf("\nID \tNAME \t\tSTB\tC語言\tSQL\tAVG\n");
for(i=0;i<count;i++)
{
printf("%d\t%-15.7s %d\t%d\t%d\t%-.2f\n",stu[i].ID,stu[i].name,stu[i].score[0],
stu[i].score[1],stu[i].score[2],stu[i].avg);
}
}

void sort_ID(struct student stu[]) //按學號進行排序
{
int i,j;
struct student temp;//定義結構體變數來實現整條記錄交換
for(i=0;i<count-1;i++) //選擇排序法進行排序
{
for(j=i+1;j<count;j++)
{
if(stu[i].ID<stu[j].ID)
{
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
}
view(stu);
}
void sort_Name(struct student stu[]) //按姓名進行排序
{
int i,j;
struct student temp;
for(i=0;i<count-1;i++)//選擇排序法進行排序
{
for(j=i+1;j<count;j++)
{
if(strcmp(stu[i].name,stu[j].name)>0) //用strcmp比較名字的大小
{
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
}
view(stu);
}
void sort_Avg(struct student stu[]) //按平均分大小進行排序
{
int i,j;
struct student temp;//定義結構體變數來實現整條記錄交換
for(i=0;i<count-1;i++) //選擇排序法進行排序
{
for(j=i+1;j<count;j++)
{
if(stu[i].avg<stu[j].avg)
{
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
}
view(stu);
}
void sort(struct student stu[])
{
int option;
do
{
printf("\n========1.按學號排序==========2.按姓名排序=======\n");
printf("\n========3.按平均分排序========0.退出=============\n");
printf("請選擇:\n");
scanf("%d",&option);
switch(option)
{
case 0:printf("謝謝!");break;
case 1:sort_ID(stu);break;
case 2:sort_Name(stu);break;
case 3:sort_Avg(stu);break;
default:printf("請重新選擇!"); break;
}
}while(option!=0);
}
/* 按學號查詢*/
void findID(struct student stu[])
{
int tempID;//要查找的臨時學號
char flag='n'; //默認沒有找到狀態
int i;
printf("請輸入要查詢的學號:");
scanf("%d",&tempID);
printf("\n\t 學號 \t姓名\tSTB \tC成績\tSQL成績 平均分\n");
for(i=0;i<count;i++)
{
if(tempID==stu[i].ID) //找到
{
printf("\t%d\t%s\t%d\t%d\t%df\t%-.2f\n",stu[i].ID,stu[i].name,
stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].avg);
flag='y'; //找到的狀態

}

}
if(flag!='y')
printf("找不到學號是%d的記錄\n",tempID);

}
/* 按姓名查詢*/
void findName(struct student stu[])
{
char tempName[15]; //要查找的臨時姓名
int flag=0; //默認沒有找到狀態
int i;
printf("請輸入要查詢的姓名:");
fflush(stdin);
scanf("%s",tempName);
printf("\n\t 學號 \t姓名\tSTB \tC成績\tSQL成績 平均分\n");
for(i=0;i<count;i++)
{
if(strcmp(tempName,stu[i].name)==0) //找到
{
printf("\t%d\t%s\t%d\t%df\t%df\t%-.2f\n",stu[i].ID,stu[i].name,
stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].avg);
flag=1; //找到的狀態

}

}
if(flag!=1)
printf("找不到姓名是%s的記錄\n",tempName);

}
/* 按學號,姓名,平均分查詢 */
void find(struct student stu[])
{
int option;
printf("\n********************************************************\n");
printf("\n****** 1. 退出 2.按學號 3.按姓名 ****************\n");
printf("\n********************************************************\n");
printf("請選擇: ");
scanf("%d",&option);
switch(option)
{
case 1: break;
case 2: findID(stu);break;
case 3: findName(stu);break;
default: printf("選項無效!! \n");break;
}
}
int main(void)
{
/* 此處添加你自己的代碼 */
struct student stu[SIZE]={{1002,"LiLan",70,80,90,80},{1004,"WangHai",75,85,95,85},
{1003,"LiYang",60,80,73,81},{1001,"JackChen",95,90,100,95}}; //初始化四條信息
int option;
do
{
printf("\n======0.退出===========1.信息錄入==========2.信息瀏覽=====\n");
printf("\n======3.信息排序========4.信息查詢========================\n");
printf("請選擇:\n");
scanf("%d",&option);
switch(option)
{
case 0:printf("歡迎下次再使用!");break;
case 1:input(stu);break;
case 2:view(stu);break;
case 3:sort(stu);break;
case 4:find(stu);break;
default:printf("請重新選擇!");
}
}while(option!=0);
getch();
return 0;
}

5. 用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);

}

6. 數據結構單鏈表實驗(c語言版)

#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 20 /* 存儲空間初始分配量 */

typedef int Status;/* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */
typedef int ElemType;/* ElemType類型根據實際情況而定,這里假設為int */

Status visit(ElemType c)
{
printf("%d ",c);
return OK;
}

typedef struct Node
{
ElemType data;
struct Node *next;
}Node;
typedef struct Node *LinkList; /* 定義LinkList */

/* 初始化順序線性表 */
Status InitList(LinkList *L)
{
*L=(LinkList)malloc(sizeof(Node)); /* 產生頭結點,並使L指向此頭結點 */
if(!(*L)) /* 存儲分配失敗 */
return ERROR;
(*L)->next=NULL; /* 指針域為空 */

return OK;
}

/* 初始條件:順序線性表L已存在。操作結果:若L為空表,則返回TRUE,否則返回FALSE */
Status ListEmpty(LinkList L)
{
if(L->next)
return FALSE;
else
return TRUE;
}

/* 初始條件:順序線性表L已存在。操作結果:將L重置為空表 */
Status ClearList(LinkList *L)
{
LinkList p,q;
p=(*L)->next; /* p指向第一個結點 */
while(p) /* 沒到表尾 */
{
q=p->next;
free(p);
p=q;
}
(*L)->next=NULL; /* 頭結點指針域為空 */
return OK;
}

/* 初始條件:順序線性表L已存在。操作結果:返回L中數據元素個數 */
int ListLength(LinkList L)
{
int i=0;
LinkList p=L->next; /* p指向第一個結點 */
while(p)
{
i++;
p=p->next;
}
return i;
}

/* 初始條件:順序線性表L已存在,1≤i≤ListLength(L) */
/* 操作結果:用e返回L中第i個數據元素的值 */
Status GetElem(LinkList L,int i,ElemType *e)
{
int j;
LinkList p; /* 聲明一結點p */
p = L->next; /* 讓p指向鏈表L的第一個結點 */
j = 1; /* j為計數器 */
while (p && j<i) /* p不為空或者計數器j還沒有等於i時,循環繼續 */
{
p = p->next; /* 讓p指向下一個結點 */
++j;
}
if ( !p || j>i )
return ERROR; /* 第i個元素不存在 */
*e = p->data; /* 取第i個元素的數據 */
return OK;
}

/* 初始條件:順序線性表L已存在 */
/* 操作結果:返回L中第1個與e滿足關系的數據元素的位序。 */
/* 若這樣的數據元素不存在,則返回值為0 */
int LocateElem(LinkList L,ElemType e)
{
int i=0;
LinkList p=L->next;
while(p)
{
i++;
if(p->data==e) /* 找到這樣的數據元素 */
return i;
p=p->next;
}

return 0;
}

/* 初始條件:順序線性表L已存在,1≤i≤ListLength(L), */
/* 操作結果:在L中第i個位置之前插入新的數據元素e,L的長度加1 */
Status ListInsert(LinkList *L,int i,ElemType e)
{
int j;
LinkList p,s;
p = *L;
j = 1;
while (p && j < i) /* 尋找第i個結點 */
{
p = p->next;
++j;
}
if (!p || j > i)
return ERROR; /* 第i個元素不存在 */
s = (LinkList)malloc(sizeof(Node)); /* 生成新結點(C語言標准函數) */
s->data = e;
s->next = p->next; /* 將p的後繼結點賦值給s的後繼 */
p->next = s; /* 將s賦值給p的後繼 */
return OK;
}

/* 初始條件:順序線性表L已存在,1≤i≤ListLength(L) */
/* 操作結果:刪除L的第i個數據元素,並用e返回其值,L的長度減1 */
Status ListDelete(LinkList *L,int i,ElemType *e)
{
int j;
LinkList p,q;
p = *L;
j = 1;
while (p->next && j < i) /* 遍歷尋找第i個元素 */
{
p = p->next;
++j;
}
if (!(p->next) || j > i)
return ERROR; /* 第i個元素不存在 */
q = p->next;
p->next = q->next; /* 將q的後繼賦值給p的後繼 */
*e = q->data; /* 將q結點中的數據給e */
free(q); /* 讓系統回收此結點,釋放內存 */
return OK;
}

/* 初始條件:順序線性表L已存在 */
/* 操作結果:依次對L的每個數據元素輸出 */
Status ListTraverse(LinkList L)
{
LinkList p=L->next;
while(p)
{
visit(p->data);
p=p->next;
}
printf("\n");
return OK;
}

/* 隨機產生n個元素的值,建立帶表頭結點的單鏈線性表L(頭插法) */
void CreateListHead(LinkList *L, int n)
{
LinkList p;
int i;
srand(time(0)); /* 初始化隨機數種子 */
*L = (LinkList)malloc(sizeof(Node));
(*L)->next = NULL; /* 先建立一個帶頭結點的單鏈表 */
for (i=0; i<n; i++)
{
p = (LinkList)malloc(sizeof(Node)); /* 生成新結點 */
p->data = rand()%100+1; /* 隨機生成100以內的數字 */
p->next = (*L)->next;
(*L)->next = p; /* 插入到表頭 */
}
}

/* 隨機產生n個元素的值,建立帶表頭結點的單鏈線性表L(尾插法) */
void CreateListTail(LinkList *L, int n)
{
LinkList p,r;
int i;
srand(time(0)); /* 初始化隨機數種子 */
*L = (LinkList)malloc(sizeof(Node)); /* L為整個線性表 */
r=*L; /* r為指向尾部的結點 */
for (i=0; i<n; i++)
{
p = (Node *)malloc(sizeof(Node)); /* 生成新結點 */
p->data = rand()%100+1; /* 隨機生成100以內的數字 */
r->next=p; /* 將表尾終端結點的指針指向新結點 */
r = p; /* 將當前的新結點定義為表尾終端結點 */
}
r->next = NULL; /* 表示當前鏈表結束 */
}

int main()
{
LinkList L;
ElemType e;
Status i;
int j,k;
i=InitList(&L);
printf("初始化L後:ListLength(L)=%d\n",ListLength(L));
for(j=1;j<=5;j++)
i=ListInsert(&L,1,j);
printf("在L的表頭依次插入1~5後:L.data=");
ListTraverse(L);

printf("ListLength(L)=%d \n",ListLength(L));
i=ListEmpty(L);
printf("L是否空:i=%d(1:是 0:否)\n",i);

i=ClearList(&L);
printf("清空L後:ListLength(L)=%d\n",ListLength(L));
i=ListEmpty(L);
printf("L是否空:i=%d(1:是 0:否)\n",i);

for(j=1;j<=10;j++)
ListInsert(&L,j,j);
printf("在L的表尾依次插入1~10後:L.data=");
ListTraverse(L);

printf("ListLength(L)=%d \n",ListLength(L));

ListInsert(&L,1,0);
printf("在L的表頭插入0後:L.data=");
ListTraverse(L);
printf("ListLength(L)=%d \n",ListLength(L));

GetElem(L,5,&e);
printf("第5個元素的值為:%d\n",e);
for(j=3;j<=4;j++)
{
k=LocateElem(L,j);
if(k)
printf("第%d個元素的值為%d\n",k,j);
else
printf("沒有值為%d的元素\n",j);
}

k=ListLength(L); /* k為表長 */
for(j=k+1;j>=k;j--)
{
i=ListDelete(&L,j,&e); /* 刪除第j個數據 */
if(i==ERROR)
printf("刪除第%d個數據失敗\n",j);
else
printf("刪除第%d個的元素值為:%d\n",j,e);
}
printf("依次輸出L的元素:");
ListTraverse(L);

j=5;
ListDelete(&L,j,&e); /* 刪除第5個數據 */
printf("刪除第%d個的元素值為:%d\n",j,e);

printf("依次輸出L的元素:");
ListTraverse(L);

i=ClearList(&L);
printf("\n清空L後:ListLength(L)=%d\n",ListLength(L));
CreateListHead(&L,20);
printf("整體創建L的元素(頭插法):");
ListTraverse(L);

i=ClearList(&L);
printf("\n刪除L後:ListLength(L)=%d\n",ListLength(L));
CreateListTail(&L,20);
printf("整體創建L的元素(尾插法):");
ListTraverse(L);

return 0;
}

7. 怎麼做數據結構-單鏈表實踐報告!!!!!!

問題一:逆位序輸入n個元素的值, 逆位 就是位置相逆的意思,比如現在的list節點為 1 2 3 4 5,讓你輸入的時候按 5 4 3 2 1的順序,這就叫逆位輸入。這個函數裡面並沒有體現逆位,只是想告訴使用這個方法的人,最終得到的LIST是與輸入相反的。
問題二和問題三:他們是一起的,這兩句話的意思是將新輸入的節點插入到頭結點的後面,每次都插入到頭結點後面,與問題一相呼應,這樣逆位輸入就可以得到一個正序的LIST了。
p->next=L->next; 作用是:用 P 的next指針,指向頭結點的next指針;
L->next=p;//插入到表頭 作用是:用頭結點的next指針,指向P
如:
原來的鏈表: L->N1->N2->N3-null
插入P結點: L N1->N2->N3-null
| /|\
|-> P----- |