永发信息网

帮我解决下链表问题!

答案:3  悬赏:80  手机版
解决时间 2021-05-04 20:55
  • 提问者网友:ミ烙印ゝ
  • 2021-05-04 09:31

C语言链表的使用,帮我写个非常简单的就可以!

只要能说明怎么使用就可以!

易于理解!

多谢大哥大姐了!

最佳答案
  • 五星知识达人网友:夜风逐马
  • 2021-05-04 10:58

#include<stdio.h>
#include<malloc.h>
#define N sizeof(stu)
typedef struct students
{
int num;
struct students *next;
}stu;
int i,j;
void insertValue(stu *head);
void deleteValue(stu *head);
int main()
{
stu *p,*head;
head=(stu *)malloc(N);
p=(stu *)malloc(N);
p->num=7;
p->next=NULL;
head->next=p;
p=(stu *)malloc(N);
p->num=25;
p->next=NULL;
head->next->next=p;
p=head->next;
LL:
printf("\n------潇洒菠菜插入删除链表练习------\n");
printf("\t1.对现有链表的插入。\n");
printf("\t2.对现有链表的删除。\n");
printf("\t3.显示现有链表。\n");
printf("\t4.退出程序。\n");
printf("请选择<1-4>:");
scanf("%d",&i);
if(i==3)
{
p=head->next;
printf("\n现有链表如下:\n");
while(p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
}
if(i==4)
{
printf("\n您的宝贵建议是我成功制胜的关键!\n");
printf("\t\t谢谢使用!\n");
return 0;
}
if(i==1)
insertValue(head);
if(i==2)
deleteValue(head);
fflush(stdin);
goto LL;
}


void insertValue(stu *head)
{
int k;
stu *p1,*p2;
p1=head;
p2=(stu *)malloc(N);

printf("输入插入位置:");
scanf("%d",&i);
i--;
k=i;
while(p1!=NULL&&i>0)
{
p1=p1->next;
i--;
}

printf("输入插入的值:");
scanf("%d",&j);
p2->num=j;
if(k==0){
p2->next=head->next;
head->next=p2;
}else{
p2->next=p1->next;
p1->next=p2;
}
i=0;
printf("插入成功啦!...\n\n");
}


void deleteValue(stu *head)
{
stu *p1,*p2;
j=1;
p1=head->next;
printf("\n请输入删除的位置:");
scanf("%d",&i);
i--;
if(i==0)
{
head->next=head->next->next;
printf("删除成功啦!...\n");
}
else{
while(p1!=NULL||j<i)
{
p1=p1->next;
j++;
}
if(j!=i)
{
printf("删除位置错误\n");
}else
{
p1->next=p1->next->next;
printf("删除成功啦!...\n");
}
}
}



这个是我先前写的一个线性链表


对链表进行插入和删除操作!



更简单的就是拼接char*


比如!!!



c 字符串拼接



#include
#include


int main(void)
{
char fullpath [100] = "/home/dimgtui"; //记录路径名
printf("fullpath:%s ",fullpath);

char * ptr;//记录fullpath的最后位置,用于拼接
ptr = fullpath + strlen(fullpath);
*ptr ++ = '/';
*ptr = 0;


char * filename = "filename1";
strcpy(ptr,filename);
printf("fullpath:%s ",fullpath);

//恢复之前的fullpath
ptr[-1] = 0;
printf("fullpath:%s ",fullpath);
exit(0);
}


全部回答
  • 1楼网友:上分大魔王
  • 2021-05-04 12:54

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

#define HEAD struct listhead #define NODE struct listnode

void createList (HEAD *); int insertNode (HEAD *, NODE *, int); int emptyList (HEAD *); int getNext (HEAD *, int, int *); void printList (HEAD *); void destroyList (HEAD *); char menu(); void removeNode (HEAD *, int); void deleteNode (HEAD *, NODE *, NODE *, int); int searchList (HEAD *, NODE **, NODE **, int); int getdata();

HEAD { int count; NODE *pos; NODE *head; };

NODE { int data; NODE *link; };

void createList (HEAD *list) { list->count = 0; list->pos = NULL; list->head = NULL; }

int getdata() { int InsertData; printf("\nEnter the key to be inserted:"); scanf("%d", &InsertData); return InsertData; }

int insertNode (HEAD *list, NODE *pPre, int dataIn) { NODE *pNew; pNew = (NODE *)malloc(sizeof(NODE));

if (pNew == NULL) return 0;

pNew->data = dataIn; if (pPre == NULL) { pNew->link = list->head; list->head = pNew; } else { pNew->link = pPre->link; pPre->link = pNew; } list->count ++; return 1; }

int emptyList(HEAD *list) { return (list->count == 0); }

int getNext(HEAD *list, int fromWhere, int *dataOut) { int success; if (fromWhere == 0) if (emptyList(list) == 1) success = 0; else { list->pos = list->head; *dataOut = list->pos->data; success = 1; } else if (list->pos->link == NULL) success = 0; else { list->pos = list->pos->link; *dataOut = list->pos->data; success = 1; } return success; }

void printList(HEAD *list) { int count, moreData; int dataPtr; if (emptyList(list) == 1) printf("\nNo data in the list.\n"); else { printf("\n\nBegin data print...\n"); count = 0; moreData = getNext(list, 0, &dataPtr); while (moreData == 1) { count ++; printf(" %d: %d\n", count, dataPtr); moreData = getNext(list, 1, &dataPtr); } printf("End data print...\n"); } }

void destroyList (HEAD *list) { NODE *dltPtr; while (list->count > 0) { dltPtr = list->head; list->head = dltPtr->link; list->count --; free(dltPtr); } list->pos = NULL; }

char menu() { int valid, choice; printf("\n---Menu---\n"); printf("\n"); printf("A: Add new data\n"); printf("D: Delete data\n"); printf("P: Print List\n"); printf("Q: Quit\n"); printf("\n"); valid = 0; while (valid == 0) { printf("Enter your choice:"); choice = getch(); printf("\n"); if (strchr("AaDdPpQq", choice) != NULL) valid = 1; else printf("\nInvalid choice. Choices are <A, D, P, Q>:"); } return choice; }

void removeNode (HEAD *list, int dataOut) { int found; NODE *pPre, *pLoc; found = searchList(list, &pPre, &pLoc, dataOut); if (found) { deleteNode(list, pPre, pLoc, dataOut); printf("Data:%d was removed !",dataOut); } else printf("Data:%d not found, remove node fail!",dataOut); }

void deleteNode (HEAD *list, NODE *pPre, NODE *pLoc, int dataOut) { dataOut = pLoc->data; if (pPre == NULL) list->head = pLoc->link; else pPre->link = pLoc->link; list->count --; free(pLoc); }

int searchList(HEAD *list, NODE **pPre, NODE **pLoc, int target) { *pPre = NULL; *pLoc = list->head; while (*pLoc != NULL && target != (*pLoc)->data) { *pPre = *pLoc; *pLoc = (*pLoc)->link; } if (*pLoc == NULL) return 0; else if (target == (*pLoc)->data) return 1; else return 0; }

void main() { HEAD *list1; NODE *pPre, *pLoc; int dataIn; char option; int deleteKey; clrscr(); list1 = (HEAD *)malloc(sizeof(HEAD)); createList(list1); option = ' '; while (strchr("Qq", option) == NULL) { option = menu(); switch (option) { case 'a': case 'A': dataIn = getdata(); insertNode(list1, NULL, dataIn); break; case 'd': case 'D': printf("\nEnter the key to be deleted:"); scanf("%d", &deleteKey); removeNode(list1, deleteKey); break; case 'p': case 'P': printList(list1); break; } printf("\n"); } destroyList(list1); printf("\nPress any key to return..."); getch(); }

有哪儿不懂,来我们团队问吧~

  • 2楼网友:不甚了了
  • 2021-05-04 12:10
这个是我以前写的例子代码,修改了下贴了上来,包含插入,查找,删除和遍历4个基本操作 #include <stdio.h> #include <stdlib.h> typedef struct _list { int val; struct _list* next; } *node, list; // 在pos位置之后插一个节点 node insert( node* head, node pos, int val ) { node tmp; tmp = ( node )malloc( sizeof( list ) ); tmp->val = val; tmp->next = pos ? pos->next : *head; return ( pos ? pos->next : *head ) = tmp; } // 查找值为n的节点 node find( node head, int n ) { if ( !head ) return NULL; while ( head->val != n ) { head = head->next; } return head; } // 从数组简单构造一个链表 node create( int* beg, int* end ) { node head, t; head = t = NULL; while ( beg != end ) { t = insert( &head, t, *beg++ ); } return head; } // 删除节点n void remove( node* head, node n ) { node prev, next; prev = next = *head; while ( next != n ) { prev = next; next = next->next; } if ( prev == *head ) { *head = next->next; } else { prev->next = next->next; } free( next ); } // 遍历输出各个节点的值 void print( node head ) { while ( head ) { printf( "%d ", head->val ); head = head->next; } putchar( '\n' ); } // 删除链表 void del( node head ) { node tmp; while ( head ) { tmp = head; head = head->next; free( tmp ); } } int main() { int a[] = { 6,1,3,9,2,8,5,0,7,4 }; node head, pos; head = create( a, a + 10 ); print( head ); pos = find( head, 2 ); // 找到2的位置 insert( &head, pos, 999 ); // 在2后面插一个999 remove( &head, pos->next->next ); // 删除8 print( head ); del( head ); return 0; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯