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);


  1. /*链表节点逆置:模拟尾插算法 

  2.   在本算法中共使用了两个其他的指针变量空间复杂度是O(1)所以是就地逆置的*/  

  3. void    rever_chain (node *head)  

  4. {  

  5.     node *temp;  

  6.     node *next;  

  7.   

  8.     temp        =   head->next ;  

  9.     /*temp指向首个有数据的节点*/  

  10.     head->next   =   NULL ;  

  11.     /*构造一个头节点:head*/  

  12.     while(temp) {  

  13.         next        =   temp->next ;  

  14.         temp->next   =   head->next ; // 

  15.         head->next   =   temp ;  //head的next是用来当前被断开链表的头结点

  16.         temp        =   next ;  

  17.     }  

  18. }  

另外,在编译时通过,连接时出现: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;

}

原创粉丝点击