C语言各种链表操作(创建、打印、删除、插入、反转)

来源:互联网 发布:怎么查看telnet端口号 编辑:程序博客网 时间:2024/05/14 16:07

#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;
}


运行结果:


0 0