C语言创建单链表如下:
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"
#include "iostream.h"
typedef struct node
{
int data
node * next
}node , * List
void create(int n)
{
int c
List s,L
L=(List)malloc(sizeof(node))
L->next=NULL
printf("请输入第1个数据:")
scanf("%d",&c)
L->data=c
for(int i=2i<=ni++)
{
s=(List)malloc(sizeof(node))
printf("请输入第%d个数据:",i)
scanf("%d",&c)
s->data=c
s->next=L
L->next =s
}
printf("链表创建成功!")
}
void main()
{
int n
printf("请你输入链表的个数:")
scanf("%d",&n)
create(n)
}
第二个显示为什么?为什么什么东西都没有、#include<stdio.h>
typedef
struct
sample
{
char
ch
struct
sample
*next
}LNode
LNode
*Createlist(LNode
*head)
//创建单链表,头插入法
{
LNode
*p
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf("aply
error\n")
head->next=NULL
head->ch
=
'\0'
int
i=
0
printf("请一次输入A-Z:\n")
for(i
=
0
i
<
26
++i)
{
p=(LNode
*)malloc(sizeof(LNode))
if(!p)
printf("aply
error\n")
scanf("%c",&p->ch)
p->next
=
head->next
head->next=p
}
return
head
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head->next,*r
=
head->next
while(p->next)
{
p
=
p->next
}
int
i
=
0
for(i
=
0
i
<
3
i++)
{
p->next
=
r
p
=
p->next
q
=
q->next
r
=
q
}
p->next
=NULL
head->next
=
q
return
head
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head->next
while(p->next!=NULL)
{
printf("%c\t",p->ch)
p=p->next
}
printf("%c\n",p->ch)
}
int
main(void)
{
LNode
*head
head
=
Createlist(head)//链表初始化
ListPrint(head)
//打印单链表数据
head
=
EncryptList(head)
ListPrint(head)
return
0
}
看看。
#include <stdio.h>#include <stdlib.h>
typedef int DataType
typedef struct node {
DataType member
struct node *next
}*LinkList, *pNode
// 初始化链表
LinkList GetEmptyList() {
LinkList head = (pNode)malloc(sizeof(struct node))
head->member = 0
head->next = NULL
return head
}
// 在非增链表中插入结点
void InsertNode(LinkList head, DataType x) {
pNode p,q
for(p = head p->next != NULL p = p->next) {
if(p->next->member <= x) {
q = (pNode)malloc(sizeof(struct node))
q->member = x
q->next = p->next
p->next = q
return
}
}
q = (pNode)malloc(sizeof(struct node))
q->member = x
q->next = p->next
p->next = q
}
// 新结点插入为首结点
void PushNode(LinkList head, DataType x) {
pNode p = (pNode)malloc(sizeof(struct node))
p->member = x
p->next = head->next
head->next = p
}
// 删除结点
int DeleteNode(LinkList head, DataType x) {
pNode p,q
for(p = head p != NULL p = p->next) {
if(p->next->member == x) {
q = p->next
p->next = q->next
free(q)
return 1 // 成功删除member(第一个)为x的结点
}
}
return 0 // 没有找到member为x的结点
}
// 查找结点
int FindNode(LinkList head, DataType x) {
pNode p
for(p = head->next p != NULL p = p->next) {
if(p->member == x) return 1 // 找到了
}
return 0 // 没有找到
}
// 销毁链表
void DestroyList(LinkList head) {
pNode q,p = head
while(p) {
q = p
p = q->next
free(q)
}
head = NULL
}
// 遍历链表
void ShowList(LinkList head) {
pNode p = head->next
while(p != NULL) {
printf("%d ",p->member)
p = p->next
}
printf("\n")
}
int main() {
DataType x,res
LinkList head = GetEmptyList()
printf("输入一个整数('q' to quit): ")
while(scanf("%d",&x) == 1) {
InsertNode(head, x) // 创建非增链表
printf("输入一个整数('q' to quit): ")
}
fflush(stdin)
ShowList(head)
printf("输入待查找的整数: ")
scanf("%d",&x)
res = FindNode(head, x)
if(res) printf("找到了。\n")
else printf("没找到!\n")
printf("输入待删除的整数: ")
scanf("%d",&x)
res = DeleteNode(head, x)
if(res) printf("成功删除。\n")
else printf("没找到数据为:%d的结点!\n",x)
ShowList(head)
DestroyList(head)
return 0
}