文章目录
再“C”一点
MY BLOG: 优快云
链表
静态链表的创建
#include <stdio.h>
struct Text
{
int data;
struct Text *pNext;
};
struct Text *head=NULL;
struct Text t1={1,NULL};
struct Text t2={2,NULL};
struct Text t3={3,NULL};
struct Text t4={4,NULL};
int main()
{
head=&t1;
t1.pNext=&t2;
t2.pNext=&t3;
t3.pNext=&t4;
return 0;
}
打印链表数据
函数:
void printLink(struct Text *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pNext;
}
putchar('\n');
}
调用:
printLink(&t1);
从指定结点后方插入新结点
函数:
int insertFromBehind(struct Text *p,int data,struct Text *newdata)
{
while(p!=NULL)
{
if(p->data==data)
{
newdata->pNext=p->pNext;
p->pNext=newdata;
return 1;
}
p=p->pNext;
}
printf("Not Found %d!\n",data);
return 0;
}
调用:
struct Text t5={5,NULL};
insertFromBehind(&t1,2,&t5);
从指定结点前方插入新结点
函数:
struct Text* insertFromBefor(struct Text *head,int data,struct Text *newdata)
{
struct Text *p=head;
if(data==p->data)
{
newdata->pNext=p;
return newdata;
}
while(p->pNext!=NULL)
{
if(p->pNext->data==data)
{
newdata->pNext=p->pNext;
p->pNext=newdata;
printf("success insert %d before %d!\n",p->pNext->data,data);
//printf("success insert %d before %d!\n",newdata->data,data);
return head;
}
p=p->pNext;
}
printf("Not find %d in the chain!\n",data);
return head;
}
调用
struct Text t6={6,NULL};
insertFromBefor(&t1,3,&t6);
总结1
#include <stdio.h>
struct Text
{
int data;
struct Text *pNext;
};
struct Text *head=NULL;
struct Text t1={1,NULL};
struct Text t2={2,NULL};
struct Text t3={3,NULL};
struct Text t4={4,NULL};
struct Text t5={5,NULL};
struct Text t6={6,NULL};
void printLink(struct Text *p);
int insertFromBehind(struct Text *p,int date,struct Text *newdata);
struct Text* insertFromBefor(struct Text *head,int data,struct Text *newdata);
int main()
{
head=&t1;
t1.pNext=&t2;
t2.pNext=&t3;
t3.pNext=&t4;
printLink(&t1);
insertFromBehind(&t1,2,&t5);
printLink(&t1);
insertFromBefor(&t1,5,&t6);
printLink(&t1);
return 0;
}
void printLink(struct Text *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pNext;
}
putchar('\n');
}
int insertFromBehind(struct Text *p,int data,struct Text *newdata)
{
while(p!=NULL)
{
if(p->data==data)
{
newdata->pNext=p->pNext;
p->pNext=newdata;
return 1;
}
p=p->pNext;
}
printf("Not Found %d!\n",data);
return 0;
}
struct Text* insertFromBefor(struct Text *head,int data,struct Text *newdata)
{
struct Text *p=head;
if(data==p->data)
{
newdata->pNext=p;
return newdata;
}
while(p->pNext!=NULL)
{
if(p->pNext->data==data)
{
newdata->pNext=p->pNext;
p->pNext=newdata;
printf("success insert %d before %d!\n",p->pNext->data,data);
//printf("success insert %d before %d!\n",newdata->data,data);
return head;
}
p=p->pNext;
}
printf("Not find %d in the chain!\n",data);
return head;
}
动态链表的创建
头插法
struct Text* headInsert(struct Text *head,struct Text *new)
{
if(head==NULL) //若传入链表头结点为NULL(链表为空)
{
return new;
}
else
{
new->pNext=head;
head=new;
return head;
}
}
尾插法
struct Text* tailInsert(struct Text *head,struct Text *new)
{
if(head==NULL) //若传入链表头结点为NULL(链表为空)
{
return new;
}
else
{
struct Text *p=head;
while(p-pNext!=NULL)
{
p=p->pNext; //往后找,直至链尾
}
p->pNext=new;
return new;
new->pNext=head;
head=new;
return head;
}
}
创建结点
函数:
struct Node* createNode(int data)
{
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->date=data;
newNode->next=NULL;
return newNode;
}
调用:
int main()
{
struct Text *newNode=createNode(3);
printLink(newNode);
return 0;
}
删除结点
struct Text* delNode(struct Text* head,int data)
{
struct Text *p=head;
if(p->data==data)
{
head=p->pNext;
free(p); //释放原头节点内存
return head;
}
while(p->pNext!=NULL)
{
if(p->pNext->data==data)
{
p->pNext=p->pNext->pNext;
struct Text *del_p=p->pNext;
free(del_p); //释放删除掉的结点的内存
return head;
}
p=p->pNext;
}
printf("Failed del %d\n",data);
return head;
}
总结2
#include <stdio.h>
#include <stdlib.h>
struct Text
{
int data;
struct Text *pNext;
};
void printLink(struct Text *p);
struct Text* createNode(int data);
int insertFromBehind(struct Text *p,int data,struct Text *newdata);
struct Text* insertFromBefore(struct Text *p,int data,struct Text *newdata);
struct Text* delNode(struct Text* head,int data);
struct Text* headInsert(struct Text *head,struct Text *new);
struct Text* tailInsert(struct Text *head,struct Text *new);
int main()
{
struct Text *newNode=createNode(3); //动态创建结点
printLink(newNode); //打印当前链表
//newNode=delNode(newNode,1); //尝试删除结点
//printLink(newNode);
//newNode=delNode(newNode,3); //删除数据为3的结点
//printLink(newNode);
//struct Text *newNode2=createNode(5);
//insertFromBehind(newNode,3,newNode2);
//printLink(newNode);
//struct Text *newNode3=createNode(6);
//newNode=insertFromBefore(newNode,3,newNode3);
//printLink(newNode);
struct Text *newNode4=malloc(sizeof(struct Text));
newNode=headInsert(newNode,newNode4);
printLink(newNode);
struct Text *newNode5=malloc(sizeof(struct Text));
newNode=tailInsert(newNode,newNode5);
printLink(newNode);
//newNode=headInsert(newNode,newNode3);
//printLink(newNode);
free(newNode); //释放链表内存
newNode=NULL; //指向NULL
printLink(newNode);
return 0;
}
void printLink(struct Text *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pNext;
}
putchar('\n');
}
int insertFromBehind(struct Text *p,int data,struct Text *newdata)
{
while(p!=NULL)
{
if(p->data==data)
{
newdata->pNext=p->pNext;
p->pNext=newdata;
return 1;
}
p=p->pNext;
}
printf("Not Found %d!\n",data);
return 0;
}
struct Text* insertFromBefore(struct Text *head,int data,struct Text *newdata)
{
struct Text *p=head;
if(data==p->data)
{
newdata->pNext=p;
return newdata;
}
while(p->pNext!=NULL)
{
if(p->pNext->data==data)
{
newdata->pNext=p->pNext;
p->pNext=newdata;
printf("success insert %d before %d!\n",p->pNext->data,data);
//printf("success insert %d before %d!\n",newdata->data,data);
return head;
}
p=p->pNext;
}
printf("Not find %d in the chain!\n",data);
return head;
}
struct Text* createNode(int data)
{
struct Text *newNode=(struct Text*)malloc(sizeof(struct Text));
if (newNode == NULL)
{
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data=data;
newNode->pNext=NULL;
return newNode;
}
struct Text* delNode(struct Text* head,int data)
{
struct Text *p=head;
if(p->data==data)
{
head=p->pNext;
free(p); //释放原头节点内存
return head;
}
while(p->pNext!=NULL)
{
if(p->pNext->data==data)
{
p->pNext=p->pNext->pNext;
struct Text *del_p=p->pNext;
free(del_p); //释放删除掉的结点的内存
return head;
}
p=p->pNext;
}
printf("Failed del %d\n",data);
return head;
}
struct Text* headInsert(struct Text *head,struct Text *new)
{
if(head==NULL) //若传入链表头结点为NULL(链表为空)
{
return new;
}
else
{
new->pNext=head;
head=new;
return head;
}
}
struct Text* tailInsert(struct Text *head,struct Text *new)
{
if(head==NULL) //若传入链表头结点为NULL(链表为空)
{
return new;
}
else
{
struct Text *p=head;
while(p->pNext!=NULL)
{
p=p->pNext; //往后找,直至链尾
}
p->pNext=new;
return head;
}
}
结合函数指针动态创建链表
#include <stdio.h>
#include <stdlib.h>
typedef struct Text
{
int data;
struct Text *pNext;
}text;
void printLink(struct Text *p);
struct Text* headInsert(struct Text *head,struct Text *newNode);
struct Text* tailInsert(struct Text *head,struct Text *newNode);
struct Text* activeCreatNode(struct Text *head,struct Text *(*func)(struct Text *head,struct Text *newNode));
int main()
{
text *head=NULL;
text newdata={2,NULL};
printLink(head);
head=activeCreatNode(head,tailInsert);
printLink(head);
head=activeCreatNode(head,headInsert);
printLink(head);
return 0;
}
void printLink(struct Text *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pNext;
}
putchar('\n');
}
struct Text* headInsert(struct Text *head,struct Text *newNode)
{
if(head==NULL) //若传入链表头结点为NULL(链表为空)
{
return newNode;
}
else
{
newNode->pNext=head;
head=newNode;
return head;
}
}
struct Text* tailInsert(struct Text *head,struct Text *newNode)
{
if(head==NULL) //若传入链表头结点为NULL(链表为空)
{
return newNode;
}
else
{
struct Text *p=head;
while(p->pNext!=NULL)
{
p=p->pNext; //往后找,直至链尾
}
p->pNext=newNode;
return head;
}
}
struct Text* activeCreatNode(struct Text *head,struct Text *(*func)(struct Text *head,struct Text *newNode))
{
while(1)
{
struct Text *newNode=(struct Text*)malloc(sizeof(struct Text));
printf("Please input newdata:");
scanf("%d",&(newNode->data));
if(newNode->data==0)
{
free(newNode);
printf("end input\n");
return head;
}
head=(*func)(head,newNode);
}
}
函数指针
调用方式
#include <stdio.h>
void PrintWelcom(void)
{
printf("哈喽,welcome\n");
}
int main()
{
PrintWelcom(); //直接调用函数
void (*func)(void)=PrintWelcom; //定义一个函数指针来调用
(*func)();
return 0;
}
实际案例
#include <stdio.h>
#include <stdlib.h>
int GetMax(int a,int b)
{
return a>b?a:b;
}
int GetMin(int a,int b)
{
return a<b?a:b;
}
int GetSum(int a,int b)
{
return a+b;
}
int dataHandler(int a,int b,int (*pfunc)(int a,int b))
{
int data;
data=(*pfunc)(a,b);
}
int main()
{
int a=10;
int b=-20;
int cmd;
int ret;
// int (*pfunc)(int a,int b);
int (*pfunc)(int,int); //省略形参的名字
printf("请输入一个数字.1:取最大值、2:取最小值、3:取两数之和\n");
scanf("%d",&cmd);
switch(cmd)
{
case 1:
printf("最大值为:");
pfunc=GetMax;
break;
case 2:
printf("最小值为:");
pfunc=GetMin;
break;
case 3:
printf("和为:");
pfunc=GetSum;
break;
default:
printf("输入错误");
exit(-1);
}
ret=dataHandler(a,b,pfunc);
printf("ret=%d\n",ret);
return 0;
}
文件操作基础
打开和关闭文件
定义文件指针
FILE *fp;
打开文件fopen()
"r"
:只读,文件必须存在,光标位于文件开头
"w"
:只写,如果文件不存在则创建,如果文件存在则截断,光标位于文件开头
"a"
:追加,如果文件不存在则创建,光标位于文件末尾
"r+"
、"w+"
、"a+"
:读写,文件必须存在
fp=fopne("test.txt","r"); //双引号
if(fp==NULL)
printf("文件打开失败\n");
关闭文件fclose()
fclose(fp);
测试
#include <stdio.h>
int main()
{
FILE *fp;
fp=fopen("test.c","r");
if(fp==NULL)
printf("Open failed!\n");
else
printf("Open succeed!\n");
fclose(fp);
return 0;
}
读取文件内容
测试fgetc()
(fread()
)
#include <stdio.h>
int main()
{
FILE *fp;
fp=fopen("test.c","r");
if(fp==NULL)
printf("Open failed!\n");
else
printf("Open succeed!\n");
char c;
while((c=fgetc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
return 0;
}
写入文件内容
测试fprintf()
(fwrite()
)
#include <stdio.h>
int main()
{
FILE *fp;
fp=fopen("filetest.txt","w");
if(fp==NULL)
printf("Open failed!\n");
else
printf("Open succeed!\n");
fprintf(fp,"Hello");
char c;
while((c=fgetc(fp))!=EOF)
printf("%c",c);
printf("\n",c);
fclose(fp);
return 0;
}
光标定位
测试:移动光标fseek()
,获取光标位置ftell()
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
fp=fopen("filetest.txt","w+");
if(fp==NULL)
{
printf("Open failed!\n");
exit(-1);
}
else
{
printf("Open succeed!\n");
}
fprintf(fp,"Hello,World!");
/*偏移量0:光标相对于起始位置的偏移量
**起始位置SEEK_END:文件末尾
** SEEK_SET: 文件开头
** SEEK_CUR: 当前位置
*/
fseek(fp,6,SEEK_SET);
char c;
while((c=fgetc(fp))!=EOF)
printf("%c",c);
putchar('\n');
printf("光标位置:%ld\n",ftell(fp));
fclose(fp);
return 0;
}
微信公众号
关注博主微信公众号:灵萧宝殿
分享更多有趣有料内容
点此扫码