#include struct node /*节点的数据结构*/
{
int num;
char str[20];
struct node *next;
};
main( )
{
/*函数声明*/
struct node* creat(struct node *head);
struct node* insert(struct node *head, char *pstr, int n);
struct node* delet(struct node *head, char *pstr);
void print(struct node *head);
struct node *head;
char str[20];
int n;
head=NULL; /*做空表*/
head=creat(head); /*调用函数创建以head 为头的链表*/
print(head);/*调用函数输出节点*/
printf(" input inserted num,name:");
gets(str); /*输入学号*/
n=atoi (str);
gets(str); /*输入姓名*/
head=insert(head, str, n); /*将节点插入链表*/
print (head); /*调用函数输出节点*/
printf(" input deleted name:");
gets(str); /*输入被删姓名*/
head=delet(head,str); /*调用函数删除节点*/
print (head); /*调用函数输出节点*/
system("pause");
return 0;
}
/* * * 创建链表* * * * * * * * * * * */
struct node *creat(struct node *head)
{
char temp[30];
struct node *p1,*p2;
p1 = p2=(struct node*) malloc(sizeof(struct node));
printf ("input num, name: ;");
printf("exit:double times Enter!");
gets(temp) ;
gets(p1->str);
p1->num=atoi (temp);
p1->next =NULL;
while (strlen(p1->str)>0)
{
if (head == NULL) head = p1;
else p2->next=p1;
p2 = p1;
p1=(struct node *)malloc(sizeof(struct node));
printf ("input num, name: ");
printf("exit:double times Enter!");
gets(temp ) ;
gets(p1->str);
p1->num=atoi (temp);
p1->next = NULL;
}
return head;
}
/* * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * 插入节点* * * * * * * * * */
struct node *insert (struct node *head, char *pstr, int n)
{
struct node *p1,*p2,*p3;
p1=(struct node*)malloc(sizeof(struct node));
strcpy (p1->str, pstr);
p1->num = n ;
p2 = head ;
if ( head == NULL )
{
head = p1 ; p1->next = NULL ;
}
else
{
while (n>p2->num&&p2->next!=NULL)
{
p3 = p2;
p2 = p2->next;
}
if (n<=p2->num)
if (head==p2)
{
head = p1 ;
p1->next = p2 ;
}
else
{
p3->next = p1 ;
p1->next = p2 ;
}
else
{
p2->next = p1;
p1->next = NULL ;
}
}
return (head) ;
}
/* * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * 删除节点* * * * * * * * * * * * */
struct node *delet (struct node *head, char *pstr)
{
struct node *temp,*p;
temp = head ;
if (head==NULL)
printf("List is null!");
else
{
temp = head ;
while (strcmp(temp->str,pstr)!=0&&temp->next!=NULL)
{
p = temp;
temp = temp->next;
}
if (strcmp(temp->str , pstr ) == 0 )
{
if (temp== head)
{
head = head->next ;
free(temp) ;
}
else
{
p->next =temp->next;
printf("delete string :%s",temp->str);
free(temp) ;
}
}
else printf("no find string!");
}
return(head);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * * 链表各节点的输出* * * * * * * * * */
void print (struct node *head)
{
struct node *temp;
temp =head ;
printf(" output strings:");
while (temp!=NULL)
{
printf( "%d - - - - %s" , temp->num ,temp->str );
temp = temp->next;
}
}