码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构——单链表

时间:2014-05-25 07:18:35      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:数据结构   单链表   插入   删除   逆置   

单链表及其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;
}



数据结构——单链表,布布扣,bubuko.com

数据结构——单链表

标签:数据结构   单链表   插入   删除   逆置   

原文地址:http://blog.csdn.net/dafeng_blog/article/details/26849023

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!