内容包括链表的创建,增加、删除节点,链表的逆序、排序和销毁等。
#include
#include
typedef struct node
{
int data;
node* pNext;
}Node;
//链表的操作,以有头节点为例,无头节点类似
Node* head = NULL;
//创建链表,头结点data=0,pNext=NULL;
bool createNodeList()
{
head = (Node*) malloc(sizeof(Node));
if(NULL == head)
{
return false;
}
else
{
head->data = 0;
head->pNext = NULL;
return true;
}
}
//增加节点
bool addNode(Node* node)
{
if(NULL == head)
{
return false;
}
Node* p = head->pNext;
Node* q = head;
while(NULL != p)
{
q = p;
p = p->pNext;
}
q->pNext = node;
node->pNext = NULL;
return true;
}
//删除节点
bool deleteNode(int index)
{
if(NULL == head)
{
return false;
}
Node* p = head->pNext;
int length = 0;
while(NULL != p)
{
length ++;
p = p->pNext;
}
if(length < index)
{
return false;
}
else
{
Node* q = head;
p = head;
for(int i=0;i{
q = p;
p = p->pNext;
}
Node* t = p->pNext;
q->pNext = t;
free(p);
return true;
}
}
//逆序
void reverseNodeList()
{
if(NULL == head)
{
return;
}
//如果链表长度为1
if(head->pNext == NULL)
{
return;
}
Node* p = head->pNext;
Node* q = p->pNext;
Node* t = NULL;
while(NULL != q)
{
t = q->pNext;
q->pNext = p;
p = q;
q = t;
}
head->pNext->pNext = NULL;
head->pNext = p;
}
//排序(降序)
void sort()
{
//冒泡排序
Node* pHead = head;
if(head == NULL)
{
return;
}
if(pHead->pNext == NULL)
{
return;
}
Node* pi = pHead->pNext;
Node* pj = pi->pNext;
for(;pi != NULL;pi=pi->pNext)
{
for(pj = pi->pNext;pj != NULL;pj=pj->pNext)
{
if(pj->data>pi->data)
{
int tmp = pj->data;
pj->data = pi->data;
pi->data = tmp;
}
}
}
}
//销毁
void destroyNodeList()
{
if(NULL == head)
{
return;
}
if(NULL == head->pNext)
{
free(head);
head = NULL;
return;
}
Node* p = head->pNext;
while(NULL != p)
{
Node* tmp = p;
p = p->pNext;
free(tmp);
}
free(head);
head = NULL;
}
void main()
{
createNodeList();
Node* node1 = (Node*)malloc(sizeof(Node));
node1->data = 1;
node1->pNext = NULL;
Node* node2 = (Node*)malloc(sizeof(Node));
node2->data = 2;
node2->pNext = NULL;
addNode(node1);
addNode(node2);
reverseNodeList();
Node* node3 = (Node*)malloc(sizeof(Node));
node3->data = 3;
node3->pNext = NULL;
addNode(node3);
sort();
deleteNode(2);
destroyNodeList();
}
我有一个完整的代码给你。如附件
更多技术文章可以关注我的微博,名字:成都睿尔科技
具体代码如下:
//------------------------------( 链表 )-------------------------------------
#include
#include
#define SUCCESS 1
#define FLASE 0
typedef struct node
{
int iData;
struct node *pNext;
}Node,*LinkList;
//初始化链表
//返回类型为指向结构体的指针
LinkList Init_LinkList(LinkList pHead)
{
pHead=NULL;
return pHead;
}
//返回链表长度
int Get_Length(LinkList pHead)
{
int iLength = 0;
Node* pFlag = NULL;
pFlag = pHead;
while(pFlag!=NULL)
{
iLength++;
pFlag=pFlag->pNext;
}
return iLength;
}
//插入结点
//成功插入返回链表头指针,不成功提示错误,返回头指针
//pHead:结点头指针
//iLocate:位置
//iData:数据
LinkList Inset_LinkList(LinkList pHead , int iLocate , int iData)
{
int iCount = 0;
Node* pNode = NULL;
Node* pFlag = NULL;
pNode = (Node* )malloc(sizeof(pNode));
if(iLocate==1)
{
pNode->iData = iData;
pNode->pNext = pHead;
pHead = pNode;
return pHead;
}
else if(iLocate<=(Get_Length(pHead)+1))
{
pFlag = pHead;
for ( iCount=1; iCountpFlag = pFlag->pNext;
pNode->iData = iData;
pNode->pNext = pFlag->pNext;
pFlag->pNext = pNode;
printf("Insert Success\n");
return pHead;
}
else
{
free(pNode);
printf("Locate is Error.Insert Failre\n");
return pHead;
}
}
//根据数据删除结点
//iData为要删除的结点
LinkList Delete_Data_LinkList(LinkList pHead,int iData)
{
LinkList pFlag1,pFlag2;
pFlag1 = pHead;
pFlag2 = pHead;
if(pHead==NULL)
{
printf("del a empty LinkList\n");
return pHead;
}
if(pHead->iData==iData)
{
pHead=pFlag1->pNext;
// free(pFlag1);
printf("Del Succuess\n");
return pHead;
}
pFlag1=pFlag1->pNext;
while(pFlag1->pNext!=NULL)
{
if(pFlag1->iData==iData)
{
pFlag2->pNext=pFlag1->pNext;
// free(pFlag1);
printf("Del Success");
return pHead;
}
else
{
pFlag1=pFlag1->pNext;
pFlag2=pFlag2->pNext;
}
}
printf("no such data\n");
return pHead;
}
//根据位置删除结点
//iLocate为需要删除的位置
LinkList Delete_Locate_LinkList(LinkList pHead,int iLocate)
{
LinkList pFlag=pHead;
int iCount;
if(pFlag==NULL)
{
printf("the LinkList is empty\n");
return pHead;
}
else if(iLocate==1)
{
pHead=pHead->pNext;
printf("test1\n");
printf("del Success");
// free(pFlag);
return pHead;
}
else if (iLocate <= Get_Length(pHead))
{
for ( iCount=1; iCount<=iLocate-2; iCount++)
{
pFlag = pFlag->pNext;
}
pFlag->pNext = pFlag->pNext->pNext;
// free(pFlag->pNext);
printf("Del Success");
return pHead;
}
else
{
printf("Locate error\n");
return pHead;
}
}
//输入数据函数
int Input_iData(void)
{
int iData;
printf("\nplease input data : ");
scanf("%d",&iData);
return iData;
}
//输入位置函数
int Input_iLocate(void)
{
int iLocate;
printf("\nplease input Locate : ");
scanf("%d",&iLocate);
if (iLocate>0)
return iLocate;
else
{
printf("Locate Error.\n ");
return FLASE;
}
}
//打印所有结点
int Printf_Node(LinkList pHead)
{
LinkList pNode;
pNode=pHead;
while(pNode!=NULL)
{
printf("%d\t",pNode->iData);
pNode = pNode->pNext;
}
return SUCCESS; //成功返回SUCCESS
}
void Run_LinkList(void)
{
int iChoose;
int bRun;
bRun = 1;
//LinkList temp;
LinkList pHead=NULL;
// pHead = Init_LinkList(pHead);
while (bRun)
{
printf("1.Input data into LinkList.\n");
printf("2.Delete data form LinkList by locate.\n");
printf("3.Delete data form LinkList by data.\n");
printf("4.Printf all data form LinkList.\n");
printf("5.Quit Program.\n\n");
printf("Please choose your mind : ");
scanf("%d",&iChoose);
printf("\n");
switch (iChoose)
{
case 1 : pHead=Inset_LinkList(pHead,Input_iLocate(),Input_iData());
break;
case 2 : pHead=Delete_Locate_LinkList(pHead,Input_iLocate());
break;
case 3 : pHead=Delete_Data_LinkList(pHead,Input_iData());
break;
case 4 : Printf_Node(pHead);
break;
case 5 : bRun = 0;
break;
default :printf("Your choose maybe is error,please try again.\n");
break;
}
printf("\n\n");
getchar();
}
}
int main(void)
{
Run_LinkList();
return 0;
}
//创建一个单链表
#include
#include
typedef struct lnode * pointer;
struct lnode
{
int data;
struct lnode * next;
};
typedef pointer lklist;
void create(lklist &l)
{
int x;
lklist r;
l=(lklist)malloc(sizeof(struct lnode));
r=l;
printf("Input the elemment: ");
scanf("%d", &x);//输入元素
while(x != 0) //以0作为结束标志
{
lklist s;
s=(pointer)malloc(sizeof(struct lnode));
s->data=x;
r->next=s;
r=s;
scanf("%d", &x);}
r->next=NULL;
}
void output(lklist l) //输出单链表的元素
{
lklist p;
p=l->next;
while(p!=NULL)
{
printf("%d ", p->data);
p=p->next;
}
}
#include
using namespace std;
#include
typedef struct Lnode{//定义单链表结点
int data;
struct Lnode *next;
}Lnode;
void createlistT(Lnode *&C,int a[],int n){//用尾插法建立单链表
int i;
Lnode *s,*r;
C=(Lnode*)malloc(sizeof(Lnode));
C->next=NULL;
r=C;
for(i=0;i
s=(Lnode*)malloc(sizeof(Lnode));
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void createlistH(Lnode *&C,int a[],int n){//用头插法建立单链表
int i;
Lnode *s;
C=(Lnode*)malloc(sizeof(Lnode));
C->next=NULL;
for(i=0;i
s=(Lnode*)malloc(sizeof(Lnode));
s->data=a[i];
s->next=C->next;
C->next=s;
}
}
void output(Lnode *L)//输出单链表中内容
{
Lnode *p;
p=L->next;
while(p!=NULL)
{
cout<< p->data<
p=p->next;
}
}
int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
Lnode *L;//定义一个头指针
createlistH(L,a,10);
output(L);
createlistT(L,a,10);
output(L);
return 0;
}
用了头插 尾插进行简单输入输出