C/C++的一些基本问题
来源:互联网 发布:网络红包用英语怎么说 编辑:程序博客网 时间:2024/05/17 23:50
结构体的定义
typedef struct student
{
int num;
struct student *next;
}student;
或者使用
struct student
{
int num;
struct student *next;
};
第一个是用typedef把struct student这个结构体类型名字重新定义为student,也就是说struct student和student表示同一个事物,都是一个类型的标识符,比如 typedef int zhengshu; 就是你把整型int重命名为zhengshu,下面定义:int i; 和 zhengshu i; 两句就是等价的了。
第一种是定义一个结构体并取个student结构体名,下次定义结构体是:只要:student <标识符>; 如student stu;而第二种:必须这样定义:struct student stu;
C语言创建新节点为:
Node *newNode ,*head,*tail;//注意new在C++中为关键字,如果使用VC编译器可能会出现问题,这里可以使用*p
newNode = (Node*)malloc(sizeof(Node));
C++为:
newNode = (Node*)new(Node);
/*链表节点逆置:模拟尾插算法
在本算法中共使用了两个其他的指针变量空间复杂度是O(1)所以是就地逆置的*/
void rever_chain (node *head)
{
node *temp;
node *next;
temp = head->next ;
/*temp指向首个有数据的节点*/
head->next = NULL ;
/*构造一个头节点:head*/
while(temp) {
next = temp->next ;
temp->next = head->next ; //
head->next = temp ; //head的next是用来当前被断开链表的头结点
temp = next ;
}
}
另外,在编译时通过,连接时出现:unresolved external symbol "struct Node * __cdecl create(void)"的错误解决方法是:
你在main函数中调用了Create()函数,但是你从来没有定义过这个函数。你定义过的函数叫create,c是小写的,简单的说就是你拼错了,改成正确的拼写就可以了。希望这类问题以后可以自行解决~
有时会遇到下面的错误:
d:\program files\microsoft visual studio\vc98\include\ios.h(146) : error C2872: 'streambuf' : ambiguous symbol
d:\program files\microsoft visual studio\vc98\include\ios.h(159) : error C2872: 'ostream' : ambiguous symbol
d:\program files\microsoft visual studio\vc98\include\ios.h(159) : error C2872: 'ostream' : ambiguous symbol
d:\program files\microsoft visual studio\vc98\include\ios.h(160) : error C2872: 'ostream' : ambiguous symbol
d:\program files\microsoft visual studio\vc98\include\ios.h(180) : error C2872: 'streambuf' : ambiguous symbol
d:\program files\microsoft visual studio\vc98\include\ios.h(207) : error C2872: 'streambuf' : ambiguous symbol
错误原因:
就是 #incldue <iostream> 同时 #incldue <iostream.h> 并且 还 using namespace std;
下面是链表的一些基本操作 (C++版):
#include <iostream>
using namespace std;
typedef int eleType;
struct Node
{
eleType data;
struct Node *next;
};
Node *Create();// create the linklist
Node* Insert(Node* head , int data); //插入节点
int ListLength(Node *L); //链表的计数
int Search(Node *L , int value); //链表的查找
void Print(Node *head); //输出链表
void Destruct(Node *head); //清空链表
Node *ReverseList(Node *head); //链表逆序(循环方法)
Node *ReverseList2(Node *head); //链表逆序(递归方法)
Node* Delete(Node* head , int data); //删除节点
int main()
{
Node *head=Create();
cout<<ListLength(head)<<endl;
head=Insert(head,6);
cout<<ListLength(head)<<endl;
Print(head);
head=ReverseList(head);
Print(head);
head=ReverseList2(head);
Print(head);
cout<<"3 is located on "<<Search(head,3)<<endl;
Delete(head,3);
Destruct(head);
return 1;
}
Node *Create()
{
int n=0;
Node *p,*tail,*head;
p=new (Node);
cout<<"please input the data( when <=0 is to stop):"<<endl;
cin>>p->data;
head=NULL;
while (p->data>0)//小于或等于0将会停止创建节点
{
if (n==0)//创建的链表不带有头结点,若带有头结点,则后续处理中头结点都不能予以考虑
{
head=tail=p;
}
else
{
tail->next=p;
}
tail=p;
n++;
p=new Node;
cin>>p->data;
}
tail->next=NULL;
return head;
}
int ListLength(Node *L)
{
Node *p=L;
int count=0;
while(p)
{
count++;
p=p->next;
}
return count;
}
int Search(Node *L , int value)
{
Node *p=L;
int index=0;
while(p)
{
if(p->data== value)
return ++index;//返回的从1开始的位置
p=p->next;
index++;
}
return 0;
}
void Print(Node *head)
{
Node* p=head;
while (p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void Destruct(Node *head)
{
Node *current = head, *temp = NULL;
while (current)
{
temp = current;
current = current ->next;
delete temp;
}
}
Node *ReverseList(Node *head) //链表逆序(循环方法)
{
Node *p,*q,*r;
p=head; //一开始p指向第一个节点
q=p->next; //q指向第二个节点
while (q!=NULL) //如果没到链尾
{ //以第一次循环为例
r=q->next; //r暂时存储第三个节点
q->next=p; //没执行此句前,q->next指向第三个节点 //执行之后,q->next指向第一个节点p
p=q; //之后p指向第二个节点
q=r; //q指向第三个节点 //即...p=>q=>r...变为 ...p<=q<=r...
}
head->next=NULL; //最后原来的链头变为链尾,把它指向NULL。
head=p; //原来的链尾变成链头
return head;
}
Node *ReverseList2(Node *head) //链表逆序(递归方法)
{
if (!head)
{
return NULL;
}
Node *temp = ReverseList2 (head->next);//将head->next后面的整体进行逆置
if (!temp)
{
return head;
}
head->next->next = head;//head接到head->next的后面,两个交换位置即可。
head->next = NULL;
return temp;
}
Node* Delete(Node* head , int data) //删除节点
{
if (head==NULL)
{
cout<<"List is Null"<<endl;
return head;
}
Node *p1,*p2;
p1=head;
while (p1->data!=data && p1->next)
{
p2=p1;
p1=p1->next;
}
if (p1->data==data)
{
if (p1==head)
{
head=p1->next;
}
else
p2->next=p1->next;
}
else
cout<<"Do not Find The Num "<<data<<endl;
return head;
}
Node* Insert(Node* head , int num) //插入节点
{
Node *p0,*p1,*p2;
p1=head;
p0=new Node;
p0->data=num;
if (head==NULL)
{
head=p0;
p0->next=NULL;
return head;
}
while (p1->data<p0->data && p1->next)
{
p2=p1;
p1=p1->next;
}
if (p1->data>=p0->data)
{
if (p1==head)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
return head;
}
- C语言程序设计的一些基本问题
- C语言程序设计的一些基本问题[转]
- C/C++的一些基本问题
- c的一些基本语法
- C的一些问题
- C的一些问题
- C语言全局变量一些基本问题
- c-c+ 全局变量的一些细节问题
- c的一些细节问题
- c语言的一些问题
- C指针的一些问题
- pro*c的一些问题
- C指针的一些问题
- C字符串的一些问题
- C的一些基本指针操作
- C的一些基本指针操作
- Pro*C的一些基本操作
- C语言的一些基本算法
- struct timeval结构体 以及 gettimeofday()函数
- JNI返回值方法总结
- ogre1.7.x教程中需要修改的地方。
- 水果的用途大全,喜欢水果的亲们一定要收藏哦!
- 基于DDD以及事件驱动架构(EDA)的应用开发框架enode
- C/C++的一些基本问题
- word从任意页开始加页码
- 马云“吐槽”
- Building Clouds on Server 2012 R2
- Leap Motion:一家将在2013年迎来大爆发的小公司
- 关于别再乱用JQ return false
- javascript 时间日期处理相加,减操作方法js
- Android 4.0 Ice Cream “冰激凌三明治”
- android:minSdkVersion的理解