线性表之单循环链表

//sList
#include <stdio.h>
#include <stdlib.h>


//===============状态量====================
#define OK 1
#define ERROR 0

//===================ADT 循环单链表 =============== 
typedef int ElemType;
typedef struct CLNode{
	ElemType data;
	struct CLNode *next;	
}CListNode;
typedef CListNode *CLinkList;

//================================模块定义============================== 
//内置申请节点函数 
static CLinkList newNode(){
	CLinkList p = (CLinkList)malloc(sizeof(CListNode));
	if(p != NULL)
		p->next = NULL;
	return p; 
}

//初始化
void InitList(CLinkList *L){
	(*L) = newNode();
	if(*L == NULL){
		fprintf(stderr, "初始化失败!\n");
		exit(EXIT_FAILURE);
	}
	(*L)->next = (*L); 
} 

//销毁表
void DestroyList(CLinkList L){
	CLinkList p = L;
	CLinkList q = p->next;
	while(q != L){
		free(p);
		p = q;
		q = p->next;
	}
	free(p);
} 

//判断表空
int ListEmpty(CLinkList L){
	return (L->next == L);
} 

//求表长
int ListLength(CLinkList L){
	CLinkList p = L;
	int i = 0;
	while(p->next != L){
		i++;
		p = p->next;
	}
	return i;
} 

//输出表
void DispList(CLinkList L){
	CLinkList p = L->next;
	while(p != L){
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
} 

//求下标为 i 的元素
int GetElem(CLinkList L, int i, ElemType *e){
	int j = 0;
	CLinkList p = L;
	while(j < i && p->next != L){
		p = p->next;
	}
	if(p == NULL)
		return ERROR;
	else{
		*e = p->data;
		return OK;
	}
} 

//求元素的下标
int LocateElem(CLinkList L, ElemType e){
	int j = 0;
	CLinkList p = L;
	while(p->next != L && p->data != e){
		j++;
		p = p->next;
	} 
	if(p == L)
		return ERROR;
	else
		return j;
} 

//插入元素
int InsertList(CLinkList L, int i, ElemType e){
	CLinkList p = L;
	CLinkList pNew = NULL;
	int j = 0;
	
	if(p->next == L || i == 1){                                 //原链表为空表或者 i = 1时 
		pNew = newNode();
		pNew->data = e;
		pNew->next = p->next;                                    //因p的指针域会改变所以先连接后面
		p->next = pNew;
		return OK; 
	} 
	else{
		p = L->next;
		while(j < i - 2 && p != L){                     //查找前驱节点 
			j++;
			p = p->next;
		}
		if(p == L)                                           //i 输入错误 
 			return ERROR;
		else{
			pNew = newNode();
			pNew->data = e;
			pNew->next = p->next;
			p->next = pNew;
			return OK;
		} 
	}
} 
//删除元素
int DeleteList(CLinkList L, int i, ElemType *e){
	int j = 0;
	CLinkList p = L;
	CLinkList q = NULL;
	
	if(p->next == L){                                             //为空表时
		return ERROR;
	}
	else{
		if(i == 1){                                         
			q = p->next;
			*e = q->data;
			p->next = q->next;
			free(q);
			return OK; 
		}//if
		else{
			p = L->next;
			while(j < i - 2 && p != L){
				j++;
				p = p->next;
			}
			if(p == L)
				return ERROR;
			else{
				q = p->next;
				*e = q->data;
				p->next = q->next;
				free(q);
				return OK;
			}
		}//else
	}//else
} 

阅读更多

单循环链表

05-16

/*题目:假设在长度大于1的单循环链表中,既无头结点也无头指针。S为指向链表中某个结点rn的指针,试编写算法删除结点*s 的直接前驱结点。*/ 貌似我连题目也没有读懂,就胡乱写了一个!高手帮忙解释一下!rn下面的程序是算法问题,帮忙修正!或者按题目要求另写一个!谢谢~rn[code=C/C++]rn#includern#includern#includernrn#define OK 1rn#define OVERFLOW -2rn#define ERROR 0rnrntypedef int ElemType;rntypedef int Status;rnrntypedef struct LNodern ElemType data;rn struct LNode *next;rn LNode,*LinkList;rn///创建链表 rnStatus CreateList(LinkList &L,int n)rnrn int i;rn L=(LinkList)malloc(sizeof(LNode));rn LinkList L1=L;rn if(!L)exit(ERROR);rn rn L1->next=NULL;rn printf("Please input numbers:\n");rn for(i=0;idata);rn if(i==n-1)rn L1->next=L;//L1->next=NULL;rn elsern rn LinkList p=(LinkList)malloc(sizeof(LNode));rn L1->next=p;rn L1=p; rn rn rn return OK; rn rn///输出元素 rnStatus PrintList(LinkList L,int m)rnrn int i=1; rn printf("The numbers are:");rn while(L)rn rn printf("%d ",L->data);rn if(i>=m) break; rn if(L->next)rn L=L->next;i++; rn elsern break;rn rn printf("\n"); rn return OK; rnrn ///删除结点*s 的直接前驱结点rnStatus DeleteList(LinkList &L,LinkList s)rnrn //int locat=0; rn LinkList p,q;//=(LinkList)malloc(sizeof(LNode));rn p=s;rn //LinkList s=(LinkList)malloc(sizeof(LNode));rn //s->data=e; rn while(p->next->next!=s)rn rn q=p; rn p=p->next;rn free(s); rn return OK; rn rn q->next=s;rn free(q);rn return OK; rn rnrn//主函数 rn int main()rn rn int n,slocat;rn ElemType e; rn LinkList L,s;rn printf("Please input the number you need:"); rn scanf("%d",&n); rn CreateList(L,n); rn PrintList(L,n);rn printf("请输入结点S的位置:");rn scanf("%d",&slocat);rn DeleteList(L,s);rn n=n-1; rn PrintList(L,n);rn system("pause"); rn rn[/code]rnrn

没有更多推荐了,返回首页