用C语言编程实现单链表的基本操作
发布网友
发布时间:2022-04-22 11:49
我来回答
共4个回答
热心网友
时间:2023-11-03 17:03
运行结果如下:
完整代码如下:
#include<stdio.h>
#include<stdlib.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;
}
如果看不懂代码可以问我
热心网友
时间:2023-11-03 17:04
第二个显示为什么?为什么什么东西都没有、
#include<stdio.h>
typedef struct sample
{
char ch;
struct sample *next;
}LNode;
LNode *Createlist(LNode *head) //创建单链表,头插入法
{
LNode *p;
if((head=(LNode *)malloc(sizeof(LNode)))==NULL)
printf("aply error\n");
head->next=NULL;
head->ch = '\0';
int i= 0;
printf("请一次输入A-Z:\n");
for(i = 0 ; i < 26; ++i)
{
p=(LNode *)malloc(sizeof(LNode));
if(!p)
printf("aply error\n");
scanf("%c",&p->ch);
p->next = head->next;
head->next=p;
}
return head;
}
LNode *EncryptList(LNode* head)
{
LNode *p= head,*q = head->next,*r = head->next;
while(p->next)
{
p = p->next;
}
int i = 0 ;
for(i = 0 ; i < 3; i++)
{
p->next = r;
p = p->next;
q = q->next;
r = q;
}
p->next =NULL;
head->next = q;
return head;
}
void ListPrint(LNode *head)
{
LNode *p = head->next;
while(p->next!=NULL)
{
printf("%c\t",p->ch);
p=p->next;
}
printf("%c\n",p->ch);
}
int main(void)
{
LNode *head;
head = Createlist(head);//链表初始化
ListPrint(head); //打印单链表数据
head = EncryptList(head);
ListPrint(head);
return 0;
}
看看。
热心网友
时间:2023-11-03 17:04
#include <stdio.h>
typedef struct node{
char key;
struct node* next;
}Node;
Node* CreateList() //逆序创建一个链表
{
Node* head = (Node*)malloc(sizeof(Node));
Node* p = NULL;
Node* q = NULL;
int i = 0;
char data = 0;
for(i = 0; i < 26; i++)
{
while((data = getchar()) != (i+65) );
if(i == 0)
{
p = (Node*)malloc(sizeof(Node));
p->key = data;
p->next = NULL;
}
else
{
q = (Node*)malloc(sizeof(Node));
q->key = data;
q->next = p;
p = q;
}
}
head->next = q;
return head;
}
void EncryptList(Node* head) //对链表元素进行移位,你是想加密?
{
Node* p = NULL;
if(head == NULL || head->next == NULL)
return ;
p = head->next;
while(p)
{
p->key += 3;
p = p->next;
}
}
void ListPrint(Node* head) //显示所有链表元素
{
Node* p = NULL;
if(head == NULL || head->next == NULL)
return ;
p = head->next;
while(p)
{
printf("%c ", p->key);
p = p->next;
}
}
void DestroyList(Node** headp) //销毁链表
{
Node* p = *headp;
Node* q = NULL;
while(p)
{
q = p->next;
free(p);
p = q;
}
*headp = NULL;
}
int main(void) //主函数
{
Node* head = NULL;
head = CreateList();
ListPrint(head);
EncryptList(head);
ListPrint(head);
DestroyList(&head);
return 0;
}
//没有测试,找到bug给我说一声
热心网友
时间:2023-11-03 17:05
这个教科书上很多的。纯属支持楼上。