C语言:单链表 ---《大话数据结构》学习记录

源码:

#include                                                                                    
#include 
#include 
#include 

typedef unsigned char BYTE;
typedef unsigned int DWORD;
typedef unsigned short WORD;

#define pri(fmt, ...) 	printf("["__FILE__"] <%s>_<%d> " ,__FUNCTION__,__LINE__ );\
						printf(fmt, ##__VA_ARGS__);

						

typedef struct Node
{
	int data;
	struct Node *next;
}Node;

typedef struct Node *LinkList;
	

/* 创建一个链表 */
void CreateListTail(LinkList *L, int n)
{
	LinkList p;
	LinkList r;
	int i;
	srand(time(0));
	
	*L = (LinkList)malloc(sizeof(Node));
	r = *L;
	for(i=0;idata = rand()%100+1;
		r->next = p;
		r = p;
	}
	r->next = NULL;
}
/* 删除一个链表*/
int ClearList(LinkList *L)
{
	LinkList p;
	LinkList q;
	p=(*L)->next;
	while(p)
	{
		q=p->next;
		free(p);
		p=q;
	}
	(*L)->next = NULL;
	return 0;
}
	
/* 读取第i个元素 */
int GetElem(LinkList L, int i, int *e)
{
	int j;
	/* 声明一个节点 */
	LinkList p;
	/* 初始化 */
	p = L->next; //指向头结点
	j = 1;
	
	while(p && jnext; //指向下一个节点
		++j;
	}
	if(!p || j>i)
		return -1;
	*e = p->data;
	return 0;
}
	
/*在链表L中的i位置,插入元素e */
int ListInsert(LinkList *L, int i, int e)
{
	int j;
	LinkList p;
	LinkList s;
	p = *L;
	j = 1;
	while(p && jnext; //指向下一个节点
		++j;
	}
	if(!p || j>i)
		return -1;
	s = (LinkList)malloc(sizeof(Node));
	
	s->data = e;
	s->next = p->next;
	p->next = s;
	return 0;
}

/* 删除链表L的第i个数据元素,并返回其值e */
int ListDelete(LinkList *L, int i, int *e)
{
	int j;
	LinkList p;
	LinkList q;
	p = *L;
	j = 1;
	while(p && jnext; //指向下一个节点
		++j;
	}
	if(!p || j>i)
		return -1;
	
	q = p->next;
	p->next = q->next;
	
	*e = q->data;
	free(q);
	
	return 0;
	
}

/* 按照顺序打印链表L*/
void ListPrint(LinkList L)
{
	/* 声明一个节点 */
	LinkList p;
	p = L->next; //指向头结点
	pri("data : ");
	while(p)
	{
		printf("%d ", p->data);
		p = p->next; //指向下一个节点
	}
	printf("\n");
}


int main()
{
	int tmp;
	LinkList test;
	CreateListTail(&test, 5);
	ListPrint(test);
	
	ListDelete(&test, 2, &tmp);
	ListPrint(test);
	
	ListInsert(&test, 4, 6);
	ListPrint(test);
	
	GetElem(test,3,&tmp);
	pri("tmp : %d \n", tmp);

	ClearList(&test);
	return 0;
}


编译运行:

root@ubuntu:/mnt/hgfs/Ubuntu12-share/01_C/01_dhsjjg/list# gcc -o test test.c 
root@ubuntu:/mnt/hgfs/Ubuntu12-share/01_C/01_dhsjjg/list# 
root@ubuntu:/mnt/hgfs/Ubuntu12-share/01_C/01_dhsjjg/list# 
root@ubuntu:/mnt/hgfs/Ubuntu12-share/01_C/01_dhsjjg/list# ./test
[test.c] _<135> data : 95 24 3 57 35 
[test.c] _<135> data : 95 3 57 35 
[test.c] _<135> data : 95 3 57 6 35 
[test.c] 
_<159> tmp : 57 [test.c]
_<161> --- root@ubuntu:/mnt/hgfs/Ubuntu12-share/01_C/01_dhsjjg/list# ./test [test.c] _<135> data : 22 51 1 86 41 [test.c] _<135> data : 22 1 86 41 [test.c] _<135> data : 22 1 86 6 41 [test.c]
_<159> tmp : 86 [test.c]
_<161> --- root@ubuntu:/mnt/hgfs/Ubuntu12-share/01_C/01_dhsjjg/list# root@ubuntu:/mnt/hgfs/Ubuntu12-share/01_C/01_dhsjjg/list# ./test [test.c] _<135> data : 100 65 24 13 1 [test.c] _<135> data : 100 24 13 1 [test.c] _<135> data : 100 24 13 6 1 [test.c]
_<159> tmp : 13 [test.c]
_<161> --- root@ubuntu:/mnt/hgfs/Ubuntu12-share/01_C/01_dhsjjg/list#

你可能感兴趣的:(数据结构,C/C++)