您的位置:首页 > 其它

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

2013-03-18 15:50 363 查看
笔试题: 
实现一个应用程序:从终端接收不确定个数的字符串,并根据这些字符串建立链表。假如应用程序链表已存在,则插入结点到链表;否则,新建立链表。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐