A. c語言鏈表怎麼通過節點數據查找節點進行插入或更改;
說一下一個大概的思路,首先定義兩個指針,一個指針用來向前移動(叫當前指針),另一個指針緊跟其後(叫前驅指針),這個主要用於鏈表的增刪,判斷是否要刪除當前指針指向的節點,或者增加節點。如果需要刪除當前節點,則前驅指針的下一個節點指向當前指針的下一個節點,釋放當前指針指向的節點,然後當前指針指向前驅指針的下一個節點,增加也是一樣的,一直這樣遍歷整個鏈表。至於修改節點數據,那麼需要一個當前指針就夠了,找到要修改的節點,修改就好了。
閑著沒事干,剛寫了代碼,給你參考一下:
//刪除節點
voiddelete_node_credit(floatconstcredit){
stu*pre_node=head;
stu*cur_node=head->next;
while(cur_node){
if(cur_node->credit==credit){
pre_node->next=cur_node->next;
free(cur_node);
cur_node=pre_node->next;
}else{
pre_node=cur_node;
cur_node=cur_node->next;
}
}
//判斷是否刪除頭節點
if(head->credit==credit){
cur_node=head->next;
free(head);
head=cur_node;
}
}
//新增節點
voidinsert_node_credit(stu*new_node,floatconstcredit){
stu*pre_node=head;
stu*cur_node=head->next;
if(pre_node->credit>credit){//新增節點是頭節點
head=new_node;
new_node->next=pre_node;
return;
}
while(cur_node){
if(cur_node->credit<credit&&pre_node->credit>credit){
pre_node->next=new_node;//插入新的節點
new_node->next=cur_node;
return;
}
pre_node=cur_node;
cur_node=cur_node->next;
}
//新增節點插入鏈表尾部
if(pre_node->credit<credit){
pre_node->next=new_node;
return;
}
}
下面是運行結果
如果你看理解了這個思路,那麼鏈表的增刪查改操作就沒有問題了。有用的話點一下採納,謝謝!!!
B. c語言程序修改:建立鏈表
1、申請分配內存後,應該檢查是否成功分配了;
2、你沒有保存鏈表的頭指針。在生成鏈表的過程中,頭指針丟了。你應該有一個變數在程序運行過程中一直保存頭指針,用其他變數執行生成和輸出的操作;
3、生成鏈表的過程中沒有明確給新節點的NEXT賦NULL;
4、你的輸出過程也有問題,一般應該是看其NEXT是否為NULL來判斷結束的,未結束則應該指向其NEXT。你這里指針一直沒變,而且還是個未賦值的指針;
5、輸入的時候scanf應該是用變數地址。
C. C語言鏈表修改問題.
1.關於insert如下修改
struct student *insert(void)
{
#define NULL 0
int Dim;
struct student *head,*pf,*pb;
int i;
head=NULL;
for(i=0;i<2000;i++)
{
pb=(struct student*) malloc(sizeof (struct student));
if(NULL == pb)
{
printf("malloc fail!\n");//添加空間申請失敗檢查!
return head;
}
printf("Please insert Name, Gender, Age,Ecation,Address,Tel\n");
scanf("%s,%s,%d,%s,%s,%ld\n",pb->Name,pb->Gender,&pb->Age,pb->Ecation,pb->Address,&pb->Tel);//對應的輸入方式修改
if(i==0)
{pf=head=pb;}
else { pf->next=pb;
pb->next=NULL;
pf=pb;}
printf("%d",head->next->Age);
printf("Continue?1 - y 2 - n\n");
scanf("%d",&Dim);
if (Dim==2)
{i=3000;}
}
return head;
}
意思是:
A.依次申請空間,建立鏈表節點,若空間申請失敗則推出!
B.每輸入一次,詢問是否繼續建立節點,輸入數字2則退出!
2.函數 change1
struct student *change1(char NAME[96],struct student *head)
{
char flag = 1;
struct student *pf,*pb;
for(pb=head;pb!=NULL;pb=pb->next)
{
if(strcmp(NAME,pb->Name)==0)
{
printf(「找到相應名字的元素,請修改相應的Ecation\n");
printf("Please insert New ecation grade;");
scanf("%s",pb->Ecation);
flag = 0;
}
}
if(1 == flag)
{
printf(「沒有相應名字的元素\n");
}
}
意思是:
輸入元素的名字,及鏈表頭,查找相應名字的元素,修改Ecation的值!
我幫你添加了一些調試用的句子!
D. C語言中鏈表的修改
字元串處理有問題,就是char nam[30];
輸入字元串,scanf("%s",stu.nam);//不用加取址符
而且,下面賦值nam的時候
最好用strcpy,字元串復制函數
E. 如何用C語言創建一個鏈表,實現增、刪、改、查
#include<stdio.h>
#include<string.h>
#include <malloc.h>
//先定義一種student類型,表示一個學生的信息,如下:
typedef struct student
{
int num; //表示學號
char name[30]; //表示姓名
float score; //表示分數
}student;
//定義一種NODE類型,表示一個結點信息,如下:
typedef struct node
{
student st; //表示一個學生的信息
struct node *next; //表示一個NODE類型的指針
}NODE;
//1、寫出建立一個帶頭結點的線性鏈表的函數,其中每個結點包括學號、姓名、分數三個數據域。函數形式如下:
NODE *creat_link(int direction)
{
NODE *head,*p,*tail;
int xh,i=1;
if(direction==1) //當direction的值為1時,新建立的結點連到尾部
{
tail=head=(NODE *)malloc(sizeof(NODE));
head->next=NULL;
printf("請輸入第%d個學生的學號:",i);
scanf("%d",&xh);
while(xh>0) //從鍵盤臨時輸入學生情況,當輸入的學號非正,則鏈表建立完畢
{
p=(NODE *)malloc(sizeof(NODE));
p->st.num=xh;
printf("請輸入第%d個學生的姓名:",i);
scanf("%s",p->st.name);
printf("請輸入第%d個學生的成績:",i);
scanf("%f",&p->st.score);
p->next=NULL;
tail->next=p;
tail=p;
i=i+1;
printf("請輸入第%d個學生的學號:",i);
scanf("%d",&xh);
}
}
else if(direction==0) //當direction為0時,新建立的結點成為第一個結點
{
head=(NODE *)malloc(sizeof(NODE));
head->next=NULL;
printf("請輸入第%d個學生的學號:",i);
scanf("%d",&xh);
while(xh>0) //從鍵盤臨時輸入學生情況,當輸入的學號非正,則鏈表建立完畢
{
p=(NODE *)malloc(sizeof(NODE));
p->st.num=xh;
printf("請輸入第%d個學生的姓名:",i);
scanf("%s",p->st.name);
printf("請輸入第%d個學生的成績:",i);
scanf("%f",&p->st.score);
p->next=head->next;
head->next=p;
i=i+1;
printf("請輸入第%d個學生的學號:",i);
scanf("%d",&xh);
}
}
return head;
}
//2、寫出輸出上述鏈表各結點數據域值的函數。該函數對應的函數需要一個形參,表示鏈表的頭指針,形式如下:
void print_link(NODE *head)
{
NODE *p;
p=head->next;
printf("%-10s%-20s%-10s\n","學號","姓名","分數");
while(p!=NULL)
{
printf("%-10d%-20s%-10.1f\n",p->st.num,p->st.name,p->st.score);
p=p->next;
}
//該函數能輸出head所指的鏈表的所有結點值,輸出形式如下:
/*本函數輸出線性表sq中所有數據,形式如下:
學號 姓名 分數
12 張三 234.5
18 李四 987.7
……… ……… …….*/
}
//3、寫出在鏈表中刪除結點的函數
int del_link(NODE *head,char name[])
{
NODE *p,*p1;
p=head->next;
p1=head;
while(p!=NULL)
{
if(strcmp(p->st.name,name)!=0)
{
p1=p;
p=p->next;
}
else
{
break;
}
}
if(p!=NULL)
{
p1->next=p->next;
free(p);
return 1;
}
else
{
return 0;
}
//刪除head所指的鏈表中,名字為name的結點,刪除成功返回1,不成功返回0
}
//4、寫出在鏈表中插入結點的演算法
int insert(NODE *head,student x,int wz)
{
NODE *p=head;
int i=0,jg;
if(wz<=0)
{
jg=0;
}
else
{
while(i<wz-1&&p!=NULL)
{
i++;
p=p->next;
}
if(p==NULL)
{
jg=0;
}
if(i=wz-1)
{
//找到wz前面的節點,p指向它
NODE *q;
q=(NODE *)malloc(sizeof(NODE));
q->st.num=x.num;
strcpy(q->st.name,x.name);
q->st.score=x.score;
q->next=p->next;
p->next=q;
jg=1;
}
}
return jg;
//該函數能夠在wz這個結點之前,插入一個新結點,新結點的數據域為x。插入成功返回1,不成功返回0。
}
//5、寫出主函數,分別調用上面演算法所對應的程序,建立鏈表,並輸出鏈表的值。
void main()
{
NODE *head; //定義指針變數head
int wz; //表示插入位置
char xm[30];
student st; //定義一個變數st,用來表示一個學生的信息
head=creat_link(1);
print_link(head); //調用函數建立鏈表,並把返回值送給head;
//調用函數,輸出鏈表中各個結點的值
//輸入一個學生的有關信息,送給變數st的有關成員
printf("\n\n請輸入要插入的位置:");
scanf("%d",&wz); //輸入wz的值
printf("請輸入要插入的學生的學號:");
scanf("%d",&st.num);
printf("請輸入要插入的學生的姓名:");
scanf("%s",st.name);
printf("請輸入要插入的學生的成績:");
scanf("%f",&st.score);
//調用函數,在鏈表中把學生st的值作為一個結點插入,如果插入成功,輸出新鏈表
if(insert(head,st,wz)==1)
{
printf("\n插入成功,新表為:\n");
print_link(head);
}
else
{
printf("插入不成功");
}
//調用函數,在鏈表中刪除一個指定結點的值,如果刪除成功,輸出新鏈表
printf("\n\n請輸入要刪除的學生的姓名:");
getchar();
gets(xm);
if(del_link(head,xm)==1)
{
printf("\n刪除成功,新表為:\n");
print_link(head);
}
else
{
printf("刪除不成功");
}
}
F. c語言使用鏈表進行學生信息管理代碼怎麼修改
這太多了,懶得寫
G. 如何用C語言創建一個鏈表,實現增、刪、改、查
#include
H. C語言單鏈表 求大神教教怎麼修改
#include<stdio.h>
#include<stdlib.h>
typedefstructnode
{
intdata;
structnode*next;
}node;
intmain(void){
intnum;
node*p,*head;
head=NULL;
do{
scanf("%d",&num);
if(num!=-1)
{
p=(node*)malloc(sizeof(node));
p->data=num;
p->next=NULL;
if(head==NULL)
head=p;
else
p->next=head;
head=p;
}
}while(num!=-1);
while(p!=NULL){
printf("%d",p->data);
p=p->next;
}
printf(" ");
return0;
}
已經修改好了,有問題如果問錯在哪可以追問
I. C語言——鏈表的修改
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score[3];
struct student *next;
};
int n=0;
struct student *creat(void) //建立鏈表
{
struct student *head,*p1,*p2;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
scanf("%d %s %f %f %f",&p1->num,p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d %s %f %f %f",&p1->num,p1->name,&p1->score[0],&p1->score[1],&p1->score[2]);
}
p2->next=NULL;
return (head);
}
void main() //main函數
{
struct student *insert(struct student *head,struct student *stu);
struct student *del(struct student *head,int num);
struct student *p, *b,*stu;
int m,flag=1,flag1=1,flag2=1;
b=p=creat();
printf("now,there are %d crunodes are:\n",n);
for(;p!=NULL;)
{
printf("%d%10s%8.2f%8.2f%8.2f\n",p->num,p->name,p->score[0],p->score[1],p->score[2]);
p=p->next;
}
while(flag2!=0) //控制開關
{
printf("if you input '1' that you will have the funtion to delete the crunode\nif you input '2' that you will have the funtion to insert crunode.\nif you input '0' that you will end.\n");
printf("what you want to enter 1 ,2 or 0:");
scanf("%d" ,&flag2);//選擇功能,是增加結點還是刪減結點
if(flag2==1)
{
while(flag)
{
printf("delete the crunode(結點):");
scanf("%d",&m);
p=del(b,m);
if(n==0)
{
b=p;
flag=0;
break;
}
b=p;
printf("now,there are %d crunodes are:\n",n);
for(;p!=NULL;)
{
printf("%d%10s%8.2f%8.2f%8.2f\n",p->num,p->name,p->score[0],p->score[1],p->score[2]);
p=p->next;
}
printf("contiune or not(0/1):");
scanf("%d",&flag);
}
}
if(flag2==2)
{
while(flag1)
{
if(flag)//第一次是不執行這句的
{
if(n==0) b=NULL;
p=insert(b,stu);
b=p;
printf("now,there are %d crunodes are:\n",n);
for(;p!=NULL;)
{
printf("%d%10s%8.2f%8.2f%8.2f\n",p->num,p->name,p->score[0],p->score[1],p->score[2]);
p=p->next;
}
printf("continue or not :(0/1):");
scanf("%d",&flag1);
if(flag1==0) break;
}
//起到控制整個while循環的作用
printf("please input the imformation of the student who you want to add:");
stu=(struct student *)malloc(LEN); //申請內存
scanf("%d %s %f %f %f",&stu->num,stu->name,&stu->score[0],&stu->score[1],&stu->score[2]);//輸入學生的信息
flag=1;
// 起到控製作用,目的是為了第一次不執行上上個if語句
}
}
}
}
struct student *del(struct student *head,int num) //刪除結點
{
struct student *p1,*p2;
if(head==NULL)
{
printf("this is list null.\n");
return head;
}
else
p1=head;
while(num!=p1->num && p1->next!=NULL)
{
p2=p1;p1=p1->next; //一個一個的下去
}
if(num==p1->num)
{
if(p1==head) //才一個結點
{
head=p1->next;
free(p1);
}
else //不止一個結點
{
p2->next=p1->next;
free(p1);
}
n=n-1;
printf("delete:%d\n",num);
printf("there are %d crunode(結點) now.\n",n);
}
else
printf("%d crunode(結點) has not been found!\n",num);
if(n==0) //如果刪除結點後結點個數為0
return NULL;
else
return (head);
}
struct student *insert(struct student *head,struct student *stu) //增加結點
{
struct student *p1,*p2,*p0;
p1=head;
p0=stu;
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;
p0->next=p1;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return(head);
}
我正好學到鏈表的,所以就話了一個晚上來做了這個,我哦完善了一下,如果有什麼不滿意的話,或有什麼不懂的話你可以加QQ419842687
J. 求大神將這段c語言代碼怎麼改成用鏈表的,謝謝
鏈表就是在你現有結構成員中添加一個指針成員,每個結構變數的指針指向下一個結構變數,就組成了鏈表。對鏈表的增刪改查就是通過這個指針來實戰。
你原代碼用的是結構數組。
要改成鏈表,不是改,而且全部要重寫。
函數傳遞參數也不能用結構數組而改用鏈表首節點或頭節點以及尾指針。
注意:我代碼中學生學號應該是唯一的,不能重復,我沒有寫驗證,你自己添加輸入驗證,學號最好是用一個變數自增。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
#include<malloc.h>
typedef struct student
{
char no[15]; /*學號*/
char name[30]; /*姓名*/
int age; /*年齡*/
char sex[5]; /*性別*/
char bir[30]; /*出生年月*/
char add[30]; /*地址*/
char tel[13]; /*電話*/
char e_mail[30]; /*電子郵件*/
char zhuanyebanji[40]; /*專業班級*/
struct student *next;//鏈表指針
}STU;
int main()
{
int num,i;
char no[15];
STU *stuHead=(STU *)malloc(sizeof(STU)),*stuTail=NULL;
if(stuHead)
stuHead->next=NULL;
int write(STU *stuHead,STU **stuTail);//錄入一條學生信息生成一個鏈表節點,成功返回1,失敗返回0
int read(STU *stuHead);//讀取鏈表,遍歷鏈表所有節點,返回節點個數
STU *search(STU *stuHead,char *no);//通過學號查詢,返回查詢到的節點
int modify(STU *stuHead,char *no);//修改指定學生信息,成功返回1,失敗返回0
int del(STU *stuHead,STU **stuTail,char *no);//刪除指定學生信息,成功返回1,失敗返回0
int tongji(STU *stuHead);//不知道你要統計什麼,自己參照我的代碼寫吧
for(i=0;;i++)
{
printf("------------------------------歡迎使用學籍管理系統------------------------------");
printf(" ");
printf(" ");
printf(" 1、添加學生信息 2、查詢學生信息 ");
printf(" ");
printf(" ");
printf(" 3、修改學生信息 4、瀏覽學生信息 ");
printf(" ");
printf(" ");
printf(" 5、刪除學生信息 6、統計學生信息 ");
printf(" ");
printf(" ");
printf(" 7、退出 ");
printf(" ");
printf(" ");
printf("-------------------------------------------------------------------------------");
printf(" ");
printf(" ");
printf("請選擇要進行的操作:");
system("color F0");
scanf("%d",&num);
switch(num)
{
case 1:if(!write(stuHead,&stuTail))printf("異常!新增信息失敗! ");
break;
case 2:printf("輸入要查找的學生學號:");scanf("%s",no),search(stuHead,no);
break;
case 3:printf("輸入要修改的學生學號:");scanf("%s",no);if(!modify(stuHead,no))printf("異常!修改失敗! ");
break;
case 4:system("cls");read(stuHead);
break;
case 5:printf("輸入要刪除的學生學號:");scanf("%s",no);del(stuHead,&stuTail,no);
break;
case 6:tongji(stuHead);
break;
}
}
return 0;
}
int write(STU *stuHead,STU **stuTail)
{
STU *stuNew=(STU *)malloc(sizeof(STU));
if(!stuNew)
return 0;
stuNew->next=NULL;
printf("請輸入要新建的學生信息: ");
printf("學號:"),scanf("%s",stuNew->no);
printf("姓名:"),scanf("%s",stuNew->name);
printf("年齡:"),scanf("%d",&stuNew->age);
printf("性別:"),scanf("%s",stuNew->sex);
printf("出生年月:"),scanf("%s",stuNew->bir);
printf("地址:"),scanf("%s",stuNew->add);
printf("電話:"),scanf("%s",stuNew->tel);
printf("專業班級:"),scanf("%s",stuNew->zhuanyebanji);
if(!stuHead->next)
stuHead->next=stuNew;
else
(*stuTail)->next=stuNew;
(*stuTail)=stuNew;
return 1;
}
int read(STU *stuHead)
{
int cnt=0;
printf("鏈表信息: ");
while(stuHead->next)
{
printf("-------------------------- ");
printf("學號:%s ",stuHead->next->no);
printf("姓名:%s ",stuHead->next->name);
printf("年齡:%d ",stuHead->next->age);
printf("性別:%s ",stuHead->next->sex);
printf("出生年月:%s ",stuHead->next->bir);
printf("地址:%s ",stuHead->next->add);
printf("電話:%s ",stuHead->next->tel);
printf("專業班級:%s ",stuHead->next->zhuanyebanji);
printf("-------------------------- ");
cnt++;
stuHead=stuHead->next;
}
return cnt;
}
STU *search(STU *stuHead,char *no)
{
while(stuHead->next)
{
if(!strcmp(stuHead->next->no,no))
{
printf("學號%s的學生信息為: ",no);
printf("學號:%s ",stuHead->next->no);
printf("姓名:%s ",stuHead->next->name);
printf("年齡:%d ",stuHead->next->age);
printf("性別:%s ",stuHead->next->sex);
printf("出生年月:%s ",stuHead->next->bir);
printf("地址:%s ",stuHead->next->add);
printf("電話:%s ",stuHead->next->tel);
printf("專業班級:%s ",stuHead->next->zhuanyebanji);
return stuHead->next;
}
stuHead=stuHead->next;
}
printf("未找到指定的學生信息! ");
return NULL;
}
int modify(STU *stuHead,char *no)
{
STU *stu=search(stuHead,no);
if(!stu)
return 0;
printf("請輸入要修改的信息: ");
printf("學號:"),scanf("%s",stu->no);
printf("姓名:"),scanf("%s",stu->name);
printf("年齡:"),scanf("%d",&stu->age);
printf("性別:"),scanf("%s",stu->sex);
printf("出生年月:"),scanf("%s",stu->bir);
printf("地址:"),scanf("%s",stu->add);
printf("電話:"),scanf("%s",stu->tel);
printf("專業班級:"),scanf("%s",stu->zhuanyebanji);
printf("信息修改成功! ");
return 1;
}
int del(STU *stuHead,STU **stuTail,char *no)
{
int flag=0;
STU *stu=NULL,*stuHSave=stuHead;
while(stuHead->next)
{
if(!strcmp(stuHead->next->no,no))
{
stu=stuHead->next;
stuHead->next=stuHead->next->next;
free(stu);
stu=NULL;
flag=1;
stuHead=stuHSave;
}
stuHead=stuHead->next;
}
if(flag)
(*stuTail)=stuHead;
else
{
printf("未找到對應學號的學生!刪除失敗! ");
return 0;
}
return 1;
}
int tongji(STU *stuHead)
{
return 1;
}