1、结构体的定义
typedef struct student { int num; struct student *next; }student; struct student { int num; struct student *next; }; 这两种定义有什么区别?
答案:
第二个struct student是定义了一个student结构体,这个明白吧。 第一个是用typedef把struct student这个结构体类型名字重新定义为student,
也就是说struct student和student表示同一个事物,都是一个类型的标识符,比如 typedef int zhengshu;
就是你把整型int重命名为zhengshu,下面定义:int i; 和 zhengshu i; 两句就是等价的了
转自百度知道
http://zhidao.baidu.com/link?url=Qee73_2ZgtRRVTiQFcoadYb4o2gHuLKvtKR48USznq-sfNnx9YPzblV1TMF20WI48NBIEFtRNKUBPdQ_7SwDyq
下面实现对链表的各种增删改查操作,单链表的反转比较有意思,链表的反转实际上很容易理解,就是同时设置三个指针,分别指向当前节点,前面的节点,和后面的节点,
原始链表
先实现这个
#include<stdio.h>
#include <iostream>
typedef int status;
typedef int ElementType;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef struct Node
{
int data;
struct Node *next;
} Node;
typedef struct Node * LinkList;
//single list init
//status InitList(linklist *l)
//{
//
//}
LinkList CREAT(int n)
{
ElementType a;
LinkList r,p ,list = NULL;
for(int i=0; i<n; i++)
{
std::cin >> a;
p = (LinkList)malloc(sizeof(Node));
p->data =a;
p->next =NULL;
if( list == NULL)
list =p;
else r->next = p; //new link node insert into the rear
r = p;
}
return (list);
}
int LENGTH(LinkList list)
{
if(list != NULL)
return 1+LENGTH(list->next);
else return 0;
}
int ISEMPTY(LinkList list)
{
return list == NULL;
}
//head node
int ISEMPTYH(LinkList list)
{
return list->next == NULL;
}
LinkList FIND( LinkList list,ElementType item)
{
LinkList p = list;
while(p!=NULL && p->data != item )
p=p->next;
return p;
}
//inset a node before the head
void INSERTLINK()
{
}
//insert a node in the rear
void INSERTLINKR()
{
}
//insert a node after the q
void INSERTLINKAQ(LinkList &list,LinkList q,ElementType item)
{
}
//insert a node after the num i node
void INSERTLINKI()
{
}
//insert an item in an ordered list
void INSERTLINKODR()
{
}
//delete linklist q
void DELLINKLIST()
{
}
//delete list
void DELLIST()
{
}
//delete the data == item
void DELITEM()
{
}
//invert the list
void INVERT(LinkList list)
{
LinkList
p = list,
q=NULL,
r=NULL;
while(p !=NULL)
{
//q = p->next;
//r = q->next;
//q->next = p;
//p = p->next;
//std::cout << q << std::endl;
r = q;
q = p;
p = p ->next;
q->next =r;
//std::cout<<p->data<<std::endl;
}
list =q;
LinkList x=list;
while(x->next !=NULL)
{
std::cout << x->data << std::endl;
x=x->next;
}
}
//
二次循环之后
下面写各种基本操作