实现一个应用程序:从终端接收不确定个数的字符串,并根据这些字符串建立链表

笔试题:

    实现一个应用程序:从终端接收不确定个数的字符串,并根据这些字符串建立链表。假如应用程序链表已存在,则插入结点到链表;否则,新建立链表。

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

typedef struct _node_t {
   char*            name;
   struct _node_t*         next;
} Node;


Node* create_list(const char* names[], int num)
{
  Node* head = (Node*)malloc(sizeof(Node));
  Node* p = head;
  Node* s;
  int i=1;
  head->name = 0;
  head->next= 0;

  for(i = 1; i < num; i++)
  {
      s = (Node*)malloc(sizeof(Node));
      p->next = s;
      s->name = (char*)malloc(strlen(names[i])+1);
      strcpy(s->name, names[i]);
      s->next =0;
      p = s;
  }

  return head;
}

int Destroy_list( Node* head)
{
    Node* Next;
    while( NULL != head )
   { 
        Next = head ->next;
        if ( NULL != head->next )
	{
           printf("%s\n", head ->next->name);
	}
        head ->next = NULL;
        free( head ->name);
        free( head );
        head = Next;
    }
     printf("succeed in deleting List.\n");

}


int main(int argc, char **argv) 
{
    printf("Argument Count is %d.\n", argc);
    Node* head = create_list(argv, argc);
    Destroy_list(head);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值