先进先出(FIFO)链表的实现

#include "iostream"
using namespace std;

struct Node
{
	int data;
	Node *next;

};

struct List
{
	Node *head;
	Node *end;
};

//创建链表
List* createList()
{
	List *list=new List();
	list->head=new Node();
	list->head->next=NULL;
	list->end=list->head;

	return list;
}

//插入(在尾结点插入)
bool insertIntoList(List *list,int data)
{
	if(list==NULL)
	{
	  return false;
	}
	Node * p=new Node();
	p->data=data;
	p->next=NULL;
	list->end->next=p;
	list->end=p;	
}

//删除(在头结点删除)
bool deleteFromList(List *list)
{
	if(list==NULL)
		return false;
	if(list->head==list->end)
		return false;
	
	Node *cur=list->head->next;
	list->head->next=cur->next;
	delete(cur);

}

//打印链表
void printList(List *list)
{
	Node * p=list->head->next;
	while(p)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;

}

int main()
{
	List *list=createList();
	insertIntoList(list,3);
	insertIntoList(list,4);
	insertIntoList(list,5);
	insertIntoList(list,6);
	printList(list);

	deleteFromList(list);
   	deleteFromList(list);
	deleteFromList(list);

	printList(list);
  return 0;
}

展开阅读全文

没有更多推荐了,返回首页