您好,欢迎光临本网站![请登录][注册会员]  
文件名称: c语言链表示例
  所属分类: C/C++
  开发工具:
  文件大小: 3kb
  下载次数: 0
  上传时间: 2014-02-02
  提 供 者: u0135*****
 详细说明: #include #include #include #include struct Node { char name[20]; int score; struct Node next; }; 定义一个结构体 此结构体存储学生姓名 成绩以及指向该结构体的指针 typedef struct Node ListNode; ListNode CreateList int n ; 创建链表函数 返回创建的链表的头指针 void InsertList ListNode h int i char name[] int score ; 插入结点函数 void DeleteList ListNode h int i int n ; 删除结点函数 void PrintList ListNode h ; 打印链表函数 int main { ListNode h; int i 1 n score; char name[20]; wh ile i { printf "1 Create a new list n" ; printf "2 Add a new student"s information n" ; printf "3 Delete a student"s information n" ; printf "4 All student"s information n" ; printf "0 Quit n" ; scanf "%d" &i ; switch i { case 1: printf "Enter the number of students n n " ; scanf "%d" &n ; h CreateList n ; 创建链表 h是头指针 printf "List elements is: n" ; PrintList h ; break; case 2: printf "input the position of insert element:" ; scanf "%d" &i ; if i<1||i>n+1 控制条件 { printf "Error input n" ; break; } else { printf "input name of the student:" ; scanf "%s" name ; printf "input score of the student:" ; scanf "%d" &score ; InsertList h i name score ; printf "List elements is: n" ; PrintList h ; break; } case 3: printf "input the position of delete element:" ; scanf "%d" &i ; DeleteList h i n ; 调用删除结点函数 printf "list element is: n" ; PrintList h ; break; case 4: printf "list element is: n" ; PrintList h ; break; case 0: return; break; default: printf "illegal input " ; } } return 0; } ListNode CreateList int n { ListNode head; 定义头指针 ListNode p pre; int i; head ListNode malloc sizeof ListNode ; 给头结点分配空间 head >next NULL; 头结点的指针域为空 pre head; for i 1;i< n;i++ { printf "input name of the %d student:" i ; p ListNode malloc sizeof ListNode ; 创建一个新的结点 指针p指向它 scanf "%s" &p >name ; 给数据域赋值 printf "input score of the %d student:" i ; scanf "%d" &p >score ; 给数据域赋值 pre >next p; pre p; } p >next NULL; 最后一个结点指针域为空 return head; } void PrintList ListNode h { ListNode p; p h >next; p指向首元结点 while p { printf "%s %d" p >name p >score ; p p >next; 依次遍历 每访问完一个结点p指向下一个结点 printf " n" ; } } void InsertList ListNode h int i char name[] int e { ListNode q p; int j 0; p h; for j 0;jnext; } q ListNode malloc sizeof ListNode ; 创建个新结点 q指向它 strcpy q >name name ; q >score e; q >next p >next; q指向的结点的指针域指向第i个结点 p >next q; 第i 1个结点指向第i个结点 } void DeleteList ListNode h int i int n { ListNode p q; int j; char name[10]; int score; if i<1||i>n printf "illegal input n" ; else { j 0; p h; for j 0;jnext; } q p >next; q指向第i个结点 p >next q >next; 第i 1个结点的指针域指向第i+1个 strcpy name q >name ; score q >score; free q ; printf "name %s score %d n" name score ; 把删除的结点的数据域输出 } }">#include #include #include #include struct Node { char name[20]; int score; struct Node next; }; 定义一个结构体 此结构体存储学生姓名 成绩以及指向该结构体的指针 typedef struct Node ListNode; ListN [更多] ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: c语言 链表
 输入关键字,在本站1000多万海量源码库中尽情搜索: