数据结构:第一次上机

1、合并有序链表

#include<iostream>
using namespace std;
struct ListNode 
{ 
    int val;
    ListNode* next; 
    ListNode():val(0),next(nullptr){}
    ListNode(int x) :val(x), next(nullptr) {}
};

ListNode* createlist(int n) //创建长度为n的链表
{
    ListNode* head = new ListNode(); //创建空的头节点,此后一直认为头节点是空的
    ListNode* p = head; //创建指针指向头节点
    
    for (int i = 0; i < n; i++)//用for循环输入节点的数,建立链表
    {
        int x = 0;
        cin >> x;
        ListNode* newnode = new ListNode(x);
        head->next = newnode;
        head = head->next;

    }
    head = p; // 输入到此结束
    return head;

}
ListNode* merge_recursion(ListNode* h1, ListNode* h2)
{
    if (!h2)
        return h1;
    if (!h1)
        return h2;


    if (h1->val < h2->val)
    {
        h1->next = merge_recursion(h1->next, h2);
        return h1;
    }
    else
    {
        h2->next = merge_recursion(h1, h2->next);
        return h2;
    }
}

void print(ListNode* h) //打印链表,参数为某链表的头指针,操作不改变实参
{
    ListNode* n = h->next;   // 因为带头节点,因此需要从head->next开始遍历链表
    if (n == nullptr)    //若是空链表,输出 NULL
        cout << "NULL" << endl;
    else
    {
        while (n != nullptr)
        {
            cout << n->val << ' ';
            n = n->next;
        }
        cout << endl;
    }
}
int main()
{
    int n1 = 0;
    int n2 = 0;

    while (cin>>n1)
    {
        cin >> n2;
        ListNode* h1 = createlist(n1);
        ListNode* h2 = createlist(n2);
        ListNode* h = new  ListNode();
        h->next = merge_recursion(h1->next, h2->next);

        print(h);

    }
    return 0;
}

2、栈:括号

#include<iostream>
#include<string>
using namespace std;



bool istrue(string s)
{
	string stack[20];
	int top = 0;

	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '(')
		{
			stack[top++] = '(';
		}
		else
		{
			if (top == 0)
				return false;
			top--;
		}
	}
	if (top == 0)
		return true;
	else
		return false;
}

int main()
{
	int n = 0;
	cin >> n;
	while (n--)
	{
		string s;
		cin >> s;
		if (istrue(s))
			cout<< "TRUE"<<endl;
		else
			cout<<"FALSE"<<endl;
		
	}

}

3、maxtoend

#include<iostream>
using namespace std;

struct List
{
    int data;
    List* next;
};
void append(List* p, int x)
{
    List* q = p;
    p = p->next;
    while (p != NULL)
    {
        q = p;
        p = p->next;
    }
    List* t = new List();
    t->data = x;
    q->next = t;
    t->next = p;
}

void maxtoend(List* p)
{
    List* q = p;
    List* pmax = q;
    List* pre = q;
    p = p->next;
    while (p != NULL)
    {
        if ((pmax->data) < (p->data))
        {
            pre = q;
            pmax = p;
        }
        q = p;
        p = p->next;
    }
    List* t = new List();
    t->data = pmax->data;
    q->next = t;
    t->next = p;
    pre->next = pmax->next;

}

int main()
{
    int n;//有多少ge数据
 
    List* head = new List();
    head->next = NULL; // 创建带头节点的链表

    while (cin >> n)
    {
        append(head, n);
    }

    
    maxtoend(head);

    List* f = head->next; // 因为带头节点,因此需要从head->next开始遍历链表
    if (f == NULL)
    {
        cout << "NULL";
    }
    while (f != NULL)
    {
        cout << f->data << " ";
        f = f->next;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值