本文目录一览:
C语言链表一道题,跪求大神
//删除所有无效数值结点的函数原型:
void deleteneg(PNODE head)
{
PNODE p,q;
p = head;
q = head;
while ( p-next != NULL )
{
if( p-next-data 0)
{
p-next = p-next-next;
p = q;
}
q = p;
p = p-next;
}
}
用C语言编程实现单链表的基本操作
运行结果如下:
完整代码如下:
#includestdio.h
#includestdlib.h
typedef struct LNode
{
char data;
LNode *next;
}* LNodePtr;
LNodePtr CreateList()
{
//初始化头节点
LNodePtr head = (LNodePtr)malloc(sizeof(LNode));
head-data = 0;
head-next = NULL;
LNodePtr tNode;//临时节点
char data;
while(true)
{
scanf("%c",data);
if(data == '\0' || data == '\n' || data == '\r' || data == '\t')
{
continue;
}
if(data == '!')//输入感叹号停止插入节点
{
printf("输入链表元素结束。\n");
break;
}
if(data = 'A' data = 'Z')
{
tNode = (LNodePtr)malloc(sizeof(LNode));
tNode-data = data; /* 数据域赋值 */
tNode-next = head-next;
head-next = tNode;
}
else
{
printf("输入字符需为大写字母。\n");
}
}
return head;
}
/**
加密函数,加密的方式为链接head的所有节点前移offset位,但是不支持offset比链表的节点数多
@param head 链表头节点
@param offset 节点前移的位数
*/
void EncryptList(LNodePtr head,int offset)
{
LNodePtr tNode = head-next;
int i;
for(i = 0; i offset; i++)
{
if(tNode-next != NULL)
{
tNode = tNode-next;
}
}
if(i == offset)
{
LNodePtr newHead = tNode;
LNodePtr tail = tNode;
while (tail-next != NULL)
{
tail = tail-next;
}
tail-next = head-next;
while(tNode-next != NULL)
{
if(tNode-next != newHead)
{
tNode = tNode-next;
}
else
{
tNode-next = NULL;
break;
}
}
head-next = newHead;
}
else
{
printf("不支持移动");
}
}
void ListPrint(LNodePtr head)
{
if(head-next != NULL)
{
LNodePtr tNode = head-next;
while (tNode-next != NULL)
{
printf("%c ",tNode-data);
tNode = tNode-next;
}
printf("%c",tNode-data);
}
}
int main()
{
LNodePtr list = CreateList();
printf("\n创建的链表如下:\n");
ListPrint(list);
EncryptList(list,3);
printf("\n所有节点前移了3位之后的链表如下:\n");
ListPrint(list);
printf("\n");
return 0;
}
如果看不懂代码可以问我
用C语言实现对单链表的基本操作
#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;
}