一、目的与要求
1.复习C程序调试、运行的基本操作方法。
2.熟练掌握编辑、编译、连接和运行C 程序的方法。
3.掌握单链表的定义、创建、插入和删除操作程序实现。
二、实验内容
1.定义单链表结构体,获取数据元素。
2.创建链表以输入-1 结束。
3.在第i个节点处插入数据元素X。
4.删除第i个节点。
5.显示链表中的数据元素。
6逆序显示链表中的数据元素
链表节点结构体的定义:
typedef struct Node
{
int data;
struct Node *next;
}Node;
创建节点:
Node *create_Node(int n)
{
Node *node = (Node *)malloc(sizeof(Node));
if(!node)
{
printf("内存不足!\n");
return NULL;
}
node->data = n;
node->next = NULL;
return node;
}
创建单链表:
Node *create_link()
{
int n=0;
Node *head = create_Node(0);
if(!head)
exit(-1);
Node *tail;
tail = head;
scanf("%d", &n);
while(n != -1)
{
Node *pnew = create_Node(n);
if(!pnew)
break;
tail->next = pnew;
tail= pnew;
scanf("%d",&n);
}
printf("Input End\n");
return head;
}
删除单链表:
void freeLink(Node *head)
{
if(head==NULL)
return;
Node *p, *q;
p = head;
while(p->next!=NULL)
{
q = p->next;
p->next = q->next;
free(q);
}
free(head);
}
遍历单链表-----正序输出:
void displayLink(Node *head)
{
if(!head)
return;
Node *p = head->next;
printf("链表正序输出为:\n");
while(p!=NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
遍历单链表-----逆序输出:
void Inverse_displayLink(Node *head)
{
if (!head)
return;
Node *p = head->next;
int count = 0,i;
int a[50] = {0};
printf("链表倒序输出为:\n");
while (p != NULL)
{
a[count] = p->data;
p = p->next;
count++;
}
for (i = count-1; i>=0;i--)
{
printf("%d\n", a[i]);
}
}
在指定位置插入节点:
void insertNode(Node *head,Node *pnew,int i)
{
if(!head)
return;
if(!pnew)
return;
Node *p = head;
int n = 0;
for (n = 0; n < i && p->next != NULL;n++)
{
p = p->next;
}
pnew->next = p->next;
p->next = pnew;
}
在指定位置删除节点:
void deleteNode(Node *head,int i)
{
if(!head)
return;
if(i<=0)
return;
Node *p = head,*q;
int n = 0;
for (n = 0; n < i && p->next != NULL;n++)
{
p = p->next;
}
if(!p)
return;
q = p->next;
p->next = q->next;
free(q);
}
全部代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}Node;
Node *create_Node(int n)
{
Node *node = (Node *)malloc(sizeof(Node));
if(!node)
{
printf("内存不足!\n");
return NULL;
}
node->data = n;
node->next = NULL;
return node;
}
Node *create_link()
{
int n=0;
Node *head = create_Node(0);
if(!head)
exit(-1);
Node *tail;
tail = head;
scanf("%d", &n);
while(n != -1)
{
Node *pnew = create_Node(n);
if(!pnew)
break;
tail->next = pnew;
tail= pnew;
scanf("%d",&n);
}
printf("Input End\n");
return head;
}
void freeLink(Node *head)
{
if(head==NULL)
return;
Node *p, *q;
p = head;
while(p->next!=NULL)
{
q = p->next;
p->next = q->next;
free(q);
}
free(head);
}
void displayLink(Node *head)
{
if(!head)
return;
Node *p = head->next;
printf("链表正序输出为:\n");
while(p!=NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
void Inverse_displayLink(Node *head)
{
if (!head)
return;
Node *p = head->next;
int count = 0,i;
int a[50] = {0};
printf("链表倒序输出为:\n");
while (p != NULL)
{
a[count] = p->data;
p = p->next;
count++;
}
for (i = count-1; i>=0;i--)
{
printf("%d\n", a[i]);
}
}
void insertNode(Node *head,Node *pnew,int i)
{
if(!head)
return;
if(!pnew)
return;
Node *p = head;
int n = 0;
for (n = 0; n < i && p->next != NULL;n++)
{
p = p->next;
}
pnew->next = p->next;
p->next = pnew;
}
void deleteNode(Node *head,int i)
{
if(!head)
return;
if(i<=0)
return;
Node *p = head,*q;
int n = 0;
for (n = 0; n < i && p->next != NULL;n++)
{
p = p->next;
}
if(!p)
return;
q = p->next;
p->next = q->next;
free(q);
}
int main()
{
Node *head = create_link();
displayLink(head);
Inverse_displayLink(head);
printf("插入新节点后的单链表\n");
int n = 0;
scanf("%d", &n);
Node *pnew = create_Node(n);
insertNode(head, pnew, 2);
displayLink(head);
printf("删除指定位置的节点之后的单链表\n");
deleteNode(head, 2);
displayLink(head);
freeLink(head);
system("pause");
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)