C++顺序表、链表的*插删查等*操作

V2!!

顺序表

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//顺序表三要素
typedef struct Vector{
	int *data;
	int size, length;
}Vec;

Vec *init(int n){
	Vec *v = (Vec *)malloc(sizeof(Vec));
	v->data = (int *) malloc (sizeof(int) * n);
	v->size = n;
	v->length = 0;
	return v;
}

void clear(Vec *v){
	if (v == NULL) return;
	free(v->data);//malloc与free成对出现
	free(v);//先销毁data再销毁v
	return;
}

//malloc在堆区上动态开辟空间,不一定清零,calloc可以清空为固定值,
//realloc可以在已开辟空间上再一次开辟,realloc先在原来空间上看能不能扩,可以则返回原地址,不能则返回新地址,并主动拷贝原来数据,并释放原来空间,失败返回0
int expand(Vec *v){
	int ex_size = v->size;
	int *q;
	while(ex_size){
		q = (int *)realloc(v->data, sizeof(int) * (v->size +ex_size));
		if (q) break;
		ex_size /= 2;
	}
	if (ex_size == 0) return 0;
	v->data = q;
	v->size += ex_size;
	return 1;

}

//0失败1成功
int insert(Vec *v, int val, int ind){
	//3种意外情况
	if (v == NULL) return 0;
	if (ind < 0 || ind > v->length) return 0;
	if (v->length >= v->size) {
		if (!expand) return 0;
		printf("success to expand!\n");
	}
		//return 0;

	for (int i = v->length; i>ind; i++){
		v->data[i] = v->data[i - 1];
	}
	v->data[ind] = val;
	v->length++;
	return 1;
}

int erase(Vec *v, int ind){
	if (v == NULL) return 0;
	if (ind < 0 || ind >= v->length) return 0;
	for (int i = ind + 1; i < v->length; i++){
		v->data[i - 1] = v->data[i];
	}
	v->length--;
	return 1;
}

void output(Vec *v){
	if (v == NULL) return;
		return ;
	printf("VECTOR: [");
	for (int i = 0; i < v->length; i++){
		i && printf(", ");
		printf("%d", v->data[i]);
	}
	printf(" ]\n");
	return ;
}


int main(){
	srand(time(0));
#define max_op 20
	Vec *v = init(max_op);
	for (int i = 0; i <= max_op; i++){
		int val = rand() % 100;
		int op = 0;//0insert 1erase
		if (insert(v, val, i)){
			printf("insert %d success!\n", val);
		}
	}
#undef max_op

	return 0;
}

单链表

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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

typedef struct List{
	ListNode head;
	int length;
}List;

ListNode *init_node(int val){
	ListNode *p = (ListNode *)malloc(sizeof(ListNode));
	p->data = val;
	p->next = NULL;
	return p;
}

//head为虚拟头结点,数据字段无意义,只代表链表头
List *init_list(){
	List *l = (List *)malloc(sizeof(List));
	l->head.next = NULL;
	l->length = 0;
	return l;
}

void clear_node(ListNode *node){
	if (node == NULL) return;
	free(node);
	return;
}

//不断地用q标识p下一个节点,然后销毁p
void clear(List *l){
	if (l == NULL) return;
	ListNode *p = l->head.next, *q;
	while(p){
		q = p->next;
		clear_node(p);
		p = q;
	}
}

//2种特殊情况,p始终标识待插入的前一个位置,如果ind=0,p就是头结点(第1个元素前)
int insert(List *l, int ind, int val){
	if (l == NULL) return 0;
	if (ind < 0 || ind > l->length) return 0;
	ListNode *p = &(l->head), *node = init_node(val);
	while(ind --) p = p->next;
	node->next = p->next;
	p->next = node;
	l->length++;
	return 1;
}

//同理,q标识待删除节点,p标识待删除的前一节点
int erase(List *l, int ind){
	if (l == NULL) return 0;
	if (ind < 0 || ind >= l->length) return 0;
	ListNode *p = &(l->head), *q;
	while(ind --) p = p->next;
	q = p->next;
	p->next = q->next;
	free(q);
	l->length--;
	return 1;
}

void output(List *l){
	if (l == NULL) return ;
	printf("List(%d) is [", l->length);
	for (ListNode *p = l->head.next; p; p = p->next){
		printf("%d->", p->data);
	}
	printf("NULL]\n");
	return ;
}

int main(){
	srand(time(0));
#define max_op 20
	List *l = init_list();
	for (int i = 0; i < max_op;i++){
		int val = rand() % 100;
		if(insert(l, i, val)) printf("insert %d success!\n", val);
	}
}

顺序表

#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
//定义顺序表模板类
template <typename Type> class Vector {
private:
    int size, length;
    Type *data;
public:
    Vector(int input_size) {
        size = input_size;
        length = 0;
        data = new Type[size];
    }
    ~Vector() {
        delete[] data;
    }
    //插入
    bool insert(int loc, Type value) {
        if (loc < 0 || loc > length) {
            return false;
        }
        if (length >= size) {
            expand();
        }
        for (int i = length; i > loc; --i) {
            data[i] = data[i - 1];
        }
        data[loc] = value;
        length++;
        return true;
    }
    //扩容
    void expand() {
        Type *old_data = data;
        size = size * 2;
        data = new Type[size];
        for (int i = 0; i < length; ++i) {
            data[i] = old_data[i];
        }
        delete[] old_data;
    }
    //查找
    int search(const Type &value) {
        for (int i = 0; i < length; i++){
            if (data[i] == value){
                return i;
            }
        }
        return -1;
    }
    bool remove(int index) {
        if (index < 0 || index >= length) {
            return false;
        }
        for (int i = index + 1; i < length; ++i) {
            data[i - 1] = data[i];
        }
        length = length - 1;
        return true;
    }
    void print() {
        for (int i = 0; i < length; i++){
            if (i > 0){
                cout << " ";
            }
            cout << data[i];
        }
        cout << endl;
    }
};
int main() {
    Vector<int> a(100);
    cout << a.insert(1, 0) << endl;
    cout << a.insert(0, 1) << endl;
    cout << a.insert(2, 1) << endl;
    cout << a.insert(1, 2) << endl;
    cout << a.insert(0, 3) << endl;
    cout << a.search(1) << endl;
    cout << a.search(4) << endl;
    return 0;
}

链表

#include <iostream>
#include<iostream>
using namespace std;
//结点模板类
template <typename Type> class Node {
public:
    Type data;
    Node<Type> *next;
    Node(const Type &_data) {
        data = _data;
        next = NULL;
    }
};
//链表模板类
template <typename Type> class LinkedList {
private:
    Node<Type> *head;
public:
    LinkedList() {
        head = NULL;
    }
    ~LinkedList() {
        Node<Type> *current_node = head;
        while (current_node != NULL) {
            Node<Type> *delete_node = current_node;
            current_node = current_node->next;
            delete delete_node;
        }
    }
    //插入
    bool insert(Node<Type> *node, int index) {
        if (head == NULL) {
            if (index != 0) {
                return false;
            }
            head = node;
            return true;
        }
        if (index == 0) {
            node->next = head;
            head = node;
            return true;
        }
        Node<Type> *current_node = head;
        int count = 0;
        while (current_node->next != NULL && count < index - 1) {
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1) {
            node->next = current_node->next;
            current_node->next = node;
            return true;
        }
        return false;
    }
    //遍历
    void output() {
        if (head == NULL) {
            return;
        }
        Node<Type> *current_node = head;
        while (current_node != NULL) {
            cout << current_node->data << " ";
            current_node = current_node->next;
        }
        cout << endl;
    }
    //删除
    int delete_node(int index) {
        if (head == NULL){
            return 0;
        }
        Node<Type> *current_node = head;
        int count = 0;
        if (index == 0){
            head = head->next;
            delete current_node;
            return 1;
        }
        while(current_node->next != NULL && count < index - 1){
            count ++;
            current_node = current_node->next;
        }
        if (current_node->next == NULL || count > index - 1){
            return 0;
        }else if(count == index - 1){
            Node<Type> *delete_node = current_node->next;
            current_node->next = delete_node->next;
            delete delete_node;
            return 1;
        }
    }
    //翻转
    void reverse() {
        if (head == NULL){
            return;
        }
        Node<Type> *current_node;
        Node<Type> *next_node;
        current_node = head->next;
        head->next = NULL;
        while(current_node != NULL){
            next_node = current_node->next;
            current_node->next = head;
            head = current_node;
            current_node = next_node;
        }
        return;
    }
};
int main() {
    LinkedList<int> linkedlist;
    int num;
    while(scanf("%d", &num) != EOF){
       if(num == 1){
           int loc, data;
                scanf("%d%d", &loc, &data);
                Node<int> *node = new Node<int>(data);
                if(linkedlist.insert(node, loc)){
                    cout << "success\n";
                }else{
                    cout << "failed\n";
                }
                
       }else if(num == 2){
           linkedlist.output();
       }else if(num == 3){
           int loc;
                scanf("%d", &loc);
                if(linkedlist.delete_node(loc)){
                    cout << "success\n";
                }else{
                    cout << "failed\n";
                }
       }else if(num == 4){
           linkedlist.reverse();
       }
    }
    return 0;
}

单向循环链表

#include<iostream>
using std::cin;
template <typename Type> class Node {
public:
    Type data;
    Node<Type> *next;
    Node(const Type &_data) {
        data = _data;
        next = NULL;
    }
};
template <typename Type> class LinkedList {
private:
    Node<Type> *head;
public:
    LinkedList() {
        head = NULL;
    }
    ~LinkedList() {
        if (head == NULL){
            return;
        }
        Node<Type> *current_node = head->next;
        head->next = NULL;
        while (current_node != NULL) {
            Node<Type> *delete_node = current_node;
            current_node = current_node->next;
            delete delete_node;
        }
    }
    void insert(Node<Type> *node, int index) {
        if (head == NULL) {
            if (index != 0) {
                return;
            }
            head = node;
            head->next = head;
            return;
        }
        if (index == 0) {
            node->next = head->next;
            head->next = node;
            return;
        }
        Node<Type> *current_node = head->next;
        int count = 0;
        while (current_node != head && count < index - 1) {
            current_node = current_node->next;
            count++;
        }
        
        if (count == index - 1) {
            node->next = current_node->next;
            current_node->next = node;
        }
        if (node == head->next){
            head = node;
        }
    }
};
int main() {
    LinkedList<int> linkedlist;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++){
        Node<int> *node = new Node<int>(i);
        linkedlist.insert(node, i - 1);
    }
    return 0;
}

双向循环链表凭序号翻转

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
template <typename Type> class Node {
public:
    Type data;
    Node<Type> *next;
    Node<Type> *prior;
    Node(const Type &_data) {
        data = _data;
        next = NULL;
    }
};
template <typename Type> class LinkedList {
private:
    Node<Type> *head;
public:
    LinkedList() {
        head = NULL;
    }
    ~LinkedList() {
        if (head == NULL){
            return;
        }
        Node<Type> *current_node = head->next;
        head->next = NULL;
        while (current_node != NULL) {
            Node<Type> *delete_node = current_node;
            current_node = current_node->next;
            delete delete_node;
        }
    }
    void insert(Node<Type> *node, int index) {
        if (head == NULL) {
            if (index != 0) {
                return;
            }
            head = node;
            head->next = head;
            head->prior = head;
            return;
        }
        // if (index == 0) {
        //     node->next = head->next;
        //     head->next = node;
        //     return;
        // }
        Node<Type> *current_node = head->next;
        int count = 0;
        while (current_node != head && count < index - 1) {
            current_node = current_node->next;
            count++;
        }
        
        if (count == index - 1) {
            node->next = current_node->next;
            current_node->next->prior = node;
            current_node->next = node;
            node->prior = current_node;
        }
        if (node == head->next){
            head = node;
        }
    }
    void output() {
        if (head == NULL) {
            return;
        }
        Node<Type> *current_node = head->next;
        while (current_node->next != head) {
            cout << current_node->data << " ";
            current_node = current_node->next;
        }
        cout << head->data;
        cout << endl;
    }
    //反向输出方法
    void reverse_out(int index){
        int count = 0;
        Node<Type> *current_node = head->next;
        while (current_node != head && count < index - 1) {
            current_node = current_node->next;
            count++;
        }
        Node<Type> *start_node = current_node->next;//正确起始节点的前一节点
        //current_node = current_node->next;
        if (count == index - 1){
            while(current_node != start_node){
                cout << current_node->data << " ";
                current_node = current_node->prior;
            }
            cout << start_node->data;
            cout << endl;
        }
    }
};

int main(){
    LinkedList<int> linkedlist;
    int num, data;
    scanf("%d", &num);
    for (int i = 0; i < num; i++){
        scanf("%d", &data);
        Node<int> *node = new Node<int>(data);
        linkedlist.insert(node, i);
    }
    int start_num;
    scanf("%d", &start_num);
    linkedlist.reverse_out(start_num);
    //linkedlist.output();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值