完整 的代码请到这里下载:
http://download.csdn.net/detail/conceptcon/5351679
#include
#include
/* 双链表结构体声明,注意链表结点创建参照了《C与指针》一书 */
typedef struct list{
int data;
struct list *forward; /* 指向前向结点 */
struct list *backward; /* 指向后向结点 */
}node;
/* 任意交换两个结点 交换指针 如果看不懂就多画图,多画几遍就行了 */
node *swap_node(node *head, node *this, node *next)
{
node *temp, *temp1;
if(!this->backward && !next->forward) {/* 首和尾巴的交换,需要改变根节点 */
if(this->forward == next) { /* 只有两个结点的情况下 */
//printf("进入!->1\n");
next->forward = this;
next->backward = NULL;
this->backward = next;
this->forward = NULL;
head->forward = next;
head->forward = next;
head->backward = this; /* OK */
} else { /* 有间隔的首尾交换 */
this->backward = next;
this->forward = NULL;
head->backward = this; /* OK */
} else { /* 和前面其他结点交换 */
//printf("进入!->4\n");
temp = next->backward;
temp->forward = this;
this->backward->forward = next;
this->forward->backward = next;
next-
next->backward = NULL;
head->forward = next; /* OK */
} else { /* 头结点和后面其他结点交换 */
//printf("进入!->6\n");
temp = this->forward;
temp->backward = next;
this->backward = next->backward;
this->forward = next->forward;
next->backward->forward = this;
next->forward->backward = this;
next->forward = temp;
next->backward = NULL; /* OK */
head->forward = next; /* 交换的结点成为头结点 */
}
}
}
return(head);
}
上文为部分核心部分代码,不全;完整代码请下载!!!
#define __KERNEL__ //这样才能使用list.h,具体请看list.h
#include "list.h" /*由于我的机器上没有list.h,所以我拷贝了一个,如果你机器上有,应该是加#include */
#include
#include
#define MAX_USER_LEN 32
#define MAX_PAS_LEN 32
#define MAX_SERVER_LEN 1024
typedef struct server_detect_ftp
{
struct list_head list;
char server[MAX_SERVER_LEN];
int port;
char username[MAX_USER_LEN];
char password[MAX_PAS_LEN];
}server_detect_ftp_t;
int main(void)
{
struct list_head head;//头部
server_detect_ftp_t ftp_link;
server_detect_ftp_t ftp_link1;
server_detect_ftp_t *entry;
struct list_head *p;
INIT_LIST_HEAD(&head);//初始化头部
strcpy(ftp_link.server,"www.163.com");
ftp_link.port=34;
strcpy(ftp_link.username,"good");
strcpy(ftp_link.password,"good");
strcpy(ftp_link1.server,"www.163.com");
ftp_link1.port=34;
strcpy(ftp_link1.username,"good");
strcpy(ftp_link1.password,"good");
INIT_LIST_HEAD(&head);
list_add(&ftp_link.list,&head);
list_add(&ftp_link1.list,&head);//添加链表
list_del(&ftp_link1.list);//删除链表
list_for_each(p,&head)//遍历
{
entry=list_entry(p,struct server_detect_ftp,list);//读取某个值
printf("%s\n",entry->username);
}
return 0;
}
而表达式:*&a 的求值顺序为先"&"后"*",即* (&a)=*p=a 。
为了叙述方便,假设系统给数组a分配的首地址为1000,如图7.4所示。
最后运算的结果为b=100,p=1004指向a[1]。
最后运算的结果为b=300,p=1008指向a[2]。
#include
using namespace std;
#include
#define MIN(A, B) ((A)>=(B)?(B):(A))
void fun()
{
int *p, bb, c;
bb = 25;
p = &c;
c = 20;
printf("MIN(A, B) = %3d\n", MIN(c, bb));
printf("MIN(A, B) = %3d\n", MIN(*p, bb));
printf("MIN(A, B) = %3d\n", MIN(*p++, bb));
printf("MIN(A, B) = %p\n", *p++ >= bb ? bb : *p++);
}
void funcc()
{
int a[4]={100,200,300,400},b;
int *p=&a[0];
cout<<'\t'<<"p="< 0)
{
printf("dsds\n");
printf("a+b=%d\n" , a+b);
printf("a+b=%p\n" , (void *)(a+b));
}
else
{
printf("ssss\n");
printf("a+b=%d\n" , a+b);
printf("a+b=%p\n" , (void *)(a+b));
}
fun();
funcc();
return 0;
}
dsds
a+b=-6
a+b=0xfffffffa
MIN(A, B) = 20
MIN(A, B) = 20
MIN(A, B) = 25
MIN(A, B) = 0xbff3cb74
p=0xbff3cb48
b=100 p=0xbff3cb4c
b=300 p=0xbff3cb50
b=300 p=0xbff3cb50
b=301 p=0xbff3cb54
b=401 p=0xbff3cb54