我現在在做單回圈鏈表的基本操作,但是呼叫Print函式只能輸出最新一次Set函式傳入的值,不能列印全部,請問是咋回事?
比如 輸入1 Alice 10001 再輸入1 Sam 10009 再輸入1 Willian 10010 在輸入2 ,只能輸出最新一次輸入的內容,而且怎么也找不到原因,感覺自己的代碼沒啥問題
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student{
int data;
char name[20];
struct student *next;
}node;
node*Set(node*head,node*p); //在末端插入p節點
void Print(node*head); //列印輸出所有節點
node*Insert(node*head,node*p,int n) ;//在第n個后面插入
node*Del(node*head,int data);//洗掉學號data的節點
int Length(node*head);//求長度
node*Sort(node*head) ;//按data升序排序
int main()
{
int choice,num,l,n;
char r[20];
node*head=NULL;
node*p=(node*)malloc(sizeof(node));
printf("Select:\n1.Set,\n2.Print,\n3.Insert,\n4.Delete,\n5.GetLength,\n6.Sort.\n");
scanf("%d",&choice);
while(choice>=1&&choice<=6)
{
switch(choice){
case 1:printf("Enter name and data:\n");
scanf("%s %d",r,&num);
strcpy(p->name,r);
p->data=https://bbs.csdn.net/topics/num;
head=Set(head,p);
break;
case 2:Print(head);break;
case 3:printf("Enter name and data:\n");
scanf("%s %d",r,&num);
strcpy(p->name,r);
p->data=https://bbs.csdn.net/topics/num;
printf("Enter the location:\n");
scanf("%d",&n);
head=Insert(head,p,n);break;
case 4:printf("Enter data:\n");
scanf("%d",&num);
head=Del(head,num);break;
case 5:l=Length(head);printf("Length = %d\n"); break;
case 6:head=Sort(head);break;
default:;break;
}
printf("Select:\n1.Set,\n2.Print,\n3.Insert,\n4.Delete,\n5.GetLength,\n6.Sort.\n");
scanf("%d",&choice);
}
return 0;
}
node*Set(node*head,node*p) //在尾部插入p節點
{
node*ptr;
if(head==NULL){
head=p;
head->next=head;
}
else{
ptr=head;
while(ptr->next!=head)
{
ptr=ptr->next;
}
/*找到尾巴*/
p->next=head;
ptr->next=p;
}
return head;
}
void Print(node*head)
{
node*ptr=head;
if(head==NULL){
printf("No records\n");
}
else{
printf("%d %s\n",head->data,head->name);
ptr=ptr->next;
for(;ptr!=head;ptr=ptr->next)
{
printf("%d %s\n",ptr->data,ptr->name);
}
}
}
node*Insert(node*head,node*p,int n) //在第n個節點后面插入p的值 ,n>0
{
node *q=head;
int i=1;
if(head==NULL){
printf("Cannot Insert\n") ;
}else
{
while(q->next!=head&&i<n)
{
q=q->next;
i++;
}
if(i==n){
node *r=q->next;
p->next=r;
q->next=p;
} else{
printf("Cannot Insert\n");
}
}
return head;
}
node*Del(node*head,int data)//洗掉學號為data的節點
{
if((head==NULL)&&(head->next==head&&head->data!=data))
{
printf("Cannot delete\n");
} else if(head->next==head&&head->data=https://bbs.csdn.net/topics/=data){
free(head);
}else {
node*p1,*p2;
int flag=1;
p1=head;
p2=p1->next;
if(p1->data=https://bbs.csdn.net/topics/=data)
{
while(p2->next!=head)
{
p2=p2->next;
} //p2尾巴
head=head->next;
p2->next=head;
free(p1);
}
else
{
for(;p2!=head;p1=p1->next){
if(p2->data=https://bbs.csdn.net/topics/=data){
flag=0;
p1->next=p2->next;
free(p2);
break;
}
p2=p2->next;
}
if(flag==1){
printf("Cannot delete\n");
}
}
}
return head;
}
int Length(node*head){
int count;
node*p;
if(head==NULL){
count=0;
} else if(head->next==head)
{
count=1;
}else
{
count=1;
for(p=head->next;p!=head;p=p->next)
{
count++;
}
}
return count;
}
node*Sort(node*head) //按學號升序排序
{
int t;
char str[20];
node*i=head,*j=head;
if(head==NULL||(head->next==head)){ //單個節點
return head;
}
else{
for(i=head;i->next!=head;i=i->next){
for(j=head;j->next!=head;j=j->next){
if((j->data)>(j->next->data))
{
t=j->data;j->data=https://bbs.csdn.net/topics/j->next->data;j->next->data=t;
strcpy(str,j->name);
strcpy(j->name,j->next->name);
strcpy(j->next->name,str);
}
}
}
return head;
}
}
uj5u.com熱心網友回復:
為什么代碼會這么多?就實作輸入和列印uj5u.com熱心網友回復:
現在鏈表插入的時候 都不用malloc 分配記憶體 洗掉的時候free 釋放記憶體了嗎 --- -- --uj5u.com熱心網友回復:
代碼太多 我也沒仔細看 就看了你的插入 沒看見你哪里分配了記憶體 你只是在main函式開頭分配了一個節點的結構體記憶體 你要在每次插入的時候都malloc分配一個記憶體uj5u.com熱心網友回復:
支持樓上。第20行改成:node *p;
在case 1: 后面增加一行 p = (node*)malloc(sizeof(node));
uj5u.com熱心網友回復:
代碼太多太亂,沒細看,但從node*Set(node*head,node*p) //在尾部插入p節點
void Print(node*head)
這兩個子函式中看到邏輯應該是有問題的。
單鏈表的尾結點必須指向NULL,否則你不知道單鏈表何時到尾。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/27708.html
標籤:C語言
上一篇:C++ 多執行緒出現無效地址
