c语言:单向循环链表的实现

// 单循环链表的实现.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#define LEN sizeof(struct SCriculist)
using namespace std;
typedef struct SCriculist  //定义结构体
{
	int data;                  //数据域
	struct SCriculist *next; //指针域
}SCriculist;

//函数声明
void Error(char *s);        //错误处理函数
SCriculist *Creat_SCriculist();//创建单向循环链表函数
void Insert_SCriculist(SCriculist & l, int i, int e);//向单向循环链表位置为i的地方插入元素e
void Delete_SCriculist(SCriculist & l, int i, int e);//从单向循环链表中删除位置为i的结点,并用e返回其数据域的值
SCriculist *Find_SCriculist(SCriculist & l, int i); //查找单向循环链表中的第i个结点
void Scarch_SCriculist(SCriculist & l, int e); //查找单向循环链表中的第一个数据域为e的结点,输出该节点在链表中的位置
int Length_SCriculist(SCriculist & l); //计算单向循环链表的长度
void Print_SCriculist(SCriculist & l);  //以此输出单向循环链表中的数据
//函数定义

void Error(char *s)
{
	cout << s << endl;
	exit(1);
}

SCriculist *Creat_SCriculist()
{
	int length = 0;
	int data = 0;
	SCriculist *head, *tail = NULL, *ptr = NULL;
	head = (struct SCriculist *)malloc(LEN);
	if (NULL == head)
		Error("内存分配失败!");
	head->data = 0;
	head->next = head;
	tail = head;
	cout << "请输入链表长度:";
	cin >> length;
	cout << "请输入链表数据:" << endl;
	for (int i = 0; i < length; i++)
	{
		ptr = (struct SCriculist *)malloc(LEN);
		if (NULL == ptr)
			Error("内存分配失败!");
		cin >> data;
		ptr->data = data;
		ptr->next = head;
		tail->next = ptr;
		tail = ptr;
	}
	return head;
}

SCriculist *Find_SCriculist(SCriculist & l, int i)
{
	SCriculist *prev = l.next;
	int j = 1;
	while ((prev->next != &l) && (j < i))
	{
		prev = prev->next;
		j++;
	}
	return prev;
}
void Insert_SCriculist(SCriculist & l, int i, int e)
{
	SCriculist *prev = Find_SCriculist(l, i);
	SCriculist *ptr = (struct SCriculist *)malloc(LEN);
	if (NULL == ptr)
		Error("内存分配失败!");
	ptr->data = e;
	ptr->next = prev->next;
	prev->next = ptr;
}

void Delete_SCriculist(SCriculist & l, int i, int e)
{
	SCriculist *prev = Find_SCriculist(l, i);
	e= prev->data;
	SCriculist *pt = prev;
	prev = prev->next;
	pt->next = prev->next;
	delete prev;
	cout << "你删除的元素是:" << e << endl;
}
void Scarch_SCriculist(SCriculist & l, int e)
{
	SCriculist *prev = &l;
	int k = 1;
	while ((prev != &l) && (prev->data != e))
	{
		prev = prev->next;
		k++;
	}
	cout << "该元素在链表中的位置是:" << k << endl;
}
int Length_SCriculist(SCriculist & l)
{
	SCriculist *prev = l.next;
	int length = 0;
	while (prev != &l)
	{
		prev = prev->next;
		length++;
	}
	return length;
}

void Print_SCriculist(SCriculist & l)
{
	SCriculist *prev = l.next;
	cout << "该链表的数据是:" << endl;
	while (prev != &l)
	{
		cout << prev->data << " ";
		prev = prev->next;
	}
	cout << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
	SCriculist *p;
	int n, m, t, e;
	int q = 0;
	p = Creat_SCriculist();
	cout << "该链表的长度为:" << Length_SCriculist(*p) << endl;
	Print_SCriculist(*p);
	cout << "请输入要插入的位置:";
	cin >> n;
	cout << "请输入要插入的元素:";
	cin >> m;
	Insert_SCriculist(*p, n, m);
	cout << "该链表的长度为:" << Length_SCriculist(*p) << endl;
	Print_SCriculist(*p);
	cout << "请输入要删除的位置:";
	cin >> t;
	Delete_SCriculist(*p, t, q);
	cout << "该链表的长度为:" << Length_SCriculist(*p) << endl;
	cout << "请输入要查找的元素:";
	cin >> e;
	Scarch_SCriculist(*p, e);
	return 0;
}

你可能感兴趣的:(数据结构与算法设计)