自己写的
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct node //创建数据类型
{
int data;
node *next;
} node, *linklist;
//linklist init() //初始化链表
//{
// node *L;
// L = new node;
// if (L->next == NULL)
// printf("开辟空间失败!");
// L->next = NULL;
//}
//输入数据
linklist linkcreat()
{
node *L;
node *r;
L=new node;
L->next = NULL;
r = L;
int x;
while (~scanf("%d", &x) && x)
{
node *p;
p=new node;
p->data = x;
r->next = p;
r = p;
}
r->next = NULL;
return L;
}
void cha(linklist L, int i)
{
node *p;
p=new node;
p = L->next;
int j = 1;
for (j = 1; j <= i; j++)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
//return p->data;
}
linklist shan(linklist L,int x)
{
node *p,*pre; //pre为前驱结点,p为查找的结点。
p = L->next;
while(p->data != x) //查找值为x的元素
{
pre = p;
p = p->next;
}
pre->next = p->next; //删除操作,将其前驱next指向其后继。
free(p);
return L;
}
void tian(linklist L,int i,int x)
{
node *p;
p=new node;
p=L->next;
for(int j=1;j<i-1;j++)
{
p=p->next;
}
node *q;
q=new node;
q->data=x;
q->next=p->next;
p->next=q;
}
int main()
{
node *gg;
gg=new node;
gg = linkcreat();
shan(gg,3);
//int b=5;
cha(gg,4);
tian(gg,3,9);
cha(gg,5);
//printf("%d\n",);
return 0;
}
别人写的
////////////////////////////////////////////
//单链表的初始化,建立,插入,查找,删除。//
////////////////////////////////////////////
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
typedef int ElemType;
////////////////////////////////////////////
//定义结点类型
typedef struct Node
{
ElemType data; //单链表中的数据域
struct Node *next; //单链表的指针域
} Node,*LinkedList;
////////////////////////////////////////////
//单链表的初始化
LinkedList LinkedListInit()
{
Node *L;
L = new Node; //申请结点空间
if(L == NULL) //判断是否有足够的内存空间
printf("申请内存空间失败/n");
L->next = NULL; //将next设置为NULL,初始长度为0的单链表
}
////////////////////////////////////////////
//单链表的建立1,头插法建立单链表
LinkedList LinkedListCreatH()
{
Node *L;
L = new Node; //申请头结点空间
L->next = NULL; //初始化一个空链表
ElemType x; //x为链表数据域中的数据
while(scanf("%d",&x) != EOF)
{
Node *p;
p = new Node; //申请新的结点
p->data = x; //结点数据域赋值
p->next = L->next; //将结点插入到表头L-->|2|-->|1|-->NULL
L->next = p;
}
return L;
}
////////////////////////////////////////////
//单链表的建立2,尾插法建立单链表
LinkedList LinkedListCreatT()
{
Node *L;
L = new Node; //申请头结点空间
L->next = NULL; //初始化一个空链表
Node *r;
r = L; //r始终指向终端结点,开始时指向头结点
ElemType x; //x为链表数据域中的数据
while(scanf("%d",&x) != EOF)
{
Node *p;
p = new Node; //申请新的结点
p->data = x; //结点数据域赋值
r->next = p; //将结点插入到表头L-->|1|-->|2|-->NULL
r = p;
}
r->next = NULL;
return L;
}
////////////////////////////////////////////
//单链表的插入,在链表的第i个位置插入x的元素
LinkedList LinkedListInsert(LinkedList L,int i,ElemType x)
{
Node *pre; //pre为前驱结点
pre = L;
int tempi = 0;
for (tempi = 1; tempi < i; tempi++)
pre = pre->next; //查找第i个位置的前驱结点
Node *p; //插入的结点为p
p = new Node;
p->data = x;
p->next = pre->next;
pre->next = p;
return L;
}
////////////////////////////////////////////
//单链表的删除,在链表中删除值为x的元素
LinkedList LinkedListDelete(LinkedList L,ElemType x)
{
Node *p,*pre; //pre为前驱结点,p为查找的结点。
p = L->next;
while(p->data != x) //查找值为x的元素
{
pre = p;
p = p->next;
}
pre->next = p->next; //删除操作,将其前驱next指向其后继。
free(p);
return L;
}
/////////////////////////////////////////////
int main()
{
LinkedList list,start;
/* printf("请输入单链表的数据:");
list = LinkedListCreatH();
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("/n");
*/ printf("请输入单链表的数据:");
list = LinkedListCreatT();
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("/n");
int i;
ElemType x;
printf("请输入插入数据的位置:");
scanf("%d",&i);
printf("请输入插入数据的值:");
scanf("%d",&x);
LinkedListInsert(list,i,x);
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("/n");
printf("请输入要删除的元素的值:");
scanf("%d",&x);
LinkedListDelete(list,x);
for(start = list->next; start != NULL; start = start->next)
printf("%d ",start->data);
printf("/n");
return 0;
}