易截截图软件、单文件、免安装、纯绿色、仅160KB

链表的基本操作(c实现)

  
链表定义及操作的源文件:employee.h
//
#pragma once
#ifndef __EMPLOYEE_H__
#define __EMPLOYEE_H__
#include<stdio.h>
#include<malloc.h>
typedef struct employee{
int id;
int age;
int salary;
}EmpType;
typedef struct Node{
EmpType data;
struct Node *next;
}LNode,*PNode ,*LinkList;
typedef LinkList EmpLinkList;
//初始化链表
int InitEmpLinkList(LinkList *h)
{
*h=(LinkList)malloc(sizeof(LNode));
if (!h) {printf("init failed!\n");return 0;}
(*h)->next=NULL;
return 1;
}
//向链表中pos中的位置插入数据data
int InsertEmpLinkList(LinkList h,int pos,EmpType data)
{
int i=0;
PNode p=h,q;
while (p&&i<pos-1)
{
p=p->next;
i++;
}
if (!p||i>pos-1) {printf("the insert position is illegal!\n");return 0;}
q=(PNode)malloc(sizeof(LNode));
if (!q) {printf("can not create new node !\n");return 0;}
q->data=data;
q->next=p->next;
p->next=q;
return 1;
}
//打印链表中age为某值的结点
int PrintByAge(LinkList h,int age,PNode *pnode)
{
PNode p=h->next;
while (p&&p->data.age!=age)
p=p->next;
if (!p) {printf("can not find the age!\n");return 0;}
(*pnode)=p;
printf("%d,%d,%d",(*pnode)->data.id,(*pnode)->data.age,(*pnode)->data.salary);
printf("\n");
return 1;
}
//返回某id的结点
PNode FindNodeById(LinkList h,int id)
{
PNode p=h->next;
while (p&&p->data.id!=id)
p=p->next;
return p;
}
//返回某id的位置
int GetPos(LinkList h,int id)
{
PNode p=h->next;
int i=0;
while (p&&p->data.id!=id)
{
p=p->next;
i++;
}
if (!p)
return 0;
return i;
}
//遍历链表
void TraverLinkList(LinkList h)
{
PNode p=h->next;
while (p)
{
printf("%d,%d,%d",p->data.id,p->data.age,p->data.salary);
printf("\n");
p=p->next;
}
}
#endif
主函数(测试)main.c
#include"employee.h"
int main(int argv,char * argc[])
{
EmpLinkList l; //定义链表
EmpType data; //定义链


相关文档:

[收藏]C/C++数组名与指针区别深层探索

根本原因在与左值和右值
char a[10]=“hello”;
sizeof(a);//数组名做左值,具有数组名的属性,是一个指向数组首地址的常量指针
strcpy(a,"abc");//数组名做右值,退化为普通的指针
原文链接:
 http://hi.baidu.com/%D2%C0%BD%A3%D0%F9/blog/item/7bbf36966c92f36a54fb9663.html
作者:宋宝华 e ......

第六章答案 c primer plus

 6.1 编写一个程序,创建一个具有26个元素的数组,并在其中存储26个小写字母,并让该程序显示该数组的内容.
#include <stdio.h>
int main(void)
{
 char a[26] = {'a', 'b', 'c', 'd', 'e', 'f',
     'g', 'h', 'i', 'j', 'k', 'l',
     'm', 'n', 'o', 'p ......

C/C++数组名与指针区别

 以下文章转载于:为了学习和收藏
http://tech.163.com/school · 2005-08-23 11:05:18 · 来源: 天极网
C/C++数组名与指针区别
  引言
  指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用。于是乎,很多程序设计者就被搞糊涂了。而许多的大学老师,他们 ......

C中宏#和##的运用

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define f(a,b) a##b
#define g(a)  #a
#define h(a) g(a)
int main()   
{   
    char a = 'a';      
    ......

C题目2

预处理器(Preprocessor)
1. 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题)
#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL
我在这想看到几件事情:
1). #define 语法的基本知识(例如:不能以分号结束,括号的使用,等等)
2). 懂得预处理器将为你计算常数表达式的值,因此,直接 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号