C语言各种链表操作(创建、打印、删除、插入、反转)
2014-04-03 18:57
686 查看
#include <stdio.h>
#include <stdlib.h>
#define STU_NUM 4
//define a student struct variable
struct Student{
int num;
char name[20];
float score;
struct Student* next;
};
//Create function: creat a new list
//Return: the struct Student list header
struct Student* creatlist(int n)
{
int i = 0;
struct Student *head=NULL;
struct Student *p1=NULL, *p2=NULL;
for (i = 1; i <= n; ++i)
{
p2 = (struct Student*)malloc(sizeof(struct Student));
scanf_s("%ld %s %f",&p2->num,p2->name,20,&p2->score);
if (1 == i)
{
head = p2;
p1 = p2;
}
else
{
p1->next = p2;
p1 = p2;
}
}
p1->next = NULL;
return (head);
}
void printlist(struct Student* head)
{
struct Student *p;
p = head;
while (p != NULL)
{
printf("\nNum:%-ld\tName:%-10s\tscore:%-5.2f\n",p->num,p->name,p->score);
p = p->next;
}
}
//delete one member from the list
//return deleted list
struct Student* deletlist(struct Student *head, int n)
{
int i = 0;
struct Student* p = NULL;
struct Student* d = NULL;
p = head;
if (head == NULL)
{
return (struct Student*)-1;//the list is null!
}
if ((head != NULL) && (1 == n))
{ //if delete head list
d = p;
p = p->next;
free(d);
//*head = *p;
//此处非常关键: 指针做形参(parameter)时,只有当改变了指针的内容时 \
//才会改变实参(argument)的值!!!!!!!
//head = p;
//这样是永远也改变不了实参的值的!!
}
else
{
for (i = 1; i < n - 1; ++i)
{//find the number of n-1 member
if (p != NULL)
{
p = p->next;
}
else
return (struct Student*)-1;//error
}
d = p->next; //find the number n member
if (p->next != NULL)
{
p->next = p->next->next;
//*head = *p;
free(d);
}
else
return (struct Student*)-1;//the number n is not exist
}
return (p);
}
//function realize insert a element into a list
//return the inserted new list
struct Student *insertlist(struct Student* head, struct Student* stu, int n)
{
int i = 0;
struct Student *pMove = NULL;
struct Student *pTemp = NULL;
pMove = head;
pTemp = stu;
if (NULL == head)
{
return (struct Student*)-1;//the list is null!
}
if (1 == n)
{//insert struct Student stu before the list head
pTemp->next = pMove;
return (pTemp);
}
else
{
for (i = 1; i < n - 1; ++i)
{//to find the number n-1 element
if (pMove != NULL)
pMove = pMove->next;
else
return (struct Student*)-1;//n is beyond the list number
}
if (pMove->next != NULL)
{//the insert position is not the rear of list
pTemp->next = pMove->next;
pMove->next = pTemp;
}
else
{
pMove->next = pTemp;
pTemp->next = NULL;
}
return (head);
}
}
//Realize the function of revease the list
struct Student* reverselist(struct Student* head)
{
int nCount = 0;
struct Student *plast = NULL;
struct Student *pPenult = NULL;
struct Student *newHead = NULL;
if (NULL == head->next)
{//only one element in list
return head;
}
pPenult = head;
while (head->next)
{
//traverse to go the last but one elements
while (pPenult->next->next != NULL)
{
pPenult = pPenult->next;
}
plast = pPenult->next;//find the last element of list
plast->next = pPenult;//reverse the last two list
pPenult->next = NULL;//unlock the circle list otherwise: plast->next = pPenult; pPenult->next = last
nCount++;
if (1 == nCount)
newHead = plast;
pPenult = head;
continue;
}
nCount = 0;
return newHead;
}
int main()
{
struct Student* result = NULL;
struct Student stu = {1007,"Song",76.5};
struct Student* pt;
pt = creatlist(STU_NUM);
printf("After creat a new list:\n");
printlist(pt);
result = deletlist(pt, 1);
printf("After delete one element in list:\n");
printlist(result);
pt = insertlist(result, &stu, 4);
printf("After reinsert one element in list:\n");
printlist(pt);
result = reverselist(pt);
printf("After reverse the list:\n");
printlist(result);
return 0;
}
运行结果:
#include <stdlib.h>
#define STU_NUM 4
//define a student struct variable
struct Student{
int num;
char name[20];
float score;
struct Student* next;
};
//Create function: creat a new list
//Return: the struct Student list header
struct Student* creatlist(int n)
{
int i = 0;
struct Student *head=NULL;
struct Student *p1=NULL, *p2=NULL;
for (i = 1; i <= n; ++i)
{
p2 = (struct Student*)malloc(sizeof(struct Student));
scanf_s("%ld %s %f",&p2->num,p2->name,20,&p2->score);
if (1 == i)
{
head = p2;
p1 = p2;
}
else
{
p1->next = p2;
p1 = p2;
}
}
p1->next = NULL;
return (head);
}
void printlist(struct Student* head)
{
struct Student *p;
p = head;
while (p != NULL)
{
printf("\nNum:%-ld\tName:%-10s\tscore:%-5.2f\n",p->num,p->name,p->score);
p = p->next;
}
}
//delete one member from the list
//return deleted list
struct Student* deletlist(struct Student *head, int n)
{
int i = 0;
struct Student* p = NULL;
struct Student* d = NULL;
p = head;
if (head == NULL)
{
return (struct Student*)-1;//the list is null!
}
if ((head != NULL) && (1 == n))
{ //if delete head list
d = p;
p = p->next;
free(d);
//*head = *p;
//此处非常关键: 指针做形参(parameter)时,只有当改变了指针的内容时 \
//才会改变实参(argument)的值!!!!!!!
//head = p;
//这样是永远也改变不了实参的值的!!
}
else
{
for (i = 1; i < n - 1; ++i)
{//find the number of n-1 member
if (p != NULL)
{
p = p->next;
}
else
return (struct Student*)-1;//error
}
d = p->next; //find the number n member
if (p->next != NULL)
{
p->next = p->next->next;
//*head = *p;
free(d);
}
else
return (struct Student*)-1;//the number n is not exist
}
return (p);
}
//function realize insert a element into a list
//return the inserted new list
struct Student *insertlist(struct Student* head, struct Student* stu, int n)
{
int i = 0;
struct Student *pMove = NULL;
struct Student *pTemp = NULL;
pMove = head;
pTemp = stu;
if (NULL == head)
{
return (struct Student*)-1;//the list is null!
}
if (1 == n)
{//insert struct Student stu before the list head
pTemp->next = pMove;
return (pTemp);
}
else
{
for (i = 1; i < n - 1; ++i)
{//to find the number n-1 element
if (pMove != NULL)
pMove = pMove->next;
else
return (struct Student*)-1;//n is beyond the list number
}
if (pMove->next != NULL)
{//the insert position is not the rear of list
pTemp->next = pMove->next;
pMove->next = pTemp;
}
else
{
pMove->next = pTemp;
pTemp->next = NULL;
}
return (head);
}
}
//Realize the function of revease the list
struct Student* reverselist(struct Student* head)
{
int nCount = 0;
struct Student *plast = NULL;
struct Student *pPenult = NULL;
struct Student *newHead = NULL;
if (NULL == head->next)
{//only one element in list
return head;
}
pPenult = head;
while (head->next)
{
//traverse to go the last but one elements
while (pPenult->next->next != NULL)
{
pPenult = pPenult->next;
}
plast = pPenult->next;//find the last element of list
plast->next = pPenult;//reverse the last two list
pPenult->next = NULL;//unlock the circle list otherwise: plast->next = pPenult; pPenult->next = last
nCount++;
if (1 == nCount)
newHead = plast;
pPenult = head;
continue;
}
nCount = 0;
return newHead;
}
int main()
{
struct Student* result = NULL;
struct Student stu = {1007,"Song",76.5};
struct Student* pt;
pt = creatlist(STU_NUM);
printf("After creat a new list:\n");
printlist(pt);
result = deletlist(pt, 1);
printf("After delete one element in list:\n");
printlist(result);
pt = insertlist(result, &stu, 4);
printf("After reinsert one element in list:\n");
printlist(pt);
result = reverselist(pt);
printf("After reverse the list:\n");
printlist(result);
return 0;
}
运行结果:
相关文章推荐
- c语言链表基本操作(带有创建链表 删除 打印 插入)
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C-链表的一些基本操作【创建-删除-打印-插入】
- 线性表的链式存储格式基本操作:创建链表、插入、删除、查找、求表长、打印链表
- java语言编写链表的基本操作(链表的创建,插入,删除,打印,排序)
- 链表基本操作(创建,插入,查找,删除)-C语言
- C语言单向链表的创建、释放、插入、删除、翻转操作练习
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 单循环链表-创建、插入、删除、反转等操作
- 单链表创建插入删除打印反转
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 链表的基本操作,创建、删除、插入、反转
- 链表操作综合练习(创建、拆分、插入、删除、合并、打印、逆置)
- 关于链表结构的基本操作 c 实现 (创建,插入删除,反转,合并链表,查找,是否有环,链表相交情况)
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- c++之链表篇1:单向链表的创建,打印,删除,插入,销毁等基本操作
- 链表操作--创建--删除--插入--打印
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 常见算法:C语言中链表的操作(创建,插入,删除,输出)