C++/C语言中链表队列与栈

// 链表、队列和栈.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
using namespace std;

class Student{
public:
	int num;
	Student *head,*end,*next,*pre;
};

static int i = 0;

Student *creat(void){
	Student *head,*end,*p1,*p2;
	cout<<"现在开始创建链表:"<<endl;
	end=head=p1=p2=new Student;
	cin>>p1->num;
	head->next=p1;
	p1->pre=head;
	if(p1->num!=0){	
		cin>>p2->num;
		while(p2->num!=0){
			p1->next=p2;
			p2->pre=p1;
			p1=p1->next;
			p2=new Student;
			cin>>p2->num;
		}
		end->pre=p1;
		p1->next=end;
		delete p2;
		if(i==1)
			return (end);
	}else{
		head=end;
		delete p1,p2;
	}
		return (head);
}

Student *fun(Student *head,int k){
	Student *p;
	p=head;
	if(i!=1){
	    cout<<p->num<<"  ";
		for(int z=0;z<k;z++){
			p=p->next;
			cout<<p->num<<"   ";
		}
	}else{
	    cout<<p->num<<"  ";
		for(int z=0;z<k-1;z++){
			p=p->pre;
			cout<<p->num<<"   ";
		}
	}
	return head;
}

int main(){
	cout<<"要创建队列还是栈:"<<endl;
	cout<<"输入1创建立栈,输入2建立队列:"<<endl;
	cin>>i;
	Student *head = creat();
	cout<<"输入要出队或出栈的数量:"<<endl;
	int k;
	cin>>k;
	fun(head,k);
	return 0;
}
这究竟是怎么一回事,有错改不过来,时间来不及了,不改了。
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值