初级数据结构

初级数据结构

顺序表

#include<iostream>
using namespace std;

#define MAX 100

typedef struct
{
	int data[MAX];//顺序表元素
	int length;//顺序表长度
}sqlist;


int initlist(sqlist *l)//初始化顺序表
{
	memset(l->data, 0, sizeof(l));
	l->length = 0;
	cout << "初始化成功" << endl;
	return 0;
}

int addlist(sqlist* l)//给顺序表添加数据
{
	if (l->length<0 || l->length>MAX)
	{
		cout << "顺序表出错" << endl;
		return 0;
	}
	else
	{
		for (int i = 0; i < 10; i++)
		{
			cin >> l->data[i];
			l->length++;
		}
		cout << "添加成功" << endl;
	}
}

void lenlist(sqlist* l)//求顺序表长度
{
	if (l == NULL)
	{
		cout << "不存在该顺序表" << endl;
	}
	else
	{
		cout << "顺序表长度为:" << l->length << endl;;
	}
}

int searchlist(sqlist *l)//查找元素
{
	int n;
	cout << "请输入要查找的元素n:";
	cin >> n;
	if (l->length<0 || l->length>MAX)
	{
		cout<<"不存在该表"<<endl;
		return 0;
	}
	else
	{
		for (int i = 0; i < MAX; i++)
		{
			if (l->data[i] == n)
			{
				cout<<"位置是"<<i+1<<endl;
				return i;
				break;
			}
		}
	}
}

int insertlist(sqlist* l)//i位置插入num插入元素
{
	int num,i;
	cout << "请输入要在i位置插入的元素num:";
	cin >>i>>num;
	if (l->length<0 || l->length>MAX)
	{
		cout << "不存在该顺序表" << endl;
		return 0;
	}
	else
	{
		for (int j = l->length; j >=i; j--)
		{
			l->data[j] = l->data[j-1];
		}
		l->data[i-1] = num;
		l->length++;
		cout<<"插入成功"<<endl;
	}
}

void dellist(sqlist* l)//删除元素
{
	int n;
	cout << "请输入要删除的元素n:";
	cin >> n;
	if (l->length<0 || l->length>MAX)
	{                                                                                                 
		cout << "不存在该顺序表" << endl;
	}
	else
	{
		for (int i = 0; i < l->length; i++)
		{
			if (i == l->length)
				cout<<"无此元素" << endl;
			if(n==l->data[i])
			{
				for (int j = i+1; j < l->length; j++)
				{
					l->data[j-1] = l->data[j];
				}
				l->length--;
				cout << "删除成功" << endl;
			}
		}
	}
}

void printlist(sqlist *l)//打印函数
{
	if (l->length<0 || l->length>MAX)
	{
		cout << "不存在该顺序表" << endl;
	}
	else
	{
		for (int i = 0; i < l->length; i++)
		{
			cout << i+1 << "位置:" << l->data[i] << endl;
		}
	}
}

void memu()
{
	cout << "1.初始化顺序表  2.添加顺序表数据" << endl;
	cout << "3.求顺序表长度  4.查找顺序表数据" << endl;
	cout << "5.插入数据      6.删除顺序表数据" << endl;
	cout << "            0.退出              " << endl;
}

int main()
{
	memu();
	int choice;
	sqlist l;
	while (1)
	{
		cout << "请输入选择";
		cin >> choice;
		switch (choice)
		{
		case 1:
			initlist(&l);
			printlist(&l);
			break;
		case 2:
			addlist(&l);
			printlist(&l);
			break;
		case 3:
			lenlist(&l);
			printlist(&l);
			break;
		case 4:
			searchlist(&l);
			printlist(&l);
			break;
		case 5:
			insertlist(&l);
			printlist(&l);
			break;
		case 6:
			dellist(&l);
			printlist(&l);
			break;
		case 0:
			exit(0);
		}
	}
}

链表

#include<iostream>
using namespace std;

typedef struct node
{
	int data;//数据域
	struct node* next;//指针域
}node,*linklist;

void initlist(linklist&l)//初始化单链表
{
	l = new node;
	if (l->next == NULL)
	{
		cout<<"初始化失败"<<endl;
	}
	else
	{
		l->next = NULL;
		cout<<"初始化成功"<<endl;
	}
}

void headinsertlist(linklist l)//头插法
{
	cout<<"请输入十个数据:"<<endl;
	for (int i = 0; i < 10; i++)
	{
		linklist p = new node;
		p->next = NULL;
		cin >> p->data;
		p->next = l->next;
		l->next = p;
	}
}

void rearinsertlist(linklist l)//尾插法
{
	linklist rear = l;

	cout << "请输入十个数据:" << endl;
	for (int i = 0; i < 10; i++)
	{
		linklist p = new node;
		p->next = NULL;
		cin >> p->data;
		rear->next=p;
		rear=p;
	}
}

void showlist(linklist l)//遍历
{
	linklist p = l->next;
	while (p != NULL)
	{
		cout <<p->data << " ";
		p = p->next;
	}
	cout<<"遍历完成"<<endl;
	cout << endl;
}

void insertlist(linklist l)//插入
{
	linklist p = l;
	int i, x,count=0;
	cout<<"请输入要在第i结点插入的数字x"<<endl;
	cin >> i >> x;
	if (l == NULL)
		cout<<"单链表未创建"<<endl;
	else
	{
		while (p!=NULL && count < i - 1)
		{
			p = p->next;
			count++;
		}
		linklist q = new node;
		q->next = NULL;
		q->data = x;
		q->next = p->next;
		p->next = q;
	}
}

void dellist(linklist l)//删除某个结点
{
	linklist p = l;
	linklist q = l;
	int i,j=0;
	cout<<"请输入要删除的第i个结点"<<endl;
	cin >> i;
	while (p!=NULL&&j<i-1)
	{
		p = p->next;
		j++;
	}
	q = p->next;
	p->next = q->next;		
	delete q;
}

void DELlist(linklist l)//删除元素
{
	int x;
	cout<<"请输入要删除的元素x"<<endl;
	cin >> x;
	linklist p = l->next;
	linklist q = l;
	while(p != NULL)
	{
		if (p->data == x)
		{
			q->next = p->next;
			q = p;
			p = p->next;
			delete q;
			linklist q=p;
		}
		q = p;
		p = p->next; 
	}
	cout<<"删除完成"<<endl;
}


void sortlist(linklist l)//排序单链表元素
{
	linklist p,q;
	int i, j;
	for (p = l->next; p != NULL;p = p->next)
	{
		for (q = p->next; q != NULL; q = q->next)
		{
			if (p->data < q->data)
			{
				int t = p->data;
				p->data = q->data;
				q->data = t;
			}
		}
	}
}

void clearlist(linklist &l)//销毁
{
	linklist p = l;
	linklist q = l;
	while (p!=NULL)
	{
		q=p;
		p = p->next;
		delete q;
	}
	cout<<"释放完成"<<endl;
}

void emptylist(linklist &l)//置空
{
	linklist p = l;
	linklist q = l;
	while (p != NULL)
	{
		q = p;
		p = p->next;
		delete q;
	}
	l->next = NULL;
	cout << "置空完成" << endl;
}

void menu()
{
	cout<<"1.初始化   2.头插"<<endl;
	cout<<"3.尾插     4.插入"<<endl;
	cout<<"5.删除     6.显示"<<endl;
	cout<<"7.销毁     8.置空"<<endl;
	cout<<"9.排序     10.删除元素"<<endl;
}

int main()
{
	menu();
	linklist l=NULL;
	int choice;
	while (1)
	{
		cout << "请输入您的选择:";
		cin >> choice;
		switch (choice)
		{
		case 1:
			initlist(l);
			break;
		case 2:
			headinsertlist(l);
			showlist(l);
			break;
		case 3:
			rearinsertlist(l);
			showlist(l);
			break;
		case 4:
			insertlist(l);
			showlist(l);
			break;
		case 5:
			dellist(l);
			showlist(l);
			break;
		case 6:
			showlist(l);
			break;
		case 7:
			clearlist(l);
			break;
		case 8:
			emptylist(l);
			break;
		case 9:
			sortlist(l);
			showlist(l);
			break;
		case 10:
			DELlist(l);
			showlist(l);
			break;
		}
	}
}

顺序栈

#include<iostream>
using namespace std;

#define MAX 100

typedef struct stack
{
	int* top;//栈顶指针 指向最后一个元素上方
	int* base;//栈底指针 指向第一个元素
	int data;
}stack;

void initstack(stack& s)//初始化
{
	s.base = new int[MAX];//从内存中分配MAX个数组空间 让base指向这个空间
	if (s.base == 0)
		cout<<"存储分配失败"<<endl;
	s.top = s.base;//栈顶指针指向栈底指针  此时没有元素
	s.data = MAX;
	cout<<"初始化成功 "<<endl;
}

void isempty(stack s)
{
	if (s.base == s.top)
		cout <<"为空" <<endl;
	else
		cout<<"不为空"<<endl;
}

void isfull(stack s)
{
	if(s.top-s.base==MAX)
		cout << "栈满" << endl;
	else
		cout << "栈未满" << endl;
}

void lenstack(stack s)
{
	cout <<"栈元素个数为:"<< s.top - s.base << endl;;//指针相减是两者之间的数量 也就是元素个数
}

void clearstack(stack &s)//清空顺序栈
{
	s.top = s.base;//让他俩指向一个地方就相当于没元素了
	cout << "清空成功" << endl;
}

void destroystack(stack& s)//销毁  和清空区别就是啥都没了
{
	if (s.base)
		s.data = 0;
	s.base = s.top = NULL;
	cout << "销毁成功" << endl;
}

void pushstack(stack &s)//进栈
{
	int x;
	cout << "请输入要进栈的元素x:" << endl;
	cin >> x;
	if (s.top - s.base == MAX)
		cout<<"栈已满"<<endl;
	//*s.top++;  一步拆成两步
	*s.top = x;
	s.top++;
	cout << "进栈成功" << endl;
}

void popstack(stack& s)//出栈
{
	int x;
	if (s.base == s.top)
		cout<<"栈空"<<endl;
	s.top--;
	x = *s.top;
	cout << "出栈成功" << endl;
}

void showstack(stack s)
{
	while (s.top - s.base>0)
	{
		cout << *(--s.top) << endl;
	}
}

void menu()
{
	cout << "1.初始化    2.是否空 " << endl;
	cout << "3.是否满    4.元素个数 " << endl;
	cout << "5.清空      6.销毁 " << endl;
	cout << "7.进栈      8.出栈 " << endl;
	cout << "9.显示栈元素       " << endl;
}

int main()
{
	stack s;
	menu();
	int choice;
	while (1)
	{
		cout << "请输入您的选择:" << endl;
		cin >> choice;
		switch (choice)
		{
		case 1:
			initstack(s);
			break;
		case 2:
			isempty(s);
			break;
		case 3:
			isfull(s);
			break;
		case 4:
			lenstack(s);
			break;
		case 5:
			clearstack(s);
			showstack(s);
			lenstack(s);
			break;
		case 6:
			destroystack(s);
			showstack(s);
			lenstack(s);
			break;
		case 7:
			pushstack(s);
			showstack(s);
			lenstack(s);
			break;
		case 8:
			popstack(s);
			showstack(s);
			lenstack(s);
			break;
		case 9:
			showstack(s);
			break;
		}
	}
}

链栈

#include<iostream>
using namespace std;

typedef struct stack
{
	int data;
	struct stack* next;
}stack, * linkstack;

void initstack(linkstack& s)
{
	s = new stack;
	if (!s)
		cout<<"内存分配失败"<<endl;
	s->next = NULL;
	cout << "初始化成功" << endl;
}

void isempty(linkstack &s)
{
	if (s->next == NULL)
		cout<<"链栈为空"<<endl;
	else
		cout << "链栈不为空" << endl;
}

void pushlinkstack(linkstack& s)
{
	int x;
	cout << "请输入要入栈的数据x:" << endl;
	cin >> x;
	linkstack p = new stack;
	if (!p)
		cout<<"内存分配失败"<<endl;
	p->data = x;
	p->next = s;//s是栈顶
	s = p;//栈顶指针上移
	cout << "入栈成功" << endl;
}

void poplinkstack(linkstack& s)
{
	int x;
	if(s->next==NULL)
		cout << "栈为空 不能出栈" << endl;
	else
	{
		x = s->data;//出栈后存储这个数据
		linkstack p = s;//建立新结点方便删除这个结点
		s = s->next;//让栈顶指针下移
		delete p;//删除原本的栈顶指针
		cout << "出栈成功" << endl;
	}
}

void lenlinkstack(linkstack &s)
{
	int sum=0;
	linkstack p = s->next;
	while (p)
	{
		p = p->next;
		sum++;
	}
	cout << "链栈元素个数为:" << sum << endl;;
}

void showlinkstack(linkstack &s)
{
	linkstack p = s;
	while (p->next)
	{
		cout << p->data << endl;
		p = p->next;
	}
}

void clearlinkstack(linkstack& s)
{
	while (s->next != NULL)
	{
		poplinkstack(s);
	}
	cout<<"清空成功"<<endl;
}

void destroylinkstack(linkstack& s)
{
	while (s->next != NULL)
	{
		poplinkstack(s);
	}
	delete s;
	s = NULL;
	cout<<"销毁成功"<<endl;
}

void menu()
{
	cout << "1.初始化    2.栈是否空" << endl;
	cout << "3.入栈      4.出栈" << endl;
	cout << "5.显示元素  6.元素个数" << endl;
	cout << "7.清空      8.销毁" << endl;
}

int main()
{
	menu();
	linkstack s;
	int choice;
	while (1)
	{
		cout << "请输入您的选择:" << endl;
		cin >> choice;
		switch (choice)
		{
		case 1:
			initstack(s);
			break;
		case 2:
			isempty(s);
			lenlinkstack(s);
			break;
		case 3:
			pushlinkstack(s);
			showlinkstack(s);
			lenlinkstack(s);
			break;
		case 4:
			poplinkstack(s);
			showlinkstack(s);
			lenlinkstack(s);
			break;
		case 5:
			showlinkstack(s);
			lenlinkstack(s);
			break;
		case 6:
			lenlinkstack(s);
			break;
		case 7:
			clearlinkstack(s);
			break;
		case 8:
			destroylinkstack(s);
			break;
		}
	}
}

顺序队列

#include<iostream>
using namespace std;

#define MAX 100

typedef struct queue
{
	int* base;
	int front;//头下标
	int rear;//尾下标
}queue;


void initqueue(queue& q)
{
	q.base = new int[MAX];
	if (!q.base)
		cout<<"内存分配失败"<<endl;
	q.front = q.rear=0;
	cout<<"初始化成功"<<endl;
}

void lenqueue(queue q)
{
	int len;
	len = (q.rear - q.front + MAX) % MAX;
	cout << "元素个数为:" << len << endl;
}

void pushqueue(queue &q)
{
	int x;
	cout<<"请输入要入队的元素x:"<<endl;
	cin >> x;
	if ((q.rear + 1) % MAX == q.front)
		cout<<"队列已满"<<endl;
	q.base[q.rear] = x;
	q.rear = (q.rear + 1) % MAX;
	cout<<"入队成功"<<endl;
}

void popqueue(queue& q)
{
	int x;
	if (q.front == q.rear)
		cout<<"队列为空"<<endl;
	x = q.base[q.front];
	q.front = (q.front + 1) % MAX;
	cout<<"出队成功"<<endl;
}

void gettopqueue(queue q)//获取队头元素和出队的区别就是  获取元素之后 队的元素还在里面 所以坐标位置不动  但是出队需要改变坐标位置
{
	int x;
	if (q.front != q.rear)
		x = q.base[q.front];
	cout<<"获取队头元素成功"<<endl;
}

void showqueue(queue& q)
{
	if (q.front == q.rear)
		cout<<"队列为空"<<endl;
	else
	{
		int i = q.front;
		while (i != q.rear)
		{
			cout << q.base[i] << endl;;
			i = (i + 1) % MAX;
		}
	}
}

void menu()
{
	cout<<"1.初始化       2.元素个数"<<endl;
	cout<<"3.入队         4.出队"<<endl;
	cout<<"5.获取头元素   6.遍历队列  "<<endl;

}



int main()
{
	menu();
	queue q;
	int choice;
	while (1)
	{
		cout<<"请输入您的选择:"<<endl;
		cin >> choice;
		switch (choice)
		{
		case 1:
			initqueue(q);
			break;
		case 2:
			lenqueue(q);
			break;
		case 3:
			pushqueue(q);
			showqueue(q);
			lenqueue(q);
			break;
		case 4:
			popqueue(q);
			showqueue(q);
			lenqueue(q);
			break;
		case 5:
			gettopqueue(q);
			break;
		case 6:
			showqueue(q);
			break;
		}
	}
}

链队列

#include<iostream>
using namespace std;

#define MAX 100

typedef struct queue
{
	int data;
	struct queue* next;
}queue,*queueptr;

typedef struct
{
	struct queue* front;//queueptr front
	struct queue* rear;
}linkqueue;

void initqueue(linkqueue& q)
{
	q.front = q.rear = new queue;//指向同一块结点
	if (!q.front)
		cout<<"内存分配失败"<<endl;
	q.front->next = NULL;
	cout<<"初始化成功"<<endl;
}

void destroyqueue(linkqueue& q)
{
	while (q.front)
	{
		//q.front指向的是struct queue型
		queueptr p = q.front->next;//新建一个结点指向头结点下一个
		delete q.front;//删除头结点
		q.front = p;//实现原先的头结点后移
	}
}

void pushqueue(linkqueue& q)
{
	int x;
	cout<<"请输入要入队的数据x:"<<endl;
	cin >> x;
	queueptr p=new queue;//新建一个结点
	if (!p)
		cout<<"分配结点失败"<<endl;
	p->data = x;
	p->next = NULL;//习惯
	q.rear->next = p;//让rear指向插入的结点
	q.rear = p;//rear指针后移
	cout<<"入队成功"<<endl;
}

void popqueue(linkqueue& q)
{

	int x;
	if (q.front == q.rear)
		cout<<"队列为空"<<endl;
	x = q.front->next->data;
	queueptr p = q.front->next;//让一个指针指向要出队的结点
	q.front->next = p->next;//让头结点指向出队结点的后面一个结点
	if (q.rear == p)//最后一个元素出队 rear指向头结点
		q.rear = q.front;
	delete p;
	cout<<"出队成功"<<endl;
}

void gettopqueue(linkqueue& q)
{
	int x;
	if (q.front == q.rear)
		cout << "队列为空" << endl;
	x = q.front->next->data;
	cout<<"队头元素为:"<<x<<endl;
}

void showqueue(linkqueue& q)
{
	if (q.front == q.rear)
		cout<<"队列为空"<<endl;
	queueptr p = q.front->next;
	while (p)
	{
		cout << p->data;
		p = p->next;
	}
	cout<<"遍历完成"<<endl;
}

void lenqueue(linkqueue& q)
{
	int sum = 0;
	queueptr p = q.front->next;
	while (p)
	{
		sum++;
		p = p->next;
	}
	cout<<"队列元素个数为:"<<sum<<endl;
}

void clearqueue(linkqueue& q)
{
	queueptr p = q.front->next;
	while (p)
	{
		q.front = p->next;
		delete p;
		p = q.front;
	}
	cout<<"清空成功"<<endl;
	delete q.front;
	cout<<"销毁成功"<<endl;
}

void menu()
{
	cout<<"1.初始化    2.入队"<<endl;
	cout<<"3.出队      4.队头元素"<<endl;
	cout<<"5.遍历      6.元素个数"<<endl;
	cout<<"7.清空销毁            "<<endl;
}

int main()
{
	menu();
	int choice;
	linkqueue q;
	while (1)
	{
		cout<<"请输入您的选择:"<<endl;
		cin >> choice;
		switch (choice)
		{
		case 1:
			initqueue(q);
			break;
		case 2:
			pushqueue(q);
			break;
		case 3:
			popqueue(q);
			break;
		case 4:
			gettopqueue(q);
			break;
		case 5:
			showqueue(q);
			break;
		case 6:
			lenqueue(q);
			break;
		case 7:
			clearqueue(q);
			break;
		}
	}
}

作者:吕文康
学校:山东第一医科大学
2020.1.22

Java初级数据结构与算法是指在Java编程语言中使用的基本数据结构和算法。在Java中,我们可以使用各种数据结构来存储和操作数据。常见的数据结构包括数组、链表、栈、队列和树等。这些数据结构可以用来解决各种实际问题,如搜索、排序、查找和插入等。 在Java中,我们可以使用ArrayList类来实现动态数组,LinkedList类来实现链表,Stack类来实现栈,Queue接口来实现队列,以及TreeSet和HashMap等类来实现树和哈希表等数据结构。这些数据结构提供了各种方法来操作和管理数据,如添加元素、删除元素、查找元素和遍历元素等。 此外,算法是解决问题的步骤和方法。在Java中,我们可以使用各种算法来处理数据结构中的数据。常见的算法包括线性搜索、二分搜索、冒泡排序、快速排序和图遍历等。这些算法可以帮助我们高效地处理和操作数据。 总结来说,Java初级数据结构与算法是指在Java编程语言中使用的基本数据结构和算法,包括数组、链表、栈、队列、树和哈希表等数据结构,以及搜索、排序、查找和遍历等算法。通过学习和掌握这些数据结构和算法,我们可以更好地解决实际问题并提高程序的效率。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [【Java】数据结构与算法入门](https://blog.csdn.net/LongLiveThePRC/article/details/124573277)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [1.0 JAVA数据结构与算法](https://blog.csdn.net/xiexieya233/article/details/124082025)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [java基础语法:数据结构和算法](https://blog.csdn.net/qq_50395011/article/details/109739043)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值