1.按位序插入 (带头结点)
typedef struct LNode{
ElemType data; // 每个节点存放一个数据元素
struct LNode *next; // 指向下一个节点
}LNode,*LinkList;
//按位序插入 (带头结点)
//在第i个位置插入元素e
bool ListInsert(LinkList &L,int i, ElemType e)
{
if (i<1)
return false;
LNode *p;//指针p指向当前扫描的节点
int j=0;//当前p指向的是第几个节点
p=L;//L指向头结点,头结点是第0个节点 LNode * GetElem(LinkList L,int i)
while (p!=NULL && j<i-1) //循环找到第i-1个结点
{ 7. 按位查找
p=p->next;
j++;
}
if (p==NULL){
return false;
}
LNode *s=(LNode *)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s; // 将节点s连接到p之后
return true;
}
int main()
{ LinkList L;
ListInsert(L,6,8);
return 0;
}
2.按位序插入(不带头结点)
bool ListInsert(LinkList &L,int i, ElemType e)
{
if (i<1)
return false;
if(i==1){
LNode *s=(LNode *)malloc(sizeof(LNode));
s->data=e;
s->next=L;
return true;
}
LNode *p;//指针p指向当前扫描的节点
// int j=0;//当前p指向的是第几个节点
int j=1;//当前p指向的是第几个节点
p=L;//L指向头结点,头结点是第0个节点
while (p!=NULL && j<i-1) //循环找到第i-1个结点
{
p=p->next;
j++;
}
if (p==NULL){
return false; InsertNextNode(p,4) // 引用插入4的函数
}
LNode *s=(LNode *)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s; // 将节点s连接到p之后
return true;
}
3.指定结点的后插操作
bool InsertNextNode(LNode *p,ElemType e){
if (p==NULL){
return false;
}
LNode *s=(LNode *)malloc(sizeof(LNode));
if(s==NULL){// 内存分配失败
return false;
}
s->data=e; // 用节点s保存数据元素e
s->next=p->next;
p->next=s; // 将结点s连接到p之后
return true;
}
4.指定结点的前插操作
bool InsertPriorNode(LNode *p,ElemType e){
if (p==NULL){
return false;
}
LNode *s=(LNode *)malloc(sizeof(LNode));
if(s==NULL){// 内存分配失败
return false;
}
s->next=p->next;
p->next=s;
s->data=p->data;
p->next=e; // 将结点s连接到p之后
return true;
}
5.按位序删除(带头结点)
bool ListDelete(LinkList &L,int i,ElemType &e){
if (i<1)
return false;
LNode *p;
int j=0;
p=L;
while (p!=NULL && j<i-1) //循环找到第i-1个结点
{
p=p->next;
j++;
}
if (p==NULL){
return false;
}
if (p->next==NULL){
return false;
}
LNode *q=p->next;
e=q->data;
p->next=q->next;
free(q);
return true;
}
6.指定结点的删除
bool DeteleNode(LNode *p){
if (p==NULL)
return false;
LNode *q=p->next;
p->data=p->next->data;
p->next=q->next;
free(q);
return true;
}
7.尾插法建立单链表
//单链表尾插法
LinkList tailInsert(LinkList& L)
{
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;
cin >> x;
while (x != 9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
cin >> x;
}
r->next = NULL;
return L;
}
8.头插法
//单链表头插法
LinkList headInsert(LinkList& L)
{
int x; LNode* s;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
cin >> x;
while (x != 9999)
{
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
cin >> x;
}
return L;
}
9.求长度
//单链表 计算单链表长度
int count(LinkList& L)
{
if (L->next == NULL)
return 0;
int count=1;
LNode *p=L->next;
while (p->next)//注意是p指向空,而不是p为空
{
p = p->next; count++;
}
return count;
}
10.单链表创建带头结点
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
# define maxsize 100
typedef int ElemType;
typedef struct node
{
int elem;
struct node *next;
}linelist;
linelist *creat()
{ int m;
linelist *tp;
linelist *head;
head=new linelist;
head->next=NULL;
tp=head;
while(~scanf("%d",&m))
{ if(m==0) break;
linelist *p;
p=new linelist;
p->elem=m;
p->next=tp->next;
tp->next=p;
tp=p;
}
return head;
}
int main()
{ int i,j;
linelist *head;
head=creat();
head=head->next;
while(head!=NULL)
{
printf("%d ",head->elem);
head=head->next;
}
return 0;
}
11.将单链表逆序
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
# define maxsize 100
typedef int ElemType;
typedef struct node
{ int elem;
struct node *next;
}linelist;
linelist *creat()
{ int m;
linelist *tp;
linelist *head;
head=new linelist;
head->next=NULL;
tp=head;
while(~scanf("%d",&m))
{ if(m==0) break;
linelist *p;
p=new linelist;
p->elem=m;
p->next=tp->next;
tp->next=p;
}
return head;
}
int main()
{ int i,j;
linelist *head;
head=creat();
head=head->next;
while(head!=NULL)
{
printf("%d ",head->elem);
head=head->next;
}
return 0;
}
12.建立一个非递减有序单链表
using namespace std;
typedef struct List{
int data;
struct List *next;
}linelist;
void init(linelist *Head,int num){
linelist *tp;
linelist*now;
tp=Head;
now=Head->next;
while(now!=NULL){
if(num<=now->data){
linelist *p;
p=new linelist;
p->data=num;
p->next=now;
tp->next=p;
break;
}
now=now->next;
tp=tp->next;
}
if(!now){
linelist *node;
node=new linelist;
node->data=num;
node->next=NULL;
tp->next=node;
}}
int main()
{ linelist *Head;
Head=new linelist;
Head->next=NULL;
int m;
while(~scanf("%d",&m)&&m!=0){
init(Head,m); }
linelist *p;
p=Head->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next; }
printf("\n");return 0;}
13.两个乱序单链表合并成一个有序单链表
using namespace std;
typedef int ElemType;
typedef struct node
{ int elem;
struct node *next;
}linelist;
linelist *creat()
{ int m;
linelist *tp;
linelist *head;
head=new linelist;
head->next=NULL;
tp=head;
while(~scanf("%d",&m))
{ if(m==0) break;
linelist *p,*pre,*p2;
pre=head;
p=head->next;
while(p!=NULL&&p->elem>m)
{ pre=p;
p=p->next;}
p2=new linelist;
p2->elem=m;
p2->next=p;
pre->next=p2;
}
return head;
}
void print(linelist *head)
{linelist *p;
p=head->next;
while(p!=NULL)
{printf("%d ",p->elem);p=p->next;}
}
Void mergelist(linelist *head,linelist *head1,linelist *Head)
{ linelist *p1,*p2,*Head1;
p1=head->next;
p2=head1->next;
Head=new linelist;
Head1=Head;
while(p1&&p2)
{if(p1->elem>p2->elem)
{Head1->next=p1;
Head1=p1;
p1=p1->next;
}
else {
Head1->next=p2;
Head1=p2;
p2=p2->next;
}}
if(p2!=NULL)
{ Head1->next=p2; }
if(p1!=NULL)
{ Head1->next=p1; }
print(Head);}
int main()
{linelist *p;
linelist *head,*head1;
head=new linelist;
head1=new linelist;
linelist *Head;
head=creat();
head1=creat();
mergelist(head,head1,Head);
return 0;
}
14.将一个链表分解成偶数连与奇数连
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
typedef struct student
{
int data;
struct student *next;
}stu;
void creat(stu *a,stu *b)
{int m;
struct student *p1,*p2,*p3,*p4;
p1=a;p3=b;
while(~scanf("%d",&m)&&m!=0)
{if(m%2!=0)
{p2=(stu*)malloc(sizeof(stu));
p2->data=m;
p1->next=p2;p1=p2;
}
else if(m%2==0)
{p4=(stu*)malloc(sizeof(stu));
p4->data=m;
p3->next=p4;p3=p4;
}
}
p1->next=NULL;
p3->next=NULL;
}
void pr(stu *a)
{stu *b;
b=a->next;
while(b!=NULL)
{printf("%d ",b->data);
b=b->next;
}
}
int main()
{int i,n;
stu *d,*b;
d=(stu*)malloc(sizeof(stu));
d->next=NULL;
b=(stu*)malloc(sizeof(stu));
b->next=NULL;
creat(d,b);
pr(d);
printf("\n");
pr(b);
return 0;
}
带头结点的前插
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
/*
//创造一个链表(带头结点的前插)
LNode *CreatLinkList(){
LinkList L;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
int x,n;
scanf("%d",&n);
int i=1;
while(i<=n){
scanf("%d",&x);i++;
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=L->next;
L->next=s;
}
LNode *p=L->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
return L;
}
int main()
{
LinkList L;
L=CreatLinkList();
return 0;
}
*/
//创造一个链表
LNode *CreatLinkList(LinkList &L,int x){
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=L->next;
L->next=s;
return L;
}
int main()
{
LinkList L;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
int x,n;
scanf("%d",&n);
int i=1;
while(i<=n){
scanf("%d",&x);i++;
L=CreatLinkList(L,x);
}
LNode *p=L->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
return 0;
}
带头结点的尾插
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
//创造一个链表(带头结点的尾插)
LNode *CreatLinkList(){
LinkList L;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
LinkList p;
p=L;
int x,n;
scanf("%d",&n);
int i=1;
while(i<=n){
scanf("%d",&x);i++;
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=p->next;
p->next=s;
p=s;
}
LNode *q=L->next;
while(q!=NULL){
printf("%d ",q->data);
q=q->next;
}
return L;
}
int main()
{
LinkList L;
L=CreatLinkList();
return 0;
}
不带头结点的尾插
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
//创造一个链表(不带头结点的尾插)
LNode *CreatLinkList(){
LinkList L;
L=(LNode*)malloc(sizeof(LNode));
L=NULL;
LinkList p;
p=L;
int x,n;
scanf("%d",&n);
int i=1;
while(i<=n){
scanf("%d",&x);i++;
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
if(p==NULL){
L=s;
p=s;
}
else{
p->next=s;
p=s;
s->next=NULL;
}
}
LNode *q=L;
while(q!=NULL){
printf("%d ",q->data);
q=q->next;
}
return L;
}
int main()
{
LinkList L;
L=CreatLinkList();
return 0;
}
不带头结点的头插
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
//创造一个链表(不带头结点的头插)
LNode *CreatLinkList(){
LinkList L;
L=(LNode*)malloc(sizeof(LNode));
L=NULL;
int x,n;
scanf("%d",&n);
int i=1;
while(i<=n){
scanf("%d",&x);i++;
LNode *s;
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=L;
L=s;
}
LNode *q=L;
while(q!=NULL){
printf("%d ",q->data);
q=q->next;
}
return L;
}
int main()
{
LinkList L;
L=CreatLinkList();
return 0;
}