链表操作

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

typedef struct node
{
 unsigned long cell_id;
 char cel_wlan[3][10];
 char cell_busy;
 char wlan_quality;
 unsigned long BSSID;
    char SSID[10];
 struct node * next;
}NODE, *PNODE;

/* 节点初始化 */
PNODE node_init(PNODE node)
{
 node = (PNODE)malloc(sizeof(NODE));
 if(NULL == node)
 {
  printf("malloc error\n");
  return -1;  
 }
 memset(node, 0, sizeof(NODE));
 printf("Please input cell_id & cell_busy & wlan_qulaty & SSID\n");
 scanf("%d,%c,%c,%s",&(node->cell_id), &(node->cell_busy), &(node->wlan_quality), node->SSID);

// printf("Input number finished!\n");
// printf("cell_id = %d cell_busy = %c wlan_qulaty = %c SSID = %s\n", node->cell_id, node->cell_busy, node->wlan_quality, node->SSID);
 node->next = NULL;

 return node;
}

/* 打印链表 */
void print_list(PNODE head)
{
 PNODE p = head;
 while(NULL != p)
 {
  printf("cell_id = %d cell_busy = %c wlan_qulaty = %c SSID = %s\n", p->cell_id, p->cell_busy, p->wlan_quality, p->SSID);
  p = p->next;
 }
}

/* 插入链表节点 小区ID号按从小到大排列*/
PNODE insert_node(PNODE head,PNODE new_node)
{
 PNODE p = NULL, pre = NULL;
 new_node = node_init(new_node);
 if(NULL == head)
 {
  head = new_node;
  return head;
 }
 else
 p = head;
 while(NULL != p)
 {
//  printf("new_node->cell_id = %d\np->cell_id = %d\n",new_node->cell_id, p->cell_id);
  if(new_node->cell_id < p->cell_id)
  {
   if(head == p)
   { 
    head = new_node;
    head->next = p;
   }
   else
   {
    new_node->next = pre->next;
    pre->next = new_node;
   }
   break;
  }
  pre = p;
  p = p->next;
 }
 if(NULL == p)
 {
  pre->next = new_node;
 }
 return head;
}

/* 创建链表 */
PNODE create_list()
{
 PNODE head = NULL, p = NULL, q = NULL;
 unsigned long x;
 printf("scanf any number but 0 to start create_list()\n");
 scanf("%x",&x);
 while(0 != x)
 {
  head = insert_node(head,p);
//  p = node_init(p);
//  if(NULL == head)
//  {
//   head = p;
//   printf("head is %x\n", head);
//  }
//  else
////   /* 直接插入,不对链表节点顺序进行排序 */
////   q->next = p;
////   q = p;
//  {
//   /* 链表节点按小区ID从小到大顺序进行排序 */
//   q = head;
//   insert(head,)
//  } 
  
  printf("scanf 0 can break out\n");
  scanf("%x",&x);
 }
 printf("create list successful\n");
 return head;
}


/* 删除链表节点 */
PNODE delete_node(PNODE head,PNODE node)
{
 PNODE p = NULL, pre = NULL;
 p = head;
// printf("node addr = %x\n",node);
 while(NULL != p)
 {
//  printf("node->cell_id = %d\np->cell_id = %d\n",node->cell_id, p->cell_id);
  if(p->cell_id == node->cell_id)
  {
   if(p == head)
   {
    head = p->next;
   }
   else
   {
    pre->next = p->next;

   }
  }
  pre = p;
  p = p->next;
 }
 return head;
}

/* 修改链表节点 */
PNODE change_node(PNODE head, PNODE node)
{
 PNODE p = NULL, pre = NULL;
 p = head;
// printf("node addr = %x\n",node);
 while(NULL != p)
 {
//  printf("node->cell_id = %d\np->cell_id = %d\n",node->cell_id, p->cell_id);
  if(p->cell_id == node->cell_id)
  {
   p->cell_busy = node->cell_busy;
   p->wlan_quality = node->wlan_quality;
   strcpy(p->SSID,node->SSID);
   break;
  }
  p = p->next;
 }
 return head; 
}

/* 查找链表节点 */
PNODE find_node(PNODE head, PNODE node)
{
 PNODE p = NULL, pre = NULL;
 p = head;
// printf("node addr = %x\n",node);
 while(NULL != p)
 {
//  printf("node->cell_id = %d\np->cell_id = %d\n",node->cell_id, p->cell_id);
  if(p->cell_id == node->cell_id)
  {
   printf("FOUND NODE\n");
   break;
  }
  p = p->next;
 }
 if(NULL == p)
  printf("Can't FOUND NODE\n");
 return p;
}

void main0(int argc, char * argv[])
{
 PNODE head = NULL, node = NULL;
 
 /* 创建链表 */
 printf("Test create_list() function\n");
 head = create_list(head);
 printf("head = %x\n",head);
 print_list(head);

 ///* 插入链表节点 */
 printf("Test insert_node() function!\n");
 head = insert_node(head, node);
 print_list(head);

 /* 删除链表节点 */
 printf("Test delete_node() function\n");
 node = node_init(node);
 head = delete_node(head,node);
 print_list(head);

 /* 修改链表节点 */
 printf("Test change_node() function\n");
 node = node_init(node);
 head = change_node(head,node);
 print_list(head);

 /* 查找链表节点 */
 printf("Test found_node() function\n");
 node = node_init(node);
 node = find_node(head,node);
 print_list(node);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值