小K对链表的一点理解

来源:互联网 发布:找客户的app软件 编辑:程序博客网 时间:2024/06/15 01:32

马上就要踏上社会,回想有点羞愧大学这几年也没留下什么。最近需要面试一家公司,所以需要把以前的一些东西复习一下,所以顺便就写一个博客吧。这次是写的对链表的复习。刚回想链表的时候就知道有个头指针,有一个当前位置的指针,有一个专门存储数据的指针。然后一个存一个记录不断交替进行就行。其实也就是这样的。只是很小的一点地方注意就好。直接贴代码吧。

#include<iostream>

#include<stdlib.h>
using namespace std;
struct Node
{
int data;
Node* nextNode;
};
int main()
{
Node* firstNode=NULL;
Node* q=NULL;
       Node* p=NULL;
Node*t=firstNode;
int a;
int n=3;
while(n--)
{
cin>>a;
  p=new Node;
  p->data=a;
  p->nextNode=NULL;
  if(firstNode==NULL)
  {
  firstNode=p;
  q=p;
  }
  else
  {
  q->nextNode=p;
q=p;
  }
  }
 for ( Node *it = firstNode ; it != NULL ; it = it ->nextNode )
  cout<<it->data<<endl;;
system("pause");

这个只是很简单的实现了一个增加的功能。然后贴一个stl中list的代码  真的是很好用呀~

#include<iostream>
#include<list>
#include <algorithm>
using namespace std;
int main()
{
list<int>  linkedList;
int a;
list<int>::iterator itLinkedList;
while(cin>>a)
{
  linkedList.push_back(a);
  }
 
  for(itLinkedList=linkedList.begin();itLinkedList!=linkedList.end();
++itLinkedList)
cout<<*itLinkedList<<endl;
system("pause");
}

写的不对的欢迎交流 

1 0
原创粉丝点击