数据结构——单链表

单链表及其ADT

void deleteList(); //删除整个链表
List makeEmpty(List l);//清空链表,只留头结点
int isEmpty(List l);//判断是否为空
int isLast(List l, Position p);//判断是否是最后一个结点
List createList();//创建一个链表
void insert(List l, Node *n, Element e);//插入
Position find(List l, Element e);//寻找元素e
Position delete(List l, Element e);//删除元素e
void reverse(List l);//链表逆置
void print(List l);//打印链表
#include <stdio.h>
#include <stdlib.h>
typedef char Element;
typedef struct Node {
	Element value;
	struct Node* next;
} Node, *List, *Position;
/*
ADT
*/
void deleteList();
List makeEmpty(List l);
int isEmpty(List l);
int isLast(List l, Position p);
List createList();
void insert(List l, Node *n, Element e);
Position find(List l, Element e);
Position delete(List l, Element e);
void reverse(List l);
void print(List l);

//delete the list
void deleteList(List l) {
	if (l == NULL)
		return;

	Position p, tmp;
	p = l->next;
	l->next = NULL;

	while (p) {
		tmp = p->next;
		free(p);
		p = tmp;
	}
}
//empty the list
List makeEmpty(List l) {
	deleteList(l);
	l->next = NULL;
	return l;
}
//judge the is empty?
int isEmpty(List l) {
	return l->next == NULL;
}

//isLast
int isLast(List l, Position p) {
	return p->next == NULL;
}

//create a list
List createList() {
	Node *l;
	l = (Node *) malloc(sizeof(Node)); //create a header
	Node *r;
	r = l;
	char c;
	while (scanf("%c", &c) != EOF) {
		Node *n = (Node *) malloc(sizeof(Node));
		n->value = c;
		r->next = n; //get the node to the list
		r = n; //direct the next node
	}
	r->next = NULL;
	return l;
}

//find the Node's value == e,else return NULL
Position find(List l, Element e) {
	List tmp = l->next;
	while (tmp) {
		if (tmp->value == e)
			return tmp;
		tmp = tmp->next;
	}
	return NULL;
}

//delete a node's value equals e and return it;else return null
Position delete(List l, Element e) {
	Position p, tmp = NULL;
	tmp = l->next;
	while (tmp) {
		if (tmp->value == e) {
			p = tmp;
			return p;
		}
		tmp = tmp->next;
	}
	return p;
}

// insert a node which value is e in the back of Node n
void insert(List l, Node *n, Element e) {
	Node* tmp;
	tmp = (Node*) malloc(sizeof(Node));
	if (tmp == NULL) {
		printf("Error!!!");
		return;
	}
	tmp->value = e;
	tmp->next = n->next;
	n->next = n;
}

//reverse the List remember it ,very important!
void reverse(List l) {
	if(l == NULL || l->next == NULL)return;
	Position pcur, pre, pnext;
	pre = NULL;
	pcur = l->next; //direct the head's next node
	pnext = NULL;
	while (pcur) {
		pnext = pcur->next; //record the next node
		pcur->next = pre; //direct the pre node
		pre = pcur; //pre node direct completed subList
		pcur = pnext; //completing the next node
	}
	l->next = pre;
}
//print the list
void print(List l){
	if(l == NULL || l->next == NULL)return;
	Position tmp = l->next;
	while(tmp){
		printf("%c", tmp->value);
		tmp = tmp->next;
	}
}

//test
int main(int argc, char **argv) {
	List l = createList();
	reverse(l);
	print(l);

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值