`
v5qqcom
  • 浏览: 1286704 次
文章分类
社区版块
存档分类
最新评论

单链表的创建、插入、删除、倒置操作

 
阅读更多

/*-----------------------------------------------------*/
/*--------------单链表的创建、插入、删除、倒置操作-----------*/
/*--------------Written by redfire250-----2005.5.10----*/
/*-----------------------------------------------------*/

#include<malloc.h>
#include<stdio.h>
#define null 0
struct student
{
long Number;
char Name[20];
long Score;
struct student *Next;
};

int n=0;/*n为全局变量,用来计算链表的结点个数*/

/*-----------------------------------------*/
/*--------------创建结点函数Creat()--------*/
/*-----------------------------------------*/
struct student *Creat()
{

struct student *p1;
struct student *p2;
struct student *head=null;
p1=p2=(struct student *)malloc(sizeof(struct student));/*开辟一段可用内存单元*/
printf("please input the student's Number Name and the Score:/n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);

while(p2->Number!=0)
{
n++;

if(n==1) /*是否开辟的是第一个结点*/
head=p2;
else
p1->Next=p2;

p1=p2;
p2=(struct student *)malloc(sizeof(struct student));
printf("Input the Number the Name and the Score:/n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
}
p1->Next=null;
return(head);
}


/*------------------------------------------*/
/*--------------查看链表内容函数View()------*/
/*------------------------------------------*/
View(struct student *head)
{
struct student *p;
p=head;
while(p->Next!=null)
{
printf("%ld %s %ld/n",p->Number,p->Name,p->Score);
p=p->Next;
}
printf("%ld %s %ld/n",p->Number,p->Name,p->Score);
}

/*-------------------------------------------------*/
/*--------------插入结点函数(前插)Insert()-------*/
/*-------------------------------------------------*/
Insert(struct student *head,int Num) /*head为链表头指针,Num插入链表位置*/
{
int t=1;
struct student *p1,*p2;
p1=head;
if (Num>n||Num<0)
{
printf("input error!!!/n");
return 0;
}

while(t<Num-1) /*找到要插入结点的前一个结点*/
{
p1=p1->Next;
t++;
}
p2=(struct student *)malloc(sizeof(struct student));
printf("Input the Number the Name and the Score:/n");
scanf("%ld%s%ld",&p2->Number,p2->Name,&p2->Score);
p2->Next=p1->Next;
p1->Next=p2;
n++;

}


/*------------------------------------------*/
/*------------ 删除结点函数Delnode()--------*/
/*-----------------------------------------*/
Delnode(struct student *head,int node)
{
int t=1;
struct student *p1,*p2;
p2=head;
if (node>n||node<1)
{
printf("error!!! The node is not exist!");
return 0;
}
while(t<node-1) /*找到要删除结点的前一个结点*/
{
p2=p2->Next;
t++;
}
p1=p2->Next->Next; /*找到要删除结点的后一个结点*/
free(p2->Next); /*释放要删除的结点空间(删除)*/
p2->Next=p1; /*前一结点指向后一结点*/
n--;
}

/*-------------------------------------------------*/
/*--------------逆序重组链表Invert()-------*/
/*-------------------------------------------------*/
struct student *Invert(struct student *head)
{
struct student *p1,*p2;
p1=head;
p2=p1->Next;
head=p2->Next;
p1->Next=null;
while(head->Next!=null)
{
p2->Next=p1;
p1=p2;
p2=head;
head=head->Next;
}
head->Next=p2;
p2->Next=p1;
return head;
}

main()
{
int number1,number2;
struct student *head;
head=Creat();
View(head);

printf("the n that you want to insert:/n");
scanf("%d",&number1);
Insert(head,number1);
View(head);

printf("the node that you want to DELETE:/n");
scanf("%d",&number2);
Delnode(head,number2);
View(head);

printf("Inverte the list:/n");
View(Invert(head));

getch();
}

分享到:
评论

相关推荐

    单链表的操作 创建 删除 排序 求长度 倒置

    单链表的操作 包括创建 删除 排序 求长度 倒置

    单链表代码.rar

    06数据结构-单链表的存储结构,07数据结构-单链表的创建与遍历,08数据结构-单链表的查找和插入,09数据结构-单链表的删除和倒置,10数据结构-单链表的有序插入和排序,用C语言在Linux下写的,可以快速移植到各平台

    C/C++单链表的一些操作

    基于C/c++实现的链表操作,用于学习数据结构。

    单链表的各种操作,包括使用二级指针创建单链表

    使用二级指针创建单链表,程序功能菜单如图所示void showMenu(){ printf("\t\t\t\t\t\t1.使用头插法创建链表\n"); printf("\t\t\t\t\t\t2.使用尾插法创建链表\n"); printf("\t\t\t\t\t\t3.按序号查找\n"); ...

    带头结点链表的各种操作(c语言)

    /*把带头结点的单链表倒置(以结点形式 )*/ node *Dao_zhi(node *head) { node *p,*s; p=head-&gt;next; head-&gt;next=NULL; while(p) { s=p; p=p-&gt;next; s-&gt;next=head-&gt;next; head-&gt;next=s; } ...

    C程序范例宝典(基础代码详解)

    实例093 创建顺序表并插入元素 123 实例094 向链表中插入结点 125 实例095 从链表中删除结点 126 实例096 合并两个链表 129 实例097 单链表就地逆置 130 实例098 头插入法建立单链表 132 3.3 栈和队列...

    C语言通用范例开发金典.part2.rar

    1.1.1 一维数组的倒置 2 范例1-1 一维数组的倒置 2 ∷相关函数:fun函数 1.1.2 一维数组应用 3 范例1-2 一维数组应用 3 1.1.3 一维数组的高级应用 5 范例1-3 一维数组的高级应用 5 1.1.4 显示杨辉三角 7 ...

    C语言通用范例开发金典.part1.rar

    1.1.1 一维数组的倒置 2 范例1-1 一维数组的倒置 2 ∷相关函数:fun函数 1.1.2 一维数组应用 3 范例1-2 一维数组应用 3 1.1.3 一维数组的高级应用 5 范例1-3 一维数组的高级应用 5 1.1.4 显示杨辉三角 7 ...

    C 开发金典

    1.1.1 一维数组的倒置 2 范例1-1 一维数组的倒置 2 ∷相关函数:fun函数 1.1.2 一维数组应用 3 范例1-2 一维数组应用 3 1.1.3 一维数组的高级应用 5 范例1-3 一维数组的高级应用 5 1.1.4 显示杨辉三角 7 ...

    数据结构实验.zip

    实现功能:采用结构化程序设计思想,编程实现客房管理程序的各个功能函数,从而熟练掌握单链表的创建、输出、查找、修改、插入、删除、排序和复杂综合应用等操作的算法实现。以带表头结点的单链表为存储结构,实现...

Global site tag (gtag.js) - Google Analytics