双向链表与文件输入输出流(C语言)

文件输入输出

#include<stdio.h>
#include<string.h>

void split(char* buff) {
	if (buff == NULL)
		return;
	char* token;

	//获取第一个子字符串
	token = strtok(buff, "=");

	//继续获取其他的子字符串
	while (token != NULL) {
		
		printf("%s\n", token);
		token = strtok(NULL, "=");
	}
}

int main() {
	//char writebuff1[20], writebuff2[20], writebuff3[10], writebuff4[20], writebuff5[20];
	char writebuff[40], readbuff[40];
	FILE* fp = NULL;
	fp = fopen("C:\\Users\\15333\\Desktop\\rw.txt", "w+");
	printf("请根据提示,写入保存到文件中的内容!\n");

	printf("ppp_name=");
	scanf("%s", writebuff);
	fprintf(fp, "ppp_name=%s\n", writebuff);

	printf("ppp_passwd=");
	scanf("%s", writebuff);
	fprintf(fp, "ppp_passwd=%s\n", writebuff);

	printf("time_out=");
	scanf("%s", writebuff);
	fprintf(fp, "time_out=%s\n", writebuff);

	printf("dns=");
	scanf("%s", writebuff);
	fprintf(fp, "dns=%s\n", writebuff);

	printf("ip=");
	scanf("%s", writebuff);
	fprintf(fp, "ip=%s\n", writebuff);
	rewind(fp);

	while (fgets(readbuff, 40, fp) != NULL) {
		split(readbuff);
	}

	fclose(fp);
	return 0;


}

双向链表

#include <stdio.h>
#include <string.h>
#include <stdlib.h>




typedef struct line {
	struct line* pre;
	int data;
	struct line* next;
}line;

line* initline(line* head);
line* insertline(line* head, int data, int add);
line* delline(line* head, int data);
void display(line* head);
line* changeline(line* head, int data, int newdata);


int main() {
	line* head = NULL;
	head = initline(head);
	display(head);

	head = insertline(head, 4, 4);
	display(head);

	head = delline(head, 4);
	display(head);

	head = changeline(head, 3, 5);
	display(head);

	return 0;
}


//创建双向链表并初始化
line* initline(line* head) 
{
	//初始化头部
	head = (line*)malloc(sizeof(line));
	head->pre = NULL;
	head->next = NULL;
	head->data = 1;
	line* list = head;
	for (int i = 2; i <= 3; i++) {
		line* body = (line*)malloc(sizeof(line));
		body->pre = NULL;
		body->next = NULL;
		body->data = i;

		list->next = body;   //前驱节点的next指针指向新节点
		body->pre = list;
		list = body;
	}
	return head;

}

//双向链表中插入节点
line* insertline(line* head, int data, int add) {
	line* temp = (line*)malloc(sizeof(line));
	temp->data = data;
	temp->pre = NULL;
	temp->next = NULL;

	//插入节点为头节点
	if (add == 1) {
		temp->next = head;
		head->pre = temp;
		temp = head;
	}
	else {
		line* body = head;
		for (int i = 2; i < add; i++) {
			body = body->next;
		}
		//尾节点插入
		if (body->next == NULL) {
			body->next = temp;
			temp->pre = body;
		}
		//中间节点插入
		else {
			body->next->pre = temp;
			temp->next = body->next;
			body->next = temp;
			temp->pre = body;
		}
	}
	return head;

}

//双向链表删除节点
line* delline(line* head, int data) {
	line* temp = head;
	while (temp) {
		
		if (temp->data == data) {
			//尾节点删除
			if (temp->next == NULL) {
				temp->pre->next = NULL;
				free(temp);
				return head;
			}
			//中间节点删除
			temp->pre->next = temp->next;
			temp->next->pre = temp->pre;
			free(temp);
			return head;
		}
		temp = temp->next;
	}
	printf("链表中无该数据元素");
	return head;
}


void display(line* head) {
	line* temp = head;
	while (temp) {
		if (temp->next == NULL) {
			printf("%d\n", temp->data);
		}
		else {
			printf("%d->", temp->data);
		}
		temp = temp->next;
	}
}


line* changeline(line* head, int data, int newdata) {
	line* temp = head;
	while (temp) {
		if (temp->data == data) {
			temp->data = newdata;
			return head;
		}
		temp = temp->next;
	}
	printf("链表中无该数据元素");
	return head;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值