数据结构单链表的打印c语言,数据结构单链表应用编写一个完整的C语言程序,输..._结构工程师_帮考网...

#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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值