首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于链表的有关问题求解

2012-04-07 
关于链表的问题求解#include stdafx.h#includestdlib.h#includestring.hstruct stud_node{int numc

关于链表的问题求解
#include "stdafx.h"
#include"stdlib.h"
#include"string.h"
struct stud_node{
int num;
char name[20];
int score;
struct stud_node* next;
};

struct stud_node* Create_Stu_Doc();
struct stud_node * InsertDoc(struct stud_node* head,struct stud_node* stud);
struct stud_node* DeleteDoc(struct stud_node* head,int num);
void Print_Stu_Doc(struct stud_node* head);

int _tmain(int argc, _TCHAR* argv[])
{
struct stud_node* head,*p;
int choice,num,score;
char name[20];
int size =sizeof (struct stud_node);
do
{
printf("1:Creat 2:Insert 3:Delete 4:Print 0:Exit \n");
scanf("%d",&choice);
switch(choice){
case 1:
head=Create_Stu_Doc();
break;
case 2:
printf("Input num,name,and score:\n");
scanf("%d%s%d",&num,name,&score);
p=(struct stud_node*) malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
head=InsertDoc(head,p);
break;
case 3:
printf("Input num:\n");
scanf("%d",&num);
head=DeleteDoc(head,num);
break;
case 4:
Print_Stu_Doc(head);
break;
case 0:
break;
}
}while(choice!=0);

return 0;
}

//新建链表
struct stud_node* Create_Stu_Doc()
{
struct stud_node* head,*p;
int num,score;
char name[20];
int size=sizeof(struct stud_node);

head=NULL;
printf("input num,name,and score:\n");
scanf("%d%s%d",&num,name,&score);
while (num!=0){
p=(struct stud_node*) malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
head=InsertDoc(head,p); //调用插入函数
scanf("%d%s%d",&num,name,&score);
}
return head;
}

//插入操作
struct stud_node* InsertDoc(struct stud_node* head,struct stud_node *stud)
{
struct stud_node *ptr,*ptr1,*ptr2;
ptr2=head;
ptr=stud;
//
if(head==NULL){
head=ptr;
head->next=NULL;
}
else{
while((ptr->num>ptr2->num)&&(ptr2->next!=NULL))
{
ptr1=ptr2;
ptr2=ptr2->next;
}
if(
ptr->num<=ptr2->num)
{
if(head==ptr2) head=ptr;
else ptr1->next=ptr;
ptr->next=ptr2;
}
else{
ptr2->next=ptr;
ptr->next=NULL;
}
}
return head;
}
//删除操作
struct stud_node* DeleteDoc(struct stud_node* head,int num)
{
struct stud_node* ptr1,*ptr2;
//
while (head!=NULL&&head->num==num)
{
ptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL)
return NULL;
ptr1=head;
ptr2=head->next;
while(ptr2!=NULL){
if(ptr2->num==num){
ptr1->next=ptr2->next;
free(ptr2);
}
else
ptr1=ptr2;
ptr2=ptr1->next;
}
return head;
}
//遍历操作
void Print_Stu_Doc(struct stud_node * head)
{
struct stud_node * ptr;
if(head==NULL){
printf("\nNO Records");
return;
}
printf("\nThe student's Records are:\n");
printf(" NUM Name Score \n");
for(ptr=head;ptr;ptr=ptr->next)
printf("%8d%20s%6d\n",ptr->num,ptr->name,ptr->score);
}
 这些代码编译链接都能通过!但是运行的结果总是没有反应。。。得不到他应有的功能!
用f10 调试也找不到错误!

[解决办法]

#include"stdlib.h"
#include"string.h"
#include "stdio.h"
typedef struct stud_node{
int num;
char name[20];
int score;
struct stud_node* next;
}LNode,LinkList;

LinkList* Create_Stu_Doc();
LinkList * InsertDoc(LinkList* head,LinkList* stud);
LinkList* DeleteDoc(LinkList* head,int num);
void Print_Stu_Doc(LinkList* head);

int main()
{
LinkList* head,*p;
int choice,num,score;
char name[20];
int size =sizeof (LNode);
do
{
printf("1:Creat 2:Insert 3:Delete 4:Print 0:Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1: head=Create_Stu_Doc();
break;

case 2: printf("Input num,name,and score:\n");
scanf("%d%s%d",&num,name,&score);
p=(LinkList *) malloc(size);
p->num=num;
strcpy(p->name,name);
p->score=score;
head=InsertDoc(head,p);
break;

case 3: printf("Input num:\n");
scanf("%d",&num);
head=DeleteDoc(head,num);
break;

case 4: Print_Stu_Doc(head);
break;

case 0: break;

}
}while(choice!=0);

return 0;
}

//新建链表带头结点
LinkList* Create_Stu_Doc()
{
LinkList* head,*p;
int num,score;
char name[20];
int size=sizeof(LNode);
head = (LinkList *)malloc(size);
head=NULL;
printf("input num,name,and score:\n");
scanf("%d%s%d",&num,name,&score);
while (num!=0)
{
p=(LinkList*) malloc(size);
if (!p)
{
printf("malloc error\n");
return 0;
}
p->num=num;
strcpy(p->name,name);
p->score=score;
head=InsertDoc(head,p); //调用插入函数
scanf("%d%s%d",&num,name,&score);
}
return head;
}

//插入操作
LinkList* InsertDoc(LinkList* head,LinkList *stud)
{
LinkList *ptr,*ptr1,*ptr2;
ptr2=head;
ptr=stud;
//
if(head==NULL)
{
head=ptr;
head->next=NULL;
}
else
{
while((ptr->num>ptr2->num)&&(ptr2->next!=NULL))
{
ptr1=ptr2;
ptr2=ptr2->next;
}
if(ptr->num<=ptr2->num)
{
if(head==ptr2) head=ptr;
else 
ptr->next=ptr2;
ptr1->next=ptr;

}
else
{
ptr2->next=ptr;
ptr->next=NULL;
}
}

return head;
}
//删除操作
LinkList* DeleteDoc(LinkList* head,int num)
{
LinkList* ptr1,*ptr2;
//
while (head!=NULL&&head->num==num)
{
ptr2=head;
head=head->next;
free(ptr2);
}
if(head==NULL)
return NULL;
ptr1=head;
ptr2=head->next;
while(ptr2!=NULL)
{
if(ptr2->num==num)
{
ptr1->next=ptr2->next;
free(ptr2);
}
else
ptr1=ptr2;
ptr2=ptr1->next;
}
return head;
}
//遍历操作
void Print_Stu_Doc(LinkList * head)
{
LinkList * ptr;
if(head==NULL)
{
printf("\nNO Records");
return;
}
printf("\nThe student's Records are:\n");
printf(" NUM Name Score \n");
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{

printf("%8d%20s%6d\n",ptr->num,ptr->name,ptr->score);
}
}
 
 

热点排行