链表操作

来源:互联网 发布:淘宝买东西一直在揽件 编辑:程序博客网 时间:2024/06/16 18:20

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 安装软件不兼容怎么办 win10驱动不尖锐怎么办 苹果内存太小怎么办 内存太小怎么办手机 苹果手机屏幕不能滑动怎么办 vivo手机软件不兼容怎么办 微信无法录音怎么办 手机卡住了怎么办vivo 好钱包闪退怎么办 闲鱼认证失败怎么办 闲鱼买了假门票怎么办 买黄金买到假的怎么办 闲鱼被买家骗了怎么办 闲鱼上小视频没法保存怎么办 qq空间无法查看怎么办 华为手机电池不耐用怎么办 内内被动过怎么办 hp电脑开机黑屏怎么办 网上开店快递费怎么办 保温杯外壳掉漆怎么办 拖鞋前面磨脚怎么办 塑料拖鞋磨脚怎么办 路由器进不去设置界面怎么办 手机号丢了微信登不上怎么办 电脑总是闪黑屏怎么办 支付宝破产钱怎么办 淘宝号码注册过怎么办 农行k宝怎么办信用卡 电脑页面无法显示怎么办 对方银行停止收款怎么办 淘宝东西买太多怎么办 淘宝号黑号了怎么办… 中通包裹异常怎么办 包裹退回去了怎么办 qq支付密码错误怎么办 ie8出现闪退怎么办 平板输不了密码怎么办 华硕笔记本键盘打不开怎么办 电脑打不开rar文件怎么办 苹果手机淘宝卡怎么办 淘宝联盟网址打不开怎么办