Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104633567
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-04-16 20:57:00

 来源:

=NULL; /*做空表* /
head=creat (head); / *调用函数创建以head 为头的链表* /
p r i n t ( h e a d ) ;/ *调用函数输出节点* /
printf("\n input inserted num,name:\n");
gets(str); /*输入学号* /
n=atoi (str);
gets(str); /*输入姓名* /
head=insert (head, str, n); 将/*节点插入链表*/
print (head); / *调用函数输出节点*/
printf("\n input deleted name:\n");
gets(str); /*输入被删姓名* /
head=delet(head,str); /调*用函数删除节点*/
print (head); /*调用函数输出节点* /
r e t u r n ;
}
/ * * * * * * * * * * * * * * * * * * * * * * /
/ * * * 创建链表* * * * * * * * * * * * /
struct node *creat(struct node *head)
{
char temp[30];
struct node *pl,*p2;
pl=p2=(struct node*) malloc(sizeof(struct node));
printf ("input num, name: \n;")
printf("exit:double times Enter!\n");
g e t s ( t e m p ) ;
gets (p1->str);
pl->num=atoi (temp);
p l - > n e x t = N U L L ;
while (strlen (pl->str)>0
{
if (head==NULL) head=pl;
else p2->next=p1;
P 2 = p l ;
pl=(struct node *)malloc(sizeof(struct node));
printf ("input num, name: \n");
printf("exit:double times Enter!\n");
g e t s ( t e m p ) ;
gets(pl ->str);
p1->num=atoi (temp);
P 1 - > n e x t = N U L L ;
}
return head;
}
/ * * * * * * * * * * * * * * * * * * * * /
/ * * * * * * * * * * 插入节点* * * * * * * * * * /
struct node *insert (head, pstr,n);
struct node *head;
char *pstr;
int n;
{
struct node *pl,*p2,*p3;
p1=(struct node*)malloc(sizeof(struct node));
strcpy (p1->str, pstr);
p 1 - > n u m = n ;
p 2 = h e a d ;
i f ( h e a d = = N U L L )
{
h e a d = p l ; p l - > n e x t = N U L L ;
}
e l s e
{
while (n>p2->num&&p2->next!=NULL)
{
p 3 = P 2
p 2 = p 2 - > n e x t ;
}
if (n<=p2->num)
if (head==p2)
{
h e a d = p l ;
p l - > n e x t = p 2 ;
}
else
{
p 3 - > n e x t = p l ;
p l - > n e x t = p 2 ;
}
else
{
p 2 - > n e x t = p l ;
p l - > n e x t = N U L L ;
}
}
r e t u r n ( h e a d ) ;
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * * * * * 删除节点* * * * * * * * * * * * * /
struct node *delet (head, pstr)
struct node *head;
char *pstr;
{
struct node *temp,*p;
t e m p = h e a d ;
if (head==NULL)
printf("\nList is null!\n");
else
{
t e m p = h e a d ;
while (strcmp(temp->str,pstr)!=O&&temp->next!=NULL)
{
p = t e m p ;
t e m p = t e m p - > n e x t ,
}
i f ( s t r c m p ( t e m p - > s t r , p s t r ) = = 0 )
{
if (temp== head)
{
h e a d = h e a d - > n e x t ;
f r e e ( t e m p ) ;
}
else
{
p->next =temp->next;
printf("delete string :%s\n",temp->str);
f r e e ( t e m p ) ;
}
}
else printf("\nno find string!\n");
}
return(head);
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * * * * * * * * * * 链表各节点的输出* * * * * * * * * * /
void print (struct node *head)
{
struct node *temp;
t e m p = h e a d ;
printf("\n output strings:\n");
while (temp!=NULL)
{
p r i n t f ( " \ n % d - - - - % s \ n " , t e m p - > n u m ,t e m p - > s t r ) ;
t e m p = t e m p - > n e x t ;
}
r e t u r n ;
}
阅读(337) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~