6.C/C++数据结构

1.单链表的各种操作

#include<iostream>
using namespace std;
//链表节点的定义
typedef struct node
{
	int data;  //节点内容
	node *next;
}node;


//创建单链表
node *create()
{
	int i = 0;   //链表中数据的个数
	node *head, *p, *q=NULL;
	int x = 0;
	head = (node*)malloc(sizeof(node)); //创建头节点

	while (1)
	{
		cout << "Please input the data: " << endl;
		cin >> x;
		if (x == 0)  //data=0时结束创建
		{
			break;
		}
		p = (node*)malloc(sizeof(node));
		p->data = x;
		if (++i==1)
		{
			head->next = p;
		}
		else
		{
			q->next = p;
		}
		q = p;
 	}
	q->next = NULL;
	return head;
}

void print(node*head)
{
	node* p;
	int index = 0;
	if (head->next==NULL)
	{
		cout << "The Link is empty" << endl;
		return;
	}
	p = head->next;
	while (p!=NULL)
	{
		cout << "The " << ++index << "th node is:" << p->data << endl;
		p = p->next;
	}
}

//单链表的测长
int length(node* head)
{
	int len = 0;
	node* p;
	p = head->next;
	while (p!=NULL)
	{
		len++;
		p = p->next;
	}
	return len;
}


//查找单链表pos位置上的节点
node* search(node* head, int pos)
{
	node* p = head;
	if (pos < 0)
	{
		cout << "error pos" << endl;
		return NULL;
	}
	if (pos == 0)
	{
		return head;
	}
	if (head->next == NULL)
	{
		cout << "the link is empty" << endl;
		return NULL;
	}
	for (int i = 1; i <= pos; i++)
	{
		p = p->next;
		if (p == NULL)
		{
			cout << "no pos search" << endl;
			break;
		}
	}
	return p;
}

//单链表的插入
node *insert(node* head, int pos, int data)
{
	node* temp = NULL;
	node *p = NULL;
	temp = (node*)malloc(sizeof(node));
	temp->data = data;
	if (pos == 0)
	{
		temp->next = head->next;
		head->next = temp;
	}
	p = search(head, pos);
	if (p != NULL)
	{
		temp->next = p->next;
		p->next = temp;
	}
	return head;
}

//删除单链表的pos位置的节点,返回链表头指针
node* delete_node(node* head, int pos)
{
	node *item = NULL;
	node *p = head->next;
	if (p == NULL)
	{
		cout << "Link is empty " << endl;
		return NULL;
	}
	p = search(head, pos - 1);
	if (p != NULL && p->next != NULL)
	{
		item = p->next;
		p->next = item->next;
		delete item;
	}
	return head;
}

//实现一个点链表的逆置
node* reverse(node *head)
{ 
	node *p, *q, *r;
	if (head->next == NULL)
	{
		return head;
	}
	p = head->next;
	q = p->next;
	p->next = NULL;
//	r = NULL;
	while (q!=NULL)
	{
		r = q->next;
		q->next = p;
		p = q;
		 q=r;
	}
	head->next = p;
	return head;
}
//也可以用栈来实现转置
node* rever1(node*head)
{
    stack<node*> stack1;
    stack<node*> stack2;

    if (head->next == NULL)
    {
        return head;
    }
    node*p;
    node *q=NULL;
    p = head->next;
    while (p!=NULL)
    {
        stack1.push(p);
        p = p->next;
    }

    q = head->next;
    while (stack1.empty()!=NULL)
    {
        q = stack1.top();
        stack1.pop();
        q = q->next;
    }
    return head;
}




//寻找单链表的中间元素
node *middle(node* head)
{
	int i = 0;
	int j = 0;
	node *current = NULL;
	node *middle = NULL;
	current = middle = head->next;
	while (current!=NULL)
	{
		if (i / 2 > j)
		{
			j++;
			middle = middle->next;
		}
		i++;
		current = current->next;
	}
	return middle;
}

//实现两个有序单链表的合并
node* Merge(node* head1, node* head2)
{
    node* head = NULL;
    if (head1 == NULL)
    {
        return head2;
    }
    if (head2 == NULL)
    {
        return head1;
    }

    if (head1->data < head2->data)
    {
        head = head1;
        head->next = Merge(head1->next, head2);
    }
    else
    {
        head = head2;
        head->next = Merge(head1, head2->next);
    }

    return head;
}



int main()
{
	node *head = create();
	cout << "Ok" << endl;
	cout << "The link's length is: " << length(head) << endl;
	print(head);
	int pos = 0;
	cout << "input add the pos :" << endl;
	cin >> pos;
	int x = 0;
	cout << "input add data:" << endl;
	cin >> x;
	head = insert(head, pos, x);
	print(head);

	cout << "input the delete pos:" << endl;
	cin >> pos;
	head = delete_node(head, pos);
	print(head);

	cout << "reverse link:" << endl;
	head = reverse(head);
    print(head);
    cout << "the middle is: " << middle(head)->data << endl;

	return 0;
}

Please input the data:
1
Please input the data:
2
Please input the data:
3
Please input the data:
0
Ok
The link's length is: 3
The 1th node is:1
The 2th node is:2
The 3th node is:3
input add the pos :
3
input add data:
4
The 1th node is:1
The 2th node is:2
The 3th node is:3
The 4th node is:4
input the delete pos:
4
The 1th node is:1
The 2th node is:2
The 3th node is:3
reverse link:
The 1th node is:3
The 2th node is:2
The 3th node is:1
the middle is: 2



2.双向链表的各种操作

//2.双向链表

#include<iostream>
using namespace std;
//双向链表的定义
typedef struct DbNode
{
	int data;   //节点数据
	DbNode* left; //前驱节点指针
	DbNode* right; //后继节点指针
}DbNode;

//创建链表函数
DbNode *CreateList(int data)
{
	DbNode* pnode = (DbNode*)malloc(sizeof(DbNode));
	pnode->data = data;
	pnode->left = pnode->right = NULL;  //前驱和后继指针都指向自身

	return pnode;
}

//插入新节点,总是在表尾插入,返回表头节点
DbNode *AppendNode(DbNode *head, int data)
{
	DbNode *node = CreateList(data);
	DbNode *p = head, *q=NULL;
	while (p!=NULL)
	{
		q = p;
		p = p->right;
	}
	q->right = node;
	node->left = q;

	return head;
}

//获取双向链表的长度
int GetLength(DbNode* head)
{
	int count = 1;
	DbNode* pnode = NULL;
	if (head == NULL)
	{
		return 0;
	}
	pnode = head->right;
	while (pnode!=NULL)
	{
		pnode = pnode->right;
		count++;
	}
	return count;
}

//打印双向链表
void PrintList(DbNode* head)
{
	DbNode* pnode = NULL;
	int i = 0;
	if (head == NULL)
	{
		cout << "The link is empty" << endl;
		return;
	}
	pnode = head;
	while (pnode!=NULL)
	{
		cout << "the " << ++i << "th is: " << pnode->data << endl;
		pnode = pnode->right;
	}
}

//双向链表节点的查找
//找到数据为data的节点,如果找到节点,返回节点
DbNode *FindNode(DbNode* head, int data)
{
	DbNode* pnode = head;
	if (head == NULL)
	{
		return NULL;
	}
	while (pnode->right!=NULL && pnode->data!=data) //找到数据或者达到链表结尾的时候退出循环
	{
		pnode = pnode->right;
	}

	if (pnode->right == NULL)  //还没找到data就已经到达链表结尾了
	{
		return NULL;
	}
	return pnode;
}

//双向链表的节点插入
void InsertNode(DbNode*head, int pos, int data)
{
	DbNode *newnode = CreateList(data);
	DbNode *p = head;
	for (int i = 1; i <=pos; i++)
	{
		p = p->right;
		if (p == NULL)
		{
			cout << "the position is no" << endl;
			return;
		}
	}
	newnode->right = p->right;
	p->right = newnode;
	newnode->left = p;

}
//删除节点
void DeleteNode(DbNode* head, int pos)
{
	DbNode *p;
	p = head;
	for (int i = 1; i <= pos; i++)
	{
		p = p->right;
		if (p == NULL)
		{
			cout << "the position is no" << endl;
			return;
		}
	}
	DbNode *ppre = p->left;
	DbNode *pafter = p->right;
	ppre->right = pafter;
	pafter->left = ppre;
	free(p);
}


int main()
{
	DbNode *head = CreateList(0);
	for (int i = 1; i < 10; i++)
	{
		head = AppendNode(head, i);
	}
	cout << "the length of link is: " << GetLength(head) << endl;
	PrintList(head);
	int pos, data=999;
	cout << "input the position of add: " << endl;
	cin >> pos;
	InsertNode(head, pos-1, data);
	PrintList(head);
	cout << "input the position of deltet:" << endl;
	cin >> pos;
	DeleteNode(head, pos-1);
	PrintList(head);
	return 0;
}

the length of link is: 10
the 1th is: 0
the 2th is: 1
the 3th is: 2
the 4th is: 3
the 5th is: 4
the 6th is: 5
the 7th is: 6
the 8th is: 7
the 9th is: 8
the 10th is: 9
input the position of add:
9
the 1th is: 0
the 2th is: 1
the 3th is: 2
the 4th is: 3
the 5th is: 4
the 6th is: 5
the 7th is: 6
the 8th is: 7
the 9th is: 8
the 10th is: 999
the 11th is: 9
input the position of deltet:
2
the 1th is: 0
the 2th is: 2
the 3th is: 3
the 4th is: 4
the 5th is: 5
the 6th is: 6
the 7th is: 7
the 8th is: 8
the 9th is: 999
the 10th is: 9




3.队列的操作

//3.队列的操作

#include<iostream>
using namespace std;
//队列中的每个节点的定义
typedef struct node
{
	int data;
	node * next;  //指向链表的下一个指针
}node;

//定义队列
typedef struct MyQuene
{
	node* front;  //队头
	node* rear;   //队尾
}MyQuene;

//构造空的队列
MyQuene *CreateMyQueue()
{
	MyQuene *q = (MyQuene*)malloc(sizeof(MyQuene));
	q->front = NULL; //把队首指针置空
	q->rear = NULL;  //把队尾指针置空
	return q;
}

//入队,从队尾一端插入节点
MyQuene *enqueue(MyQuene* q, int data)
{
	node* newP = NULL;
	newP = (node*)malloc(sizeof(node)); //新建节点
	newP->data = data;
	newP->next = NULL;
	if (q->rear == NULL)  //如果队列为空
	{  //如果队列为空,则新节点即是队首又是对尾
		q->front = q->rear = newP;
	}
	else
	{
		//如果队列不为空,则新建节点放到队尾,队尾指针指向新的节点
		q->rear->next = newP;
		q->rear = newP;  //队尾进行更新
	}
	return q;
}

//出队,从队头删除节点
MyQuene* dequeue(MyQuene *q)
{
	node *pnode = NULL;
	pnode = q->front;
	if (pnode == NULL)
	{
		cout << "the queue is empyt" << endl;
	}
	else
	{
		q->front = q->front->next; //新队头
		if (q->front == NULL) //当删除后队列如果为空,对rear置空
		{
			q->rear = NULL;
		}
		free(pnode);
	}
	return q;
}

//队列的测长
int GetLength(MyQuene *q)
{
	int nLen = 0;
	node* pnode = q->front;  //指向队头
	if (pnode != NULL) //队列不为空
	{
		nLen = 1;
	}
	while (pnode!=q->rear)  //遍历队列
	{
		pnode = pnode->next;
		nLen++;
	}
	return nLen;
}

void PrintMyQueue(MyQuene *q)
{
	int i = 0;
	node* pnode = q->front;
	if (pnode == NULL)
	{
		cout << "Queue is empyt!" << endl;
		return;
	}
	while (pnode!=q->rear)
	{
		cout << "the " << ++i << "th is: " << pnode->data << endl;
		pnode = pnode->next;
	}
	cout << "the " << ++i << "th is: " << q->rear->data << endl; //队尾节点数据
}

int main()
{
	MyQuene *hp = CreateMyQueue();
	enqueue(hp, 1);
	enqueue(hp, 2);
	enqueue(hp, 3);
	enqueue(hp, 4);
	PrintMyQueue(hp);
	cout << "the length of que is:" << GetLength(hp) << endl;
	cout << "out of data after:" << endl;
	hp = dequeue(hp);
	PrintMyQueue(hp);
	cout << "the length of que is:" << GetLength(hp) << endl;
	return 0;
}

the 1th is: 1
the 2th is: 2
the 3th is: 3
the 4th is: 4
the length of que is:4
out of data after:
the 1th is: 2
the 2th is: 3
the 3th is: 4
the length of que is:3




4.队列和栈有什么区别

队列和栈是两种不同的数据结构。它们有以下区别

(1)操作的名称不同。队列的插入称为入队,队列的删除称为出对。栈的插入称为进栈,栈的删除称为出栈。

(2)可操作的方向不同。队列是在对尾入队,队头出队,即两边都可操作。而栈的进栈和出栈都是在栈顶进行的,无法对栈底直接进行操作。

(3)操作的方法不同。对列是先进先出(FIFO),即队列的修改是依先进先出的原则进行的。新来的成员总是加入队尾(不能中间插入),每次离开的成员总是队列头上的(不允许中途离队)。而栈是后进先出(LIFO),即每次删除(出栈)的总是当前栈中“最新的”元素,即最后插入(进栈)的元素,而最先插入的被放在栈的底部,要到最后才能删除。




5.栈的操作

//5.栈的操作
#include<iostream>
using namespace std;
class MyData   //节点类
{
public:
	MyData():data(0),next(NULL){}
	MyData(int value):data(value),next(NULL){}
	int data;
	MyData *next;
};

class MyStack   //栈类
{
public:
	MyStack():top(NULL){}
	void push(int data);  //进栈
	void pop(); //出栈
	bool IsEmpty();          //判断是否为空栈
	MyData *top;
};

//进栈
void MyStack::push(int data)
{
	MyData *pData = NULL;
	pData = new MyData(data);
	pData->next = top;
	top = pData;
}

//出栈
void MyStack::pop()
{
	MyData *pData = NULL;
	if (IsEmpty())
	{
		return;
	}
	pData = top->next;
	cout << "the pop :" << top->data << endl;
	top = pData;
}

//判断是否为空栈
bool MyStack::IsEmpty()
{
	return (top == NULL);
}
int main()
{
	MyStack s;
	s.push(1);
	s.push(2);
	s.push(3);

	s.pop();
	s.pop();
	cout << "Empty = " << s.IsEmpty() << endl;
	return 0;
}

the pop :3
the pop :2
Empty = 0



原创文章 100 获赞 339 访问量 56万+

猜你喜欢

转载自blog.csdn.net/MATLAB_matlab/article/details/72858197
今日推荐