c++实现单循环链表的初始化,创建,增加和删除操作

//单循环链表的初始化,创建,增加和删除操作
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#define OK     1;
#define ERROR  0;
typedef int Status;
typedef int ElemType;
typedef struct Node{
	int data;
	struct Node *next;
}LinkList;
LinkList *head,*rear;//注意这里申明的是全局指针变量

LinkList *CreateList(){					//初始化链表并返回链表
	head = new LinkList();
	rear = new LinkList();
	rear = head;
	rear->next = head;
	return head;
}
LinkList *CreateList(int n){			//创建链表
    head = new LinkList();
    rear = new LinkList();

    LinkList *p,*q;//以p为要新创建元素要插入的前一个结点位置指针,q为要新创建元素要插入的结点位置指针

    cout << "enter the data of list:" << endl;
    scanf("%d",&head->data);
    rear = head;
    rear->next = head;//就是初始化的过程

    for(int i=2;i<=n;i++){
        p = new LinkList();
        scanf("%d",&p->data);
        p->next = rear->next;
        rear->next = p;
        rear = p;
    }
    return rear;


}
Status InsertList(LinkList *L,int i,ElemType e){		//链表的插入操作
	//步骤可分为,寻找i的位置,检查i的是否合理,插入三步,
	LinkList *p,*q;
	//i合理为1<i<length+1,不知道length,所以求length
	int length;
	length = 1;
	p = L->next;//使p指向第一个结点,

	while(p!=L){
		p = p->next;
		length++;
	}

	if(i<1 || i>length+1){
		cout << "position of i is error";
		return ERROR;
    }
	p = L;
	for(int j=1;j<i;j++)//寻找i的位置
		p = p->next;
	q = new LinkList();
	if(!q)
		exit(0);
	q->data = e;
	q->next = p->next;
	p->next = q;
	cout << "==============Insert data=================" << endl;
	return OK;
}

Status DeleteList(LinkList *L,int i){				//链表的删除操作
    LinkList *p,*q;
    //i合理为1<i<length+1,不知道length,所以求length
	int length;
	length = 0;
	p = L->next;//使p指向第一个结点,

	while(p!=L){
		p = p->next;
		length++;
	}
	if(i<1 || i>length){
		cout << "position of i is error";
		return ERROR;
    }

    p = L;
    for(int j=1;j<i;j++)
        p = p->next;
    q = p->next;
    cout << endl;
    cout << "====the delete data is:";
    cout << q->data;
    printf("\n");
    p->next = q->next;
    delete q;//实际上删除的是q指向的数据,即q->data
	cout << "==============Delete data=================" << endl;
    return OK;

}

void OutputList(LinkList *L){				//打印链表操作
	LinkList *p;
	p = L->next;//这里选择从尾结点的下一个结点开始
	cout << "==========the data of list is:============" << endl;
	while(p != L){

		cout << p->data << " ";
        p = p->next;
	}
	cout << p->data << " ";
	printf("\n");
}
int main(){
    head = CreateList(5);
    OutputList(head);
    InsertList(head,1,11);
    OutputList(head);
    DeleteList(head,3);
    OutputList(head);
	/*head = CreateList();
	InsertList(head,1,11);//在1位置上插入11
	InsertList(head,2,12);
	InsertList(head,3,13);
	OutputList(head);*/
	return 0;
}



运行结果如下:

enter the data of list:

输入:

enter the data of list:
1 2 3 4 5

得到结果如下:

enter the data of list:
1 2 3 4 5
==========the data of list is:============
1 2 3 4 5
==============Insert data=================
==========the data of list is:============
11 1 2 3 4 5

====the delete data is:2
==============Delete data=================
==========the data of list is:============
11 1 3 4 5

Process returned 0 (0x0)   execution time : 3.277 s
Press any key to continue.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值