【编程】求一段用visual C++6.0编写的单链表的代码

2024-12-05 03:07:41
推荐回答(3个)
回答1:

#ifndef LINITLIST_H_
#define LINITLIST_H_

#include
#include

template
class LinitList
{
private:
class ListNode
{
public:
ListNode():prev_(0), next_(0)
{

}

void print() const
{
std::cout< }

~ListNode()
{

}

ListNode* prev_;

ListNode* next_;

Ty_ val_;
};

public:
LinitList():head_(0), node_(0), link_(0), tail_(0), size_(0)
{

}

int size_get() const
{
return size_;
}

void push_back(const Ty_& val)
{
if(size_ == 0)
{
head_ = new ListNode;
head_->val_ = val;
tail_ = head_;
}
else
{
tail_->next_ = new ListNode;
tail_->next_->val_ = val;
tail_->next_->prev_ = tail_;
tail_ = tail_->next_;
}
size_++;
}

void pop_back()
{
if(size_ == 0 || size_ == 1)
{
if(size_ == 1)
{
delete head_;
}
head_ = 0;
tail_ = 0;
size_ = 0;
}
else
{
node_ = tail_;
tail_->prev_->next_ = 0;
tail_ = tail_->prev_;
delete node_;
}
size_--;
}

void erase(int begin, int count)
{
ListNode* temp;
if(begin + count > size_)
{
count = size_ - begin - 1;
if(begin > size_)
{
begin = size_ - 1;
count = 1;
}
}
if(begin > size_/2)
{
node_ = tail_;
for(int index = 0; index < size_ - begin; index++)
{
node_ = node_->prev_;
}
node_ = node_->next_;
}
else
{
node_ = head_;
for(int index = 0; index < begin; index++)
{
node_ = node_->next_;
}
}
temp = node_;
if(begin == 0)
{
link_ = node_;
}
else
{
link_ = node_->prev_;
}
node_ = head_;
for(int index = 0; index < (begin + count); index++)
{
node_ = node_->next_;
}
if(node_ == NULL)
{
if(begin == 0)
{
head_ = NULL;
tail_ = NULL;
}
else
{
link_->next_ = NULL;
tail_ = link_;
}
}
else
{
if(begin == 0)
{
head_ = node_;
head_->prev_ = NULL;
}
else
{
link_->next_ = node_;
node_->prev_ = link_;
}
}
for(int i = 0; i < count; i++)
{
node_ = temp->next_;
delete temp;
temp = node_;
}
size_ -= count;
}

void insert(int index, const Ty_& val)
{
if(size_ == 0)
{
head_ = new ListNode;
head_->val_ = val;
tail_ = head_;
}
else
{
if(index > size_ - 1)
{
tail_->next_ = new ListNode;
tail_->next_->val_ = val;
tail_->next_->prev_ = tail_;
tail_ = tail_->next_;
}
else
{
if(index > size_/2)
{
node_ = tail_;
for(int i = 0; i < size_ - index; i++)
{
node_ = node_->prev_;
}
node_ = node_->next_;
}
else
{
node_ = head_;
for(int i = 0; i < index; i++)
{
node_ = node_->next_;
}
}
link_ = node_->prev_;
if(link_ == NULL)
{
head_->prev_ = new ListNode;
head_->prev_->val_ = val;
head_->prev_->next_ = head_;
head_ = head_->prev_;
}
else
{
node_->prev_ = new ListNode;
node_->prev_->val_ = val;
node_->prev_->prev_ = link_;
link_->next_ = node_->prev_;
node_->prev_->next_ = node_;
}
}
}
size_++;
}

void insert(int index, const LinitList& other)
{
if(index > size_)
{
index = size_ - 1;
}
if(size_ <= 0)
{
head_ = new ListNode;
head_->val_ = other.head_->val_;
node_ = head_;
link_ = other.head_;
while(link_->next_ != NULL)
{
node_->next_ = new ListNode;
node_->next_->val_ = link_->next_->val_;
node_->next_->prev_ = node_;
node_ = node_->next_;
link_ = link_->next_;
}
tail_ = node_;
}
else
{
if(index > size_/2)
{
node_ = tail_;
for(int i = 0; i < size_ - index; i++)
{
node_ = node_->prev_;
}
if(index != size_ - 1)
{
node_ = node_->next_;
}
}
else
{
node_ = head_;
for(int i = 0; i < index; i++)
{
node_ = node_->next_;
}
}
link_ = other.head_;
ListNode* temp;
temp = node_->next_;
while(link_ != NULL)
{
node_->next_ = new ListNode;
node_->next_->val_ = link_->val_;
node_->next_->prev_ = node_;
node_ = node_->next_;
link_= link_->next_;
}
if(temp != NULL)
{
node_->next_ = temp;
temp->prev_ = node_;
}
else
{
tail_ = node_;
}
}
size_ += other.size_;
}

int find(int index, const Ty_& val)
{
if(index > size_)
{
return -1;
}
if(index > size_)
{
node_ = tail_;
for(int i = 0; i < index; i++)
{
node_ = node_->prev_;
}
}
else
{
node_ = head_;
for(int i = 0; i < index; i++)
{
node_ = node_->next_;
}
}
while(node_ != NULL)
{
if(node_->val_ == val)
{
return index;
}
else
{
node_ = node_->next_;
index++;
}
}
return -1;
}

Ty_& ListNode_get(int index)
{
if(index > size_)
{
return tail_->val_;
}
if(index > size_/2)
{
node_ = tail_;
for(int i = 0; i < size_ - index; i++)
{
node_ = node_->prev_;
}
node_ = node_->next_;
}
else
{
node_ = head_;
for(int i = 0; i < index; i++)
{
node_ = node_->next_;
}
}
return node_->val_;
}

void operator=(const LinitList& other)
{
link_ = other.head_;
head_ = new ListNode;
head_->val_ = link_->val_;
node_ = head_;
while(link_->next_ != NULL)
{
node_->next_ = new ListNode;
node_->next_->val_ = link_->next_->val_;
node_->next_->prev_ = node_;
link_ = link_->next_;
node_ = node_->next_;
}
tail_ = node_;
size_ = other.size_;
}

void print()
{
node_ = head_;
while(node_ != NULL)
{
std::cout<val_< node_ = node_->next_;
}
}

void reverseOrderPrint()
{
node_ = tail_;
while(node_ != NULL)
{
std::cout<val_< node_ = node_->prev_;
}
}

void clear()
{
node_ = head_;
while(node_ != NULL)
{
link_ = node_->next_;
delete node_;
node_ = link_;
}
head_ = 0;
tail_ = 0;
size_ = 0;
}

~LinitList()
{
node_ = head_;
while(node_ != NULL)
{
link_ = node_->next_;
delete node_;
node_ = link_;
}
}

private:
ListNode* head_;

ListNode* node_;

ListNode* link_;

ListNode* tail_;

int size_;
};

#endif

回答2:

#include
#include

typedef struct node
{
int a;
struct node *next ;
}N;

int Input(N *L)
{
int n,x=1;
N *q=L;
N *newnode=(N *)malloc(sizeof(N));
printf("请输入结点值:");
scanf("%d",&n);
newnode->a=n;
newnode->next=NULL;

newnode->next=q->next;
q->next=newnode;
while(1)
{
N *newnode=(N *)malloc(sizeof(N));
printf("请输入结点值:");
scanf("%d",&n);
if(n==-1) break;
newnode->a=n;
newnode->next=NULL;
newnode->next=q->next;
q->next=newnode;
x++;
}
return x;
}

void Output(N *L,int a)
{
int x=a;
N *p=L->next;
printf("结点数值依次为:");
for(int i=0;i {
printf("%d ",p->a);
p=p->next;

}
printf("\n");

}

void Insert(N *L)
{
int n,i;
N *p=L->next,*q=L;
N *newnode=(N *)malloc(sizeof(N));
printf("请输入插入的结点值:");
scanf("%d",&n);
newnode->a=n;
newnode->next=NULL;

printf("请输入插入结点位置!");
scanf("%d",&i);
for(int j=0;j {
p=p->next;
q=q->next;
}
q->next=newnode;
newnode->next=p;

}
void Del(N *L)
{
int i;
N *p=L->next,*q=L;
printf("删除第几个节点:");
scanf("%d",&i);
for(int j=1;j {
p=p->next;
q=q->next;
}
q->next=p->next;
p->next=NULL;
free(p);

}

void main()
{
int a;
N *L=(N *)malloc(sizeof(N));
L->next=NULL;
a=Input(L);
printf("结点数为%d\n",a);
Output(L,a);
Insert(L);
printf("插入结点后结点数值依次为:");
Output(L,a+1);
Del(L);
printf("删除节点后的节点序列依次为:");
Output(L,a);

}

纯手打,绝对不是粘贴的,别忘了给分……

回答3:

http://hi.baidu.com/mrzd/blog/item/8df8354f1b3bb633aec3ab09.html