易截截图软件、单文件、免安装、纯绿色、仅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实现一个泛型函数。郁闷了好久用C++模板实现了。
宏有如下的特点:
1.与const相比,宏是在预编译的时候完成的
2.define 只做简单的替换,不做类型安全检查
3.使用不当会引起很多问题
宏的用法:
1.简单的宏定义
#define  MAX 1024
2.宏定义功能块
#define MAX(a,b) ( ......

c socket 发送http请求

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(){
int sockfd;
int len;
struct sockaddr_in address;
int result;
char *strings="GET /svnup/rewrite.php HTTP/1 ......

C/C++ 打开PDF文档

 wchar_t wsWorkingDir[256] = _T("");
 CString strPDF(_T(""));
 ::GetCurrentDirectory(256, wsWorkingDir);
 strPDF.Format(_T("%s"), wsWorkingDir);
 if (strPDF.GetAt(strPDF.GetLength()-1) != '\\')
 {
  strPDF += '\\';
 }
 strPDF += "doc\\my.pdf" ......

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';      
    ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号