导航:首页 > 数据处理 > 双链表数据类型字符型怎么设置

双链表数据类型字符型怎么设置

发布时间:2022-12-31 05:09:36

‘壹’ 双向链表字符串匹配查询设计

#include <stdio.h>
#include <string.h> /*包含字符串比较的函数*/
typedef struct string
{
char *s; /*动态数组*/
int num; /*字符串的长度*/
struct string *next;
struct string *prior;
}
str;
int main(void)
{
char st[50];
int i , j = 0;
str *head,*ps,*lochead;

printf("请依次输入链表中各个节点的字符串。\n");

head = (str *)malloc(sizeof(str));

printf("第%d个节点:",1);

scanf("%s",st);
head->num = strlen(st);
head->s = (char *)malloc( head->num * sizeof(char) );
strcpy( head->s , st);
lochead = head;
for( i = 2 ; i <= 20 ; i++)
{
ps = (str *)malloc(sizeof(str));
lochead->next = ps ;
ps->prior = lochead ;

printf("第%d个节点:",i);

scanf("%s",st);
ps->num = strlen(st);
ps->s = (char *)malloc( ps->num * sizeof(char) );
strcpy( ps->s , st);
lochead = ps;
}
head->prior = lochead ;
lochead->next = head ;
ps = head;
while(ps)
{

printf("请输入要查找的字符串:(输入exit退出)");

scanf("%s",st);
if(!strcmp("exit",st)) return 0;
for( i = 0 ; i < 20 ; i++)
if(strcmp(ps->s,st)) ps = ps->next; /*向后查询*/
else break;
if(i == 20)
{
printf("没有找到该字符串!\n");
}
else
{
printf("找到了该字符串,其地址为%x,本次查询经过了%d个节点!\n",ps,i);
}
ps = head; /*这里可以不要,以后的查询就从上次查询到的那个位置开始*/
}
}

‘贰’ 双向链表实现字符串条件表达式的求值

描述: 给定一个以字符串形式表示的算术表达式,计算该表达式的值。
表达式支持如下运算:“+、-、*、/”,其中“*”和“/”的优先级要高于“+”和“-”;
不需要考虑括号,且表达式之间没有空格;
例如:对于表达式"3-2+15*2",该表达式值为31.
运行时间限制: 60 Sec
内存限制: 256 MByte
输入: 加减乘除四则运算表达式,长度不超过1024字节,运算式中不含有括号和空格。
输出: 表达式的运算结果。
样例输入: 3-2+15*2

样例输出: 31
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct ListNode{
int m_nValue;
char m_nChar;
ListNode* m_pNext;
ListNode* m_pPrev;
};

ListNode* CreatNode(int m_nValue,int m_nChar,ListNode* m_pPrev,ListNode* m_pNext)
{
ListNode* m_pNode=new ListNode();
m_pNode->m_nChar=m_nChar;
m_pNode->m_nValue=m_nValue;
m_pNode->m_pNext=m_pNext;
m_pNode->m_pPrev=m_pPrev;

return m_pNode;
}

void DestroyList(ListNode* pHead)
{
ListNode* pNode=pHead;
while(pNode!=0){
pHead=pNode->m_pNext;
free(pNode);
pNode=pHead;
}
}
int StrCalculate(char* str)
{
if(str==NULL)
return -1;

int tmp=0;
int len;
int i,k=0;

ListNode* head=NULL;
ListNode* p=NULL;
ListNode* q=NULL;
p=q=head=(ListNode*)malloc(sizeof(ListNode));

len=strlen(str);
for(i=0;i<len;i++){
tmp=0;
while(str[i]>='0'&&str[i]<='9'){
tmp=tmp*10+str[i]-'0';
++i;
}
if(k==0){
head=CreatNode(tmp,'0',NULL,NULL);
p=head;
}
else{
q=CreatNode(tmp,'0',p,NULL);
p->m_pNext=q;
p=q;
k++;
}

if(i<len){
q=CreatNode(0,str[i],p,NULL);
p->m_pNext=q;
p=q;
k++;
}
else
break;
}

p=head;
while(p->m_pNext!=NULL){
if(p->m_nChar=='*'){
tmp=p->m_pPrev->m_nValue * p->m_pNext->m_nValue;
p->m_pPrev->m_nValue=tmp;
if(p->m_pNext->m_pNext!=NULL){
p->m_pPrev->m_pNext=p->m_pNext->m_pNext;
p->m_pNext->m_pNext->m_pPrev=p->m_pPrev;
}
else{
p=p->m_pPrev;
p->m_pNext=NULL;
break;
}
}

else if(p->m_nChar=='/'){
tmp=p->m_pPrev->m_nValue/p->m_pNext->m_nValue;
p->m_pPrev->m_nValue=tmp;
if(p->m_pNext->m_pNext!=NULL){
p->m_pPrev->m_pNext=p->m_pNext->m_pNext;
p->m_pNext->m_pNext->m_pPrev=p->m_pPrev;
}
else{
p=p->m_pPrev;
p->m_pNext=NULL;
break;
}
}
p=p->m_pNext;
}

p=head;
while(p->m_pNext!=NULL){
if(p->m_nChar=='+'){
tmp=p->m_pPrev->m_nValue+p->m_pNext->m_nValue;
p->m_pNext->m_nValue=tmp;
}

else if(p->m_nChar=='-'){
tmp=p->m_pPrev->m_nValue-p->m_pNext->m_nValue;
p->m_pNext->m_nValue=tmp;
}
p=p->m_pNext;
}

tmp=p->m_nValue;
printf("the result is : %d\n",tmp);
DestroyList(head);
return tmp;
}

int main()
{
char* m_str="2-3-12/2*4";
StrCalculate(m_str);
return 0;
}

‘叁’ 双向链表问题

没有全看完,只看到你出错的地方,提几点建议:

  1. 乘客姓名不要定义成char类型,应该是char []类型(字符数组),相关的函数参数等也要一并修改。你的错误很有可能就是这个原因造成的(未看完代码,仅推测)。

  2. 创建了head和tail之后,应该把head->next链到tail上,tail->before链接到head上。关联好了以后,再输入新乘客。

‘肆’ 双向链表的节点定义的问题,我是初学者,请帮忙看看,谢谢

1. 这个没有先后顺序,你可以试着将改成这样:
typedef int DataType;//双向链表中数据的类型
typedef struct DNode
{
DataType element;
struct DNode *prior,*next;
}*DoubleList,DNode;//定义双向链表中一个节点
也是可以的,这个的意思就是,定义了一个DNode的结构体类型,和一个DoubleList的结构体指针类型,下次你在使用时,如果这样申明一个变量:DNode node;就表示申明了一个结构体变量,而如果这样:DoubleList douList;就表示申明了一个结构体指针变量。使用时,就将是node.element或douList->element。
2. 这个问题你看前面的typedef的定义,其实就是表示声明自定义数据类型。此声明定义了一个 int 的同义字,名字为 DataType。注意 typedef 并不创建新的类型。它仅仅为现有类型添加一个同义字。你可以在任何需要 int 的上下文中使用 DataType。

‘伍’ 双向表链表

1.
h->name[0]='\0'; // '半角字符
查找函数有问题,找到前提下才输出,
否则打印为找到。
以下是在你编写的基础上改正
2. 第2个你问的有些笼统
若2叉树最简单的办法你建立好
二叉树,然后对其遍历(先,中,后)

最简单的就是采用递归方式建立及遍历
在遍历过程中进行比对查找

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define N 5

typedef struct node
{
char name[20];
struct node *llink,*rlink;
}stud;

stud *creat(int n)
{
stud *p,*h,*s;
int i;
if((h=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("Can not allocate the memory!");
exit(0);
}
h->name[0]='\0';
h->llink=NULL;
h->rlink=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("Can not allocate the memory!");
exit(0);
}
p->rlink=s;
printf("Please input the No.%d name:",i+1);
scanf("%s",s->name);
s->llink=p;
s->rlink=NULL;
p=s;
}
h->llink=s;
p->rlink=h;
return(h);
}

stud *search(stud *h,char *x)
{
stud *p;
char *y;
p=h->rlink;
while(p!=h)
{
y=p->name;
if(strcmp(y,x)==0)
return(p);
else p=p->rlink;
}
return(NULL);
}

void print(stud *h)
{
int n;
stud *p;
p=h->rlink;
printf("The Data is:\n");
while(p!=h)
{
printf("%s ",&*(p->name));
p=p->rlink;
}
printf("\n");
}

int main()
{
int number;
char studname[20];
stud *head,*searchpoint;
number=N;
head=creat(number);
print(head);
printf("Please input the name you want to find:");
scanf("%s",studname);
searchpoint=search(head,studname);
if(searchpoint)
{
printf("what you want to find is :%s",*&searchpoint->name);
}
else
{
printf("Not Found!");
}
system("pause");
return(0);
}

‘陆’ 双向链表流程图

个人理解应该是1,000,000,000,000。每三个零一个逗号。构建一个双向链表,每个链表的节点表示一个3位整数。从输入的字符或字符串生成链表时,可以先把1作为头节点加入,然后把000加入到下一节点,依次类推。两个输入的长整数分别用2个链表来表示。加法运算时,从尾部节点开始运算,两个链表的同一个位置上两个3位整数相加,如果需要进位就带入下一节点。输出可以新生成链表,输出再从头部输出,每三位一个逗号。

‘柒’ C语言单链表改成双链表

#include "stdio.h"
#include <conio.h>
#include <stdlib.h>

struct node /*点链表的数据结构*/
{
char ch;
struct node *next;
struct node *front;
};

int main()
{
struct node *head, *rear; /*head是头指针,指向点链表第一个节点*/
struct node *newnode; /*用于指向新申请的节点*/
struct node *oldnode; /*指向新节点的前一节点*/
char inputch;
int k=0;
while(1==1)
{
inputch=getch();
if(inputch==13)break;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->ch=inputch;

if(k==0)
{
head=newnode;
oldnode=newnode;

head->next = NULL;
head->front = NULL;
}
else
{
oldnode->next=newnode;
newnode->front = oldnode;
}

oldnode=newnode; /*使当前的新节点变成下一次的前一节点*/
k++;
}
oldnode->next = NULL;
rear = oldnode;

newnode=head;
while(newnode!=NULL)
{
printf("%c",newnode->ch);
newnode=newnode->next;
}
printf("\n");
newnode=rear;
while(newnode!=NULL)
{
printf("%c",newnode->ch);
newnode=newnode->front;
}
}

阅读全文

与双链表数据类型字符型怎么设置相关的资料

热点内容
限速超速多久有信息 浏览:619
农贸市场卖什么熟食好吃 浏览:386
松原和辽源哪个职业技术学院好 浏览:10
气体采样数据有哪些标准 浏览:222
方圆代理商利润是多少 浏览:609
受托加工产品怎么处理 浏览:713
天猫如何辨别完美产品真假 浏览:558
哪个专业技术性高 浏览:710
验证机顶盒信息需要多久 浏览:615
同行恶意败坏产品如何处理 浏览:911
俯卧撑一天做多少合适官方数据 浏览:73
版本信息里哪个是屏幕信息 浏览:627
潍坊旧货市场在哪里都卖什么 浏览:147
如何避免产品刺伤 浏览:807
以前收到的信息如何删除 浏览:104
自己配眼镜框需要哪些数据 浏览:983
嘉寓的基本信息是什么 浏览:643
山楂树下如何做代理 浏览:354
信息类是什么样子 浏览:503
酱香型酒代理多少钱 浏览:418