初学,关于链表的有关问题!赐教
初学,关于链表的问题!大虾赐教!
小弟初学数据结构,以下是一个修改错误的程序,
当选择1建立表的时候,为什么num或者sex等跟规定的字符相同时候,就会把后面的
一同归并到前面的变量呢?
比如输入1,num=0001,name=li,sex=boy,phone=123456,addr=china,
输出为什么是 0001,li,boy123456,123456,china呢??
很迷惑,请大虾不吝赐教!谢过了!
还有,这个程序还有其他的毛病吗?
初学,请大家赐教!
以下是源代码:
#define NULL 0
#include "stdio.h "
#include "string.h "
#include "stdlib.h "
#include "malloc.h "
typedef struct {
char num[6];
char name[10];
char sex[5];
char phone[15];
char addr[32];
} DataType;
typedef struct node {
DataType data;
struct node *next;
} ListNode;
typedef ListNode *LinkList;
LinkList head;
ListNode *p;
int menu_select();
LinkList CreateList(void);
void InsertNode(LinkList head,ListNode *p);
ListNode *ListFind(LinkList head);
void DelNode(LinkList head);
void printList(LinkList head);
void main()
{
for( ; ; ){
switch(menu_select( ) )
{
case 1:
printf( "**********************************\n ");
printf( "* linklist create *\n ");
printf( "**********************************\n ");
head=CreateList( );
break;
case 2:
printf( "**********************************\n ");
printf( "* add *\n ");
printf( "**********************************\n ");
printf( "\n ");
printf( "************************************* \n ");
p=(ListNode *)malloc(sizeof(ListNode));
scanf( "%s%s%s%s%s ",p-> data.num,p-> data.name,p-> data.sex,
p-> data.phone,p-> data.addr);
InsertNode(head,p);
break;
case 3:
printf( "***********************************\n ");
printf( "* search *\n ");
printf( "***********************************\n ");
p=ListFind(head);
if (p!=NULL) {
printf( "\n ");
printf( "--------------------------------------------------\n ");
printf( "%s,%s,%s,%s,%s\n ",p-> data.num,p-> data.name,
p-> data.sex,p-> data.phone,p-> data.addr);
小弟初学数据结构,以下是一个修改错误的程序,
当选择1建立表的时候,为什么num或者sex等跟规定的字符相同时候,就会把后面的
一同归并到前面的变量呢?
比如输入1,num=0001,name=li,sex=boy,phone=123456,addr=china,
输出为什么是 0001,li,boy123456,123456,china呢??
很迷惑,请大虾不吝赐教!谢过了!
还有,这个程序还有其他的毛病吗?
初学,请大家赐教!
以下是源代码:
#define NULL 0
#include "stdio.h "
#include "string.h "
#include "stdlib.h "
#include "malloc.h "
typedef struct {
char num[6];
char name[10];
char sex[5];
char phone[15];
char addr[32];
} DataType;
typedef struct node {
DataType data;
struct node *next;
} ListNode;
typedef ListNode *LinkList;
LinkList head;
ListNode *p;
int menu_select();
LinkList CreateList(void);
void InsertNode(LinkList head,ListNode *p);
ListNode *ListFind(LinkList head);
void DelNode(LinkList head);
void printList(LinkList head);
void main()
{
for( ; ; ){
switch(menu_select( ) )
{
case 1:
printf( "**********************************\n ");
printf( "* linklist create *\n ");
printf( "**********************************\n ");
head=CreateList( );
break;
case 2:
printf( "**********************************\n ");
printf( "* add *\n ");
printf( "**********************************\n ");
printf( "\n ");
printf( "************************************* \n ");
p=(ListNode *)malloc(sizeof(ListNode));
scanf( "%s%s%s%s%s ",p-> data.num,p-> data.name,p-> data.sex,
p-> data.phone,p-> data.addr);
InsertNode(head,p);
break;
case 3:
printf( "***********************************\n ");
printf( "* search *\n ");
printf( "***********************************\n ");
p=ListFind(head);
if (p!=NULL) {
printf( "\n ");
printf( "--------------------------------------------------\n ");
printf( "%s,%s,%s,%s,%s\n ",p-> data.num,p-> data.name,
p-> data.sex,p-> data.phone,p-> data.addr);