[C]C语言基础巩固专题 链表之(链表反转)
链表是c语言中很重要的数据结构,是考察一个程序员的基本功的手段,之前在一家公司面试时就问到了
实现一个链表的反转,当时就是没有很好冷静的思考,今天在这里写出来,共勉!
基本算法:
1. 判断是否为空,如果为空,返回NULL
2. 否则说明至少有一个节点,那么
p2指向最后一个节点,p1指向前一个节点,
把p2指向的节点的next 置为NULL(因为这个点将是链表的末尾节点)
3. 进入循环
1)让前一个节点和后一个节点重新建立连接;
2)P2 往前移,p2=p1;
3)P1 往前移,p1=p1->next。
后两步是为下一个循环做准备。
4. 当p1为空时,p2指向的是最后一个节点,那么返回p2.
基本代码如下:
struct Node
{
int num;
Node * next;
};
Node * reverse(Node * head)
{
if(head==NULL)
{
return NULL;
}
Node * p1,p2;
p2=head;
p1=head->next;
p2->next=NULL;
while(p1)
{
p1->next=p2;
p2=p1;
p1=p1->next;
}
return p2;
}
相关文档:
http://docs.google.com/View?docid=ajbgz6fp3pjh_2dwwwwt#_38239340844832237
It is not about optimization.
The whole idea of using 'do/while' version
is to make a macro which will
expand into a regular statement, not into a
compound statement. This is
done in order to make the use of function-s ......
GNU C的一大特色(却不被初学者所知)就是__attribute__机制。__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)
和类型属性(Type Attribute)。
__attribute__书写特征是:__attribute__前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的__attribute__参数。
__at ......
最近想写个控制台下的进度条,可以知道程序的进展情况,不用弄个界面。其中最主要的就是“\b“字符,它的ascii码值是10,是退格的意思。
现把代码贴上,如下(可以在vc和linux编译):
progress.c
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#define mysleep(n) Sleep(n*1000)
......
1. 下载SQLitewindows版
我们可以从下列网站下载sqlite的windows版。
http://www.sqlite.com.cn/bbs/topicdisp.asp?tid=182&topage=1#gotolast
下载这个三个文件:
SQLite 3.3.7 下载
windows版
sqlite-3_3_7.zip 这个是SQLite的windows可执行文件
sqlitedll-3_3_7.zip 这个 ......