建立一个动态列表(结点的结构自拟)

发布时间:2024-05-11 05:00 发布:上海旅游网

问题描述:

写出程序代码,输入是数据和输出结果,谢谢!!

问题解答:

楼上的也太复杂了,我给个简单的:
#include <stdio.h>
#include <malloc.h>
#include <string.h>

struct student
{
char name[20];
char sex[7];
int score ;
struct student * next;
};

main()
{
struct student *head=NULL;
struct student *p,*p1;
char name[20],sex[7];
int score;

//建立链表
while(1)
{
printf("Input name, sex and score:\n");
scanf("%s%s%d",name,sex,&score );
if(score<0)
{
break;
}
p1=(struct student *)malloc(sizeof(struct student));
p1->next=NULL;
strcpy(p1->name,name);
strcpy(p1->sex,sex);
p1->score=score;
if(head==NULL)
{
head=p1;
p=head;
}
else
{
p->next=p1;
p=p1;
}
}

//打印链表
p=head;
while(p!=NULL)
{
printf("Name:%s\t Sex:%s\t Score:%d\n",p->name,p->sex,p->score);
p=p->next;
}
getchar();
}

输入:
gao male 90
li female -1
输出:
gao male 90

IList.h

#ifndef ILIST_H
#define ILIST_H

template <class T>
class IList //interface
{
public:
virtual int add(const T& item) = 0;
virtual void add(int index, const T& item) = 0;
virtual void clear() = 0;
virtual bool contains(const T& item) = 0;
virtual int indexOf(const T& item) = 0;
virtual T get(int index) const = 0;
virtual T set(int index, const T& item) = 0;
virtual T removeAt(int index) = 0;
virtual void remove(const T& item) = 0;
virtual int size() const = 0;
virtual ~IList() {}
};
#endif

LinkedList.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "IList.h"

template <class T>
class LinkedList : public IList<T>
{
private:
struct Node
{
T data;
Node* previous;
Node* next;
}*first,*last;
int count;
public:
LinkedList()
{
first = last = 0;
count = 0;
}

LinkedList(const T* pArray, int len = 1)
{
first = last = 0;
count = 0;
for(int i = 0; i < len; i++)
add(pArray[i]);
}

int add(const T& item)
{
if(!first)
{
first = new Node;
first->data = item;
first->previous = 0;
first->next = 0;
last = first;
}
else
{
last->next = new Node;
last->next->previous = last;
last = last->next;
last->data = item;
last->next = 0;
}
return ++count;
}

void add(int index, const T& item)
{
index = index < 0 ? 0 : index;
if(index < count)
{
Node* pos = getNode(index);
Node* tmp = new Node;
tmp->data = item;
tmp->previous = pos;
tmp->next = pos->next;
pos->next = tmp;
tmp->next->previous = tmp;
count++;
}
else
add(item);
}

void clear()
{
Node* cur = first;
Node* tmp;
while(cur)
{
tmp = cur;
cur = cur->next;
delete tmp;
}
first = last = 0;
count = 0;
}

bool contains(const T& item)
{
if(indexOf(item) >= 0)
return true;
return false;
}

int indexOf(const T& item)
{
for(int i = 0; i < count; i++)
if(item == (*this)[i])
return i;
return -1;
}

T& operator [](int index) const
{
if(index < 0)
index = 0;
else if(index >= count)
index = count -1;
return getNode(index)->data;
}

T get(int index) const
{
return (*this)[index];
}

T set(int index,const T& item)
{
T tmp = (*this)[index];
(*this)[index] = item;
return tmp;
}

T removeAt(int index)
{
if(index <= 0)
{
return removeFirst();
}
else if(index >= count-1)
{
return removeLast();
}
Node* pos = getNode(index);
pos->previous->next = pos->next;
pos->next->previous = pos->previous;
T tmp = pos->data;
count--;
delete pos;
return tmp;
}

void remove(const T& item)
{
int index = indexOf(item);
if(index >= 0)
removeAt(index);
}

inline int size() const
{
return count;
}

inline T getFirst() const
{
return first->data;
}

inline T getLast() const
{
return last->data;
}

inline T removeFirst()
{
Node* tmp = first;
first = first->next;
first->previous = 0;
count--;
T t = tmp->data;
delete tmp;
return t;
}

inline T removeLast()
{
Node* tmp = last;
last = last->previous;
last->next = 0;
count--;
T t = tmp->data;
delete tmp;
return t;
}

void addFirst(const T& item)
{
Node* tmp = new Node;
tmp->data = item;
tmp->next = first;
tmp->previous = 0;
first = tmp;
}

void addLast(const T& item)
{
add(item);
}

~LinkedList()
{
clear();
}

private:
Node* getNode(int index) const
{
Node* cur;
if(index < count / 2)
{
cur = first;
while(index-- > 0)
cur = cur->next;
}
else
{
cur = last;
while(++index < count)
cur = cur->previous;
}
return cur;
}
};

#endif

Main.cpp

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

//用来测试自定义类型
class Student
{
private:
int id;
char name[20];
public:
Student():id(0)
{
*name = 0;
}

Student(int id, char* name)
{
this->id = id;
strcpy(this->name, name);
}

Student(const Student& rhs)
{
id = rhs.id;
strcpy(name, rhs.name);
}

Student& operator =(const Student& rhs)
{
id = rhs.id;
strcpy(name, rhs.name);
return *this;
}

friend bool operator ==(const Student& lhs, const Student& rhs)
{
return lhs.id == rhs.id;
}

friend ostream& operator <<(ostream& os, const Student& rhs)
{
os<<"id="<<rhs.id<<"\tname="<<rhs.name;
return os;
}
};

template <class T>
void display(IList<T>* list) //显示数据
{
for(int i = 0; i < list->size(); i++)
cout<<list->get(i)<<"\t";
cout<<endl;
}

//用来测试前面的模板类
int main()
{
int buf[5] = {1,2,3,4,5};
Student stu[3]={Student(1,"zhangsan"),Student(2,"lisi"),Student(3,"wangwu")};
cout<<"test LinkedList in int"<<endl;
LinkedList<int> ll(buf,5);
display(&ll);

ll.add(10);
ll.add(11);
ll.add(12);
ll.set(2,10);
display(&ll);

ll.remove(10);
ll.removeAt(0);
display(&ll);

ll.removeAt(3);
display(&ll);

cout<<"test LinkedList in Student"<<endl;
LinkedList<Student> link(stu,3);
display(&link);

link.add(0,Student(0,"abc"));
link.add(Student(4,"xyz"));
display(&link);

link.remove(stu[1]);
display(&link);

link.removeAt(1);
display(&link);
return 0;
}

热点新闻